openGPMP
Open Source Mathematics Package
Public Member Functions | Private Member Functions | Private Attributes | List of all members
gpmp::core::Logger Class Reference

#include <utils.hpp>

Public Member Functions

 Logger (LogLevel level=INFO, bool useTimestamp=true)
 Constructs a Logger instance. More...
 
 ~Logger ()
 Logger destructor, destroys the Logger instance. More...
 
void setLogLevel (LogLevel level)
 Sets the minimum log level. More...
 
void enableTimestamps (bool enable)
 Enables or disables timestamps in log messages. More...
 
void setLogDestination (LogDestination destination)
 Sets the log destination. More...
 
void setLogFile (const std::string &logFile)
 Sets the log file name for FILE_ONLY or CONSOLE_AND_FILE destinations. More...
 
void log (LogLevel level, const std::string &message)
 Logs a message with the specified log level. More...
 

Private Member Functions

std::string getLogPrefix (LogLevel level)
 Gets the log prefix based on the log level. More...
 
std::string getCurrentTimestamp ()
 Gets the current timestamp as a string. More...
 
std::string formatLogMessage (const std::string &prefix, const std::string &message)
 Formats a log message with a specified prefix. More...
 

Private Attributes

LogLevel logLevel
 The minimum log level to record. More...
 
bool enableTimestamp
 Flag indicating whether to include timestamps in log messages. More...
 
LogDestination logDestination
 The log destination, which can be CONSOLE, FILE_ONLY, or CONSOLE_AND_FILE. More...
 
bool logToFile
 Flag indicating whether to log messages to a file. More...
 
std::ofstream logFileStream
 The output stream for logging to a file. More...
 

Detailed Description

Examples
linreg.cpp.

Definition at line 96 of file utils.hpp.

Constructor & Destructor Documentation

◆ Logger()

gpmp::core::Logger::Logger ( LogLevel  level = INFO,
bool  useTimestamp = true 
)

Constructs a Logger instance.

Parameters
levelThe minimum log level to record
useTimestampWhether to include timestamps in log messages (default: true)
destinationThe log destination, which can be CONSOLE, FILE_ONLY, or CONSOLE_AND_FILE (default: CONSOLE)
logFileThe name of the log file (if destination is FILE_ONLY or CONSOLE_AND_FILE)

Definition at line 46 of file utils.cpp.

47  : logLevel(level), enableTimestamp(useTimestamp), logDestination(CONSOLE),
48  logToFile(false) {
49 }
LogLevel logLevel
The minimum log level to record.
Definition: utils.hpp:152
LogDestination logDestination
The log destination, which can be CONSOLE, FILE_ONLY, or CONSOLE_AND_FILE.
Definition: utils.hpp:163
bool logToFile
Flag indicating whether to log messages to a file.
Definition: utils.hpp:168
bool enableTimestamp
Flag indicating whether to include timestamps in log messages.
Definition: utils.hpp:157
@ CONSOLE
Definition: utils.hpp:50

◆ ~Logger()

gpmp::core::Logger::~Logger ( )

Logger destructor, destroys the Logger instance.

Definition at line 51 of file utils.cpp.

51  {
52  if (logToFile) {
53  logFileStream.close();
54  }
55 }
std::ofstream logFileStream
The output stream for logging to a file.
Definition: utils.hpp:173

Member Function Documentation

◆ enableTimestamps()

void gpmp::core::Logger::enableTimestamps ( bool  enable)

Enables or disables timestamps in log messages.

Parameters
enableTrue to enable timestamps, false to disable

Definition at line 61 of file utils.cpp.

61  {
62  enableTimestamp = enable;
63 }

◆ formatLogMessage()

std::string gpmp::core::Logger::formatLogMessage ( const std::string &  prefix,
const std::string &  message 
)
private

Formats a log message with a specified prefix.

Parameters
prefixThe log message prefix
messageThe log message content
Returns
The formatted log message as a string

Definition at line 125 of file utils.cpp.

126  {
127  std::string logMessage = prefix + " " + message;
128  if (enableTimestamp) {
129  logMessage = getCurrentTimestamp() + " " + logMessage;
130  }
131  return logMessage;
132 }
std::string getCurrentTimestamp()
Gets the current timestamp as a string.
Definition: utils.cpp:110

◆ getCurrentTimestamp()

std::string gpmp::core::Logger::getCurrentTimestamp ( )
private

Gets the current timestamp as a string.

Returns
A string representing the current timestamp

Definition at line 110 of file utils.cpp.

