Skip to content

Commit

Permalink
Merge pull request #63 from nodec-project/feature/nodec-game-engine-i…
Browse files Browse the repository at this point in the history
…ssue47

feat: Add timestamp to LogRecord struct
  • Loading branch information
ContentsViewer authored Nov 19, 2023
2 parents 758ad01 + 0fd7462 commit 75be93e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions include/nodec/logging/log_record.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef NODEC__LOGGING__LOG_RECORD_HPP_
#define NODEC__LOGGING__LOG_RECORD_HPP_

#include <chrono>
#include <cstddef>
#include <string>

Expand All @@ -10,14 +11,16 @@ namespace nodec {
namespace logging {

struct LogRecord {
LogRecord(const std::string &name, Level level, const std::string &message, const char *file, std::size_t line)
: name(name), level(level), message(message), file(file), line(line) {}
public:
LogRecord(std::chrono::system_clock::time_point time, const std::string &name, Level level, const std::string &message, const char *file, std::size_t line)
: time(time), name(name), level(level), message(message), file(file), line(line) {}

const std::chrono::system_clock::time_point time;
const std::string &name;
Level level;
const Level level;
const std::string &message;
const char *file;
std::size_t line;
const std::size_t line;
};

} // namespace logging
Expand Down
16 changes: 8 additions & 8 deletions include/nodec/logging/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Logger {
parent_->handle(record);
}
}

class LogStream {
public:
LogStream(Logger &logger, Level level, const char *file, std::size_t line)
Expand Down Expand Up @@ -122,15 +122,15 @@ class Logger {
* DEBUG: Detailed information, typically of interest only when diagnosing problems.
*/
void debug(const std::string &message, const char *file, std::size_t line) {
handle(LogRecord{name_, Level::Debug, message, file, line});
handle(LogRecord{std::chrono::system_clock::now(), name_, Level::Debug, message, file, line});
}

/**
* @detail
* INFO: Confirmation that things are working as expected.
*/
void info(const std::string &message, const char *file, std::size_t line) {
handle(LogRecord{name_, Level::Info, message, file, line});
handle(LogRecord{std::chrono::system_clock::now(), name_, Level::Info, message, file, line});
}

/**
Expand All @@ -139,30 +139,30 @@ class Logger {
* The software is still working as expected.
*/
void warn(const std::string &message, const char *file, std::size_t line) {
handle(LogRecord{name_, Level::Warn, message, file, line});
handle(LogRecord{std::chrono::system_clock::now(), name_, Level::Warn, message, file, line});
}

/**
* @detail
* ERROR: Due to a more serious problem, the software has not been able to perform some function.
*/
void error(const std::string &message, const char *file, std::size_t line) {
handle(LogRecord{name_, Level::Error, message, file, line});
handle(LogRecord{std::chrono::system_clock::now(), name_, Level::Error, message, file, line});
}

/**
* @detail
* FATAL: A serious error, indicating that the program itself may be unable to continue running.
*/
void fatal(const std::string &message, const char *file, std::size_t line) {
handle(LogRecord{name_, Level::Fatal, message, file, line});
handle(LogRecord{std::chrono::system_clock::now(), name_, Level::Fatal, message, file, line});
}

/**
* @brief Logs a message with level.
*/
void log(Level level, const std::string &message, const char *file, std::size_t line) {
handle(LogRecord{name_, level, message, file, line});
handle(LogRecord{std::chrono::system_clock::now(), name_, level, message, file, line});
}

LogStream debug(const char *file, std::size_t line) {
Expand Down

0 comments on commit 75be93e

Please sign in to comment.