47 : logLevel(level), enableTimestamp(useTimestamp), logDestination(
CONSOLE),
53 logFileStream.close();
62 enableTimestamp = enable;
66 logDestination = destination;
71 logFileStream.open(logFile, std::ios::out | std::ios::app);
72 if (!logFileStream.is_open()) {
73 std::cerr <<
"Error: Could not open log file " << logFile << std::endl;
78 if (level >= logLevel) {
79 std::string prefix = getLogPrefix(level);
80 std::string logMessage = formatLogMessage(prefix, message);
84 std::cerr << logMessage << std::endl;
86 std::cout << logMessage << std::endl;
89 if (logDestination !=
CONSOLE && logToFile && logFileStream.is_open()) {
90 logFileStream << logMessage << std::endl;
111 auto now = std::chrono::system_clock::now();
112 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
113 now.time_since_epoch()) %
115 std::time_t nowTime = std::chrono::system_clock::to_time_t(now);
116 std::tm tm = *std::localtime(&nowTime);
118 std::ostringstream oss;
119 oss << std::put_time(&tm,
"%H:%M:%S") <<
'.' << std::setfill(
'0')
120 << std::setw(3) << ms.count();
126 const std::string &message) {
127 std::string logMessage = prefix +
" " + message;
128 if (enableTimestamp) {
129 logMessage = getCurrentTimestamp() +
" " + logMessage;
std::string getCurrentTimestamp()
Gets the current timestamp as a string.
void setLogLevel(LogLevel level)
Sets the minimum log level.
std::string formatLogMessage(const std::string &prefix, const std::string &message)
Formats a log message with a specified prefix.
void setLogFile(const std::string &logFile)
Sets the log file name for FILE_ONLY or CONSOLE_AND_FILE destinations.
void log(LogLevel level, const std::string &message)
Logs a message with the specified log level.
void setLogDestination(LogDestination destination)
Sets the log destination.
~Logger()
Logger destructor, destroys the Logger instance.
void enableTimestamps(bool enable)
Enables or disables timestamps in log messages.
std::string getLogPrefix(LogLevel level)
Gets the log prefix based on the log level.
Logger(LogLevel level=INFO, bool useTimestamp=true)
Constructs a Logger instance.
Miscellaneous utilities methods related to openGPMP.