110  {
111  auto now = std::chrono::system_clock::now();
112  auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
113  now.time_since_epoch()) %
114  1000;
115  std::time_t nowTime = std::chrono::system_clock::to_time_t(now);
116  std::tm tm = *std::localtime(&nowTime);
117 
118  std::ostringstream oss;
119  oss << std::put_time(&tm, "%H:%M:%S") << '.' << std::setfill('0')
120  << std::setw(3) << ms.count();
121 
122  return oss.str();
123 }

◆ getLogPrefix()

std::string gpmp::core::Logger::getLogPrefix ( LogLevel  level)
private

Gets the log prefix based on the log level.

Parameters
levelThe log level
Returns
A string representing the log prefix

Definition at line 95 of file utils.cpp.

95  {
96  switch (level) {
97  case DEBUG:
98  return "[DEBUG]";
99  case INFO:
100  return "[INFO]";
101  case WARNING:
102  return "[WARN]";
103  case ERROR:
104  return "[ERR]";
105  default:
106  return "[UNKN]";
107  }
108 }
@ DEBUG
Definition: utils.hpp:48
@ ERROR
Definition: utils.hpp:48
@ INFO
Definition: utils.hpp:48
@ WARNING
Definition: utils.hpp:48

References DEBUG, ERROR, INFO, and WARNING.

◆ log()

void gpmp::core::Logger::log ( LogLevel  level,
const std::string &  message 
)

Logs a message with the specified log level.

Parameters
levelThe log level of the message
messageThe message to log
Examples
linreg.cpp.

Definition at line 77 of file utils.cpp.

77  {
78  if (level >= logLevel) {
79  std::string prefix = getLogPrefix(level);
80  std::string logMessage = formatLogMessage(prefix, message);
81 
83  if (level == ERROR) {
84  std::cerr << logMessage << std::endl;
85  } else {
86  std::cout << logMessage << std::endl;
87  }
88  }
89  if (logDestination != CONSOLE && logToFile && logFileStream.is_open()) {
90  logFileStream << logMessage << std::endl;
91  }
92  }
93 }
std::string formatLogMessage(const std::string &prefix, const std::string &message)
Formats a log message with a specified prefix.
Definition: utils.cpp:125
std::string getLogPrefix(LogLevel level)
Gets the log prefix based on the log level.
Definition: utils.cpp:95
@ CONSOLE_AND_FILE
Definition: utils.hpp:50

References CONSOLE, CONSOLE_AND_FILE, and ERROR.

Referenced by gpmp::ml::LinearRegression::best_fit(), gpmp::core::DataTable::datetime(), gpmp::ml::LinearRegression::get_input(), gpmp::core::DataTable::group_by(), gpmp::core::DataTable::inferType(), gpmp::core::DataTable::native_type(), gpmp::ml::LinearRegression::r_sqrd(), gpmp::ml::LinearRegression::split_data(), and test_train().

◆ setLogDestination()

void gpmp::core::Logger::setLogDestination ( LogDestination  destination)

Sets the log destination.

Parameters
destinationThe log destination, which can be CONSOLE, FILE_ONLY, or CONSOLE_AND_FILE

Definition at line 65 of file utils.cpp.

65  {
66  logDestination = destination;
67 }

◆ setLogFile()

void gpmp::core::Logger::setLogFile ( const std::string &  logFile)

Sets the log file name for FILE_ONLY or CONSOLE_AND_FILE destinations.

Parameters
logFileThe name of the log file

Definition at line 69 of file utils.cpp.

69  {
70  logToFile = true;
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;
74  }
75 }

◆ setLogLevel()

void gpmp::core::Logger::setLogLevel ( LogLevel  level)

Sets the minimum log level.

Parameters
levelThe minimum log level to record

Definition at line 57 of file utils.cpp.

57  {
58  logLevel = level;
59 }

Member Data Documentation

◆ enableTimestamp

bool gpmp::core::Logger::enableTimestamp
private

Flag indicating whether to include timestamps in log messages.

Definition at line 157 of file utils.hpp.

◆ logDestination

LogDestination gpmp::core::Logger::logDestination
private

The log destination, which can be CONSOLE, FILE_ONLY, or CONSOLE_AND_FILE.

Definition at line 163 of file utils.hpp.

◆ logFileStream

std::ofstream gpmp::core::Logger::logFileStream
private

The output stream for logging to a file.

Definition at line 173 of file utils.hpp.

◆ logLevel

LogLevel gpmp::core::Logger::logLevel
private

The minimum log level to record.

Definition at line 152 of file utils.hpp.

◆ logToFile

bool gpmp::core::Logger::logToFile
private

Flag indicating whether to log messages to a file.

Definition at line 168 of file utils.hpp.


The documentation for this class was generated from the following files: