openGPMP
Open Source Mathematics Package
utils.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file.
25  *
26  *
27  *
28  * This software is distributed on an AS IS basis, WITHOUT
29  * WARRANTY OF ANY KIND, either express or implied.
30  *
31  ************************************************************************/
32 
33 /*
34  * Utilities implementations for different helper methods/functions
35  * needed for openGPMP
36  */
37 #include <chrono>
38 #include <ctime>
39 #include <fstream>
40 #include <iomanip>
41 #include <iostream>
42 #include <openGPMP/core/utils.hpp>
43 #include <sstream>
44 #include <string>
45 
46 gpmp::core::Logger::Logger(LogLevel level, bool useTimestamp)
47  : logLevel(level), enableTimestamp(useTimestamp), logDestination(CONSOLE),
48  logToFile(false) {
49 }
50 
52  if (logToFile) {
53  logFileStream.close();
54  }
55 }
56 
58  logLevel = level;
59 }
60 
62  enableTimestamp = enable;
63 }
64 
66  logDestination = destination;
67 }
68 
69 void gpmp::core::Logger::setLogFile(const std::string &logFile) {
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 }
76 
77 void gpmp::core::Logger::log(LogLevel level, const std::string &message) {
78  if (level >= logLevel) {
79  std::string prefix = getLogPrefix(level);
80  std::string logMessage = formatLogMessage(prefix, message);
81 
82  if (logDestination == CONSOLE || logDestination == CONSOLE_AND_FILE) {
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 }
94 
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 }
109 
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 }
124 
125 std::string gpmp::core::Logger::formatLogMessage(const std::string &prefix,
126  const std::string &message) {
127  std::string logMessage = prefix + " " + message;
128  if (enableTimestamp) {
129  logMessage = getCurrentTimestamp() + " " + logMessage;
130  }
131  return logMessage;
132 }
133 
134 /*int main() {
135  // Create a logger with default settings (log to console with timestamps)
136  gpmp::core::Logger logger;
137 
138  // Log messages at various log levels
139  logger.log(DEBUG, "This is a debug message.");
140  logger.log(INFO, "This is an info message.");
141  logger.log(WARNING, "This is a warning message.");
142  logger.log(ERROR, "This is an error message.");
143 
144  return 0;
145 }*/
std::string getCurrentTimestamp()
Gets the current timestamp as a string.
Definition: utils.cpp:110
void setLogLevel(LogLevel level)
Sets the minimum log level.
Definition: utils.cpp:57
std::string formatLogMessage(const std::string &prefix, const std::string &message)
Formats a log message with a specified prefix.
Definition: utils.cpp:125
void setLogFile(const std::string &logFile)
Sets the log file name for FILE_ONLY or CONSOLE_AND_FILE destinations.
Definition: utils.cpp:69
void log(LogLevel level, const std::string &message)
Logs a message with the specified log level.
Definition: utils.cpp:77
void setLogDestination(LogDestination destination)
Sets the log destination.
Definition: utils.cpp:65
~Logger()
Logger destructor, destroys the Logger instance.
Definition: utils.cpp:51
void enableTimestamps(bool enable)
Enables or disables timestamps in log messages.
Definition: utils.cpp:61
std::string getLogPrefix(LogLevel level)
Gets the log prefix based on the log level.
Definition: utils.cpp:95
Logger(LogLevel level=INFO, bool useTimestamp=true)
Constructs a Logger instance.
Definition: utils.cpp:46
Miscellaneous utilities methods related to openGPMP.
LogDestination
Definition: utils.hpp:50
@ CONSOLE
Definition: utils.hpp:50
@ CONSOLE_AND_FILE
Definition: utils.hpp:50
LogLevel
Definition: utils.hpp:48
@ DEBUG
Definition: utils.hpp:48
@ ERROR
Definition: utils.hpp:48
@ INFO
Definition: utils.hpp:48
@ WARNING
Definition: utils.hpp:48