-
Notifications
You must be signed in to change notification settings - Fork 1
Simple Logger #30
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
Simple Logger #30
Changes from 5 commits
cce957f
1e0b767
a5d8520
69855fb
5959b5a
c5a1469
a7830d5
2f940ae
77b54fb
d17c12c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @file Logger.cpp | ||
* @author Anand Doshi | ||
* @date 2025-01-08 | ||
* | ||
* @copyright Copyright (c) 2025 | ||
* | ||
*/ | ||
|
||
|
||
#include "Logger.hpp" | ||
#include <cstdio> | ||
#include <ctime> | ||
#include <iterator> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <string_view> | ||
|
||
bool Logger::isLoggable(Level level) | ||
{ | ||
if (filter == Filter::quiet) | ||
{ | ||
return false; | ||
} | ||
|
||
if (filter == Filter::important) | ||
{ | ||
return level == Level::error || level == Level::warning || level == Level::severe; | ||
} | ||
|
||
if (filter == Filter::verbose) | ||
{ | ||
return true; | ||
} | ||
|
||
return true; // unknown filter, everything logged | ||
} | ||
|
||
constexpr std::string_view Logger::getLevelName(Logger::Level level) | ||
{ | ||
switch (level) | ||
{ | ||
case Logger::Level::error: | ||
return "error"; | ||
case Logger::Level::warning: | ||
return "warning"; | ||
case Logger::Level::severe: | ||
return "severe"; | ||
case Logger::Level::info: | ||
return "info"; | ||
case Logger::Level::fine: | ||
return "fine"; | ||
default: | ||
throw std::invalid_argument("Invalid Level enum."); | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
void Logger::log(Level level, std::string_view message) | ||
{ | ||
if (isLoggable(level)) | ||
{ | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::time_t now = std::time(nullptr); | ||
|
||
char timestamp[std::size(Logger::timestampFormat)]; | ||
std::strftime(timestamp, std::size(Logger::timestampFormat), | ||
"%FT%TZ", std::localtime(&now)); | ||
logStream << '[' << timestamp << "] " << '(' << name << ") " << '[' << getLevelName(level) << "] " << message << '\n'; | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
void Logger::error(std::string_view message) | ||
{ | ||
log(Level::error, message); | ||
} | ||
|
||
void Logger::warning(std::string_view message) | ||
{ | ||
log(Level::warning, message); | ||
} | ||
|
||
void Logger::severe(std::string_view message) | ||
{ | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log(Level::severe, message); | ||
} | ||
|
||
void Logger::info(std::string_view message) | ||
{ | ||
log(Level::info, message); | ||
} | ||
|
||
void Logger::fine(std::string_view message) | ||
{ | ||
log(Level::fine, message); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @file Logger.hpp | ||
* @author Anand Doshi | ||
* @date 2025-01-08 | ||
* | ||
* @copyright Copyright (c) 2025 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've done some more thinking. I think the logger should be a singleton. We need an instance() public method and the constructor should be protected to prevent initialisation by the user. Then the info, error, ... methods become static methods calling instance().log(...) |
||
* | ||
*/ | ||
|
||
|
||
#include <iostream> | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <string_view> | ||
|
||
class Logger | ||
{ | ||
public: | ||
enum class Filter | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the Filter is overkill. Personally, I would just use a second Level variable. Then you can compare the level of the message being passed in to the level of our Level variable since enum values are just increasing integers. |
||
quiet, | ||
verbose, | ||
important | ||
}; | ||
|
||
enum class Level | ||
{ | ||
error, | ||
warning, | ||
severe, | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
info, | ||
fine, | ||
}; | ||
|
||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Logger(std::string_view name) | ||
: Logger{name, Filter::important, std::cout} | ||
{ | ||
} | ||
|
||
Logger(std::string_view name, Filter filter) | ||
: Logger{name, filter, std::cout} | ||
{ | ||
} | ||
|
||
Logger(std::string_view name, Filter filter, std::ostream &logStream) | ||
: name{name}, filter{filter}, logStream{logStream} | ||
{ | ||
} | ||
|
||
void setFilter(Filter filterToSet) { filter = filterToSet; } | ||
|
||
void log(Level level, std::string_view message); | ||
void error(std::string_view message); | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void warning(std::string_view message); | ||
void severe(std::string_view message); | ||
void info(std::string_view message); | ||
void fine(std::string_view message); | ||
|
||
private: | ||
std::string name; | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Filter filter; | ||
EdwardPalmer99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::ostream &logStream; | ||
|
||
// ISO 8601 date time format | ||
inline static const std::string timestampFormat{"yyyy-mm-ddThh:mm:ssZ"}; | ||
|
||
bool isLoggable(Level level); | ||
|
||
static constexpr std::string_view getLevelName(Level level); | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.