-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add logger #42
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3bfe012
Add logger
JakeRoggenbuck c4eda66
Complete logger
JakeRoggenbuck be264e8
Add newlines
JakeRoggenbuck f017764
Merge branch 'main' of github.com:jabacat/jml into add-logger
JakeRoggenbuck 97d7489
Fix merge conflicts
JakeRoggenbuck 05d0ea4
Add string import
JakeRoggenbuck fa88f35
Add model.cpp
JakeRoggenbuck 8d832ce
Update core/internal/logger.hpp
JakeRoggenbuck fc7f12a
Update core/src/logger.cpp
JakeRoggenbuck 2ee384a
Send all to standard error
JakeRoggenbuck fef7555
Merge branch 'add-logger' of github.com:JakeRoggenbuck/jml into add-l…
JakeRoggenbuck 4d3e02e
Log other files too
JakeRoggenbuck 0b0993a
Default parameters
JakeRoggenbuck 0eae707
Change err/warn levels
JakeRoggenbuck 1ada203
Add other WARN
JakeRoggenbuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 " | ||
<< 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ERR) << "Position " << pos << " out of bounds.\n"); | ||
} | ||
this->entries[pos] += val; | ||
} | ||
|
||
} | ||
} // namespace jml |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?