Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger #42

Merged
merged 15 commits into from
Oct 9, 2023
20 changes: 11 additions & 9 deletions core/include/jml/math/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@
#include <memory>

#include <jml/math/vector.hpp>
#include <internal/logger.hpp>

namespace jml {

extern Logger *LOGGER;

class Matrix {

int m; // Number of rows
int n; // Number of columns
int m; // Number of rows
int n; // Number of columns

double * entries;
double *entries;

// This gets the array position for a particular row and column combination.
int get_position(int i, int j);
// This gets the array position for a particular row and column combination.
int get_position(int i, int j);

public:
Matrix(int m, int n);
public:
Matrix(int m, int n);
~Matrix();
int get_n_rows();
int get_n_cols();
void set_entry(int i, int j, double value);
std::unique_ptr<Vector> multiply(std::unique_ptr<Vector>);

};

}
} // namespace jml
3 changes: 3 additions & 0 deletions core/include/jml/math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#pragma once

#include <jml/math/activation_functions.hpp>
#include <internal/logger.hpp>

namespace jml {

extern Logger *LOGGER;

class Vector {

int length; // The number of entries in the vector
Expand Down
58 changes: 58 additions & 0 deletions core/internal/logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* This is the header for the logger.
* Very little of the logger will be exposed through the API.
* Author: Jake Roggenbuck
* 10-08-23
*/

#pragma once

#include <iostream>

namespace jml {

enum Severity { DEBUG, INFO, WARN, ERR, FATAL };

static const std::string severity_text[] = {"DEBUG", "INFO", "WARN", "ERR",
"FATAL"};

const std::string get_severity_text(enum Severity s);

class Log {
private:
enum Severity severity;
std::string message;

public:
Log &operator<<(enum Severity s);
Log &operator<<(std::string m);
Log &operator<<(int m);

enum Severity get_severity();
std::string get_message();

Log(enum Severity s = WARN);
Log(enum Severity s, std::string m);
};

class Logger {
private:
enum Severity global_log_level;
static Logger *_instance;

Logger(enum Severity s = WARN);

public:
static Logger *Instance(enum Severity s = WARN);

std::string render(Log l);

// Add log to the logs vector and display it if it's the correct logging
// level or more severe
void log(enum Severity s, std::string m);
void log(Log l);

void set_global_log_level(enum Severity s);
};

} // namespace jml
2 changes: 1 addition & 1 deletion core/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
core_sources = ['src/math/matrix.cpp', 'src/math/vector.cpp', 'src/math/activation_functions.cpp', 'src/model.cpp', 'capi/cjml.cpp']
core_sources = ['src/math/matrix.cpp', 'src/math/vector.cpp', 'src/math/activation_functions.cpp', 'src/model.cpp', 'src/logger.cpp', 'capi/cjml.cpp']
jmlcore = library('jmlcore',
core_sources,
include_directories: core_inc,
Expand Down
63 changes: 63 additions & 0 deletions core/src/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <internal/logger.hpp>
#include <iostream>
#include <string>

namespace jml {

const std::string get_severity_text(enum Severity s) {
return severity_text[(int)s];
}

enum Severity Log::get_severity() { return this->severity; }

std::string Log::get_message() { return this->message; }

Log &Log::operator<<(enum Severity s) {
this->severity = s;
return *this;
}

Log &Log::operator<<(std::string m) {
this->message += m;
return *this;
}

Log &Log::operator<<(int m) {
this->message += std::to_string(m);
return *this;
}

Log::Log(enum Severity s) : severity(s) {}
Log::Log(enum Severity s, std::string m) : severity(s), message(m) {}

Logger *Logger::_instance = nullptr;
Logger *Logger::Instance(enum Severity s) {
if (_instance == nullptr) {
_instance = new Logger(s);
}
return _instance;
}

Logger::Logger(enum Severity s) { this->global_log_level = s; }

std::string Logger::render(Log l) {
return "[" + get_severity_text(l.get_severity()) + "]: " + l.get_message();
}

void Logger::log(enum Severity s, std::string m) {
Log l = Log(s, m);
this->log(l);
}

void Logger::log(Log l) {
// Print log if log level is low enough for l
if (l.get_severity() >= this->global_log_level) {
std::cerr << this->render(l);
}
}

void Logger::set_global_log_level(enum Severity s) {
this->global_log_level = s;
}

} // namespace jml
38 changes: 15 additions & 23 deletions core/src/math/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,50 @@
#include <jml/math/matrix.hpp>
#include <internal/logger.hpp>

#include <iostream>

namespace jml {

Matrix::Matrix(int m, int n) {
this->entries = new double(m * n);
}
Matrix::Matrix(int m, int n) { this->entries = new double(m * n); }

Matrix::~Matrix() {
delete this->entries;
}
Matrix::~Matrix() { delete this->entries; }

int Matrix::get_n_rows() {
return m;
}
int Matrix::get_position(int i, int j) { return i * this->n + j; }

int Matrix::get_n_cols() {
return n;
}
int Matrix::get_n_rows() { return m; }

int Matrix::get_position(int i, int j) {
return i * this->n + j;
}
int Matrix::get_n_cols() { return n; }

void Matrix::set_entry(int i, int j, double value) {

if (i < 0 || i >= this->m) {
std::cout << "Row number " << i << " out of bounds." << std::endl;
LOGGER->log(Log() << ERR << "Row number " << i << " out of bounds.\n");
}
if (j < 0 || j >= this->n) {
std::cout << "Column number " << i << " out of bounds." << std::endl;
LOGGER->log(Log() << ERR << "Column number " << i
<< " out of bounds.\n");
}

this->entries[get_position(i, j)] = value;

}

std::unique_ptr<Vector> Matrix::multiply(std::unique_ptr<Vector> in) {

if (in->get_size() != this->n) {
std::cout << "Tried to multiply a vector of size " << in->get_size()
<< " with a matrix containing " << this->n << " columns." << std::endl;
LOGGER->log(Log(WARN) << "Tried to multiply a vector of size "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a warning? It seems like an error. (Also with the one for vectors below). What do you think?

<< in->get_size() << " with a matrix containing "
<< this->n << " columns.\n");
}

std::unique_ptr<Vector> ret = std::make_unique<Vector>(this->m);

for (int i = 0; i < this->m; ++i) {
for (int j = 0; j < this->n; ++j)
ret->add_entry(i, in->get_entry(j) * this->entries[get_position(i, j)]);
ret->add_entry(i, in->get_entry(j) *
this->entries[get_position(i, j)]);
}

return ret;

}

}
} // namespace jml
29 changes: 14 additions & 15 deletions core/src/math/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
#include <jml/math/vector.hpp>
#include <internal/logger.hpp>

#include <cstring>
#include <iostream>

namespace jml {

Logger *LOGGER = Logger::Instance();

Vector::Vector(int length) {

this->entries = new double(length);
memset(this->entries, 0, length * sizeof(double));

this->length = length;

}

Vector::~Vector() {
delete this->entries;
}
Vector::~Vector() { delete this->entries; }

void Vector::apply(ActivationFunction * fn) {
void Vector::apply(ActivationFunction *fn) {
for (int i = 0; i < this->length; ++i) {
this->entries[i] = fn->f(this->entries[i]);
}
}

void Vector::add(Vector& v) {
void Vector::add(Vector &v) {
if (this->length != v.length) {
std::cout << "Tried to add a vector of length " << v.length <<
"to a vector of length " << this->length << "." << std::endl;
LOGGER->log(Log(WARN)
<< "Tried to add a vector of length " << v.length
<< "to a vector of length " << this->length << ".\n");
}
for (int i = 0; i < this->length; ++i) {
this->entries[i] += v.entries[i];
}
}

int Vector::get_size() {
return this->length;
}
int Vector::get_size() { return this->length; }

double Vector::get_entry(int pos) {
if (pos < 0 || pos >= this->length) {
std::cout << "Position " << pos << " out of bounds." << std::endl;
LOGGER->log(Log(ERR) << "Position " << pos << " out of bounds.\n");
}
return this->entries[pos];
}

void Vector::set_entry(int pos, double val) {
if (pos < 0 || pos >= this->length) {
std::cout << "Position " << pos << " out of bounds." << std::endl;
LOGGER->log(Log(ERR) << "Position " << pos << " out of bounds.\n");
}
this->entries[pos] = val;
}

void Vector::add_entry(int pos, double val) {
if (pos < 0 || pos >= this->length) {
std::cout << "Position " << pos << " out of bounds." << std::endl;
LOGGER->log(Log(WARN) << "Position " << pos << " out of bounds.\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a WARN when the other two are ERR?

}
this->entries[pos] += val;
}

}
} // namespace jml
Loading