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

feat: Add timestamp to LogRecord struct #63

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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