Line data Source code
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 6 : gpmp::core::Logger::Logger(LogLevel level, bool useTimestamp) 47 6 : : logLevel(level), enableTimestamp(useTimestamp), logDestination(CONSOLE), 48 6 : logToFile(false) { 49 6 : } 50 : 51 6 : gpmp::core::Logger::~Logger() { 52 6 : if (logToFile) { 53 0 : logFileStream.close(); 54 : } 55 6 : } 56 : 57 0 : void gpmp::core::Logger::setLogLevel(LogLevel level) { 58 0 : logLevel = level; 59 0 : } 60 : 61 0 : void gpmp::core::Logger::enableTimestamps(bool enable) { 62 0 : enableTimestamp = enable; 63 0 : } 64 : 65 0 : void gpmp::core::Logger::setLogDestination(LogDestination destination) { 66 0 : logDestination = destination; 67 0 : } 68 : 69 0 : void gpmp::core::Logger::setLogFile(const std::string &logFile) { 70 0 : logToFile = true; 71 0 : logFileStream.open(logFile, std::ios::out | std::ios::app); 72 0 : if (!logFileStream.is_open()) { 73 0 : std::cerr << "Error: Could not open log file " << logFile << std::endl; 74 : } 75 0 : } 76 : 77 0 : void gpmp::core::Logger::log(LogLevel level, const std::string &message) { 78 0 : if (level >= logLevel) { 79 0 : std::string prefix = getLogPrefix(level); 80 0 : std::string logMessage = formatLogMessage(prefix, message); 81 : 82 0 : if (logDestination == CONSOLE || logDestination == CONSOLE_AND_FILE) { 83 0 : if (level == ERROR) { 84 0 : std::cerr << logMessage << std::endl; 85 : } else { 86 0 : std::cout << logMessage << std::endl; 87 : } 88 : } 89 0 : if (logDestination != CONSOLE && logToFile && logFileStream.is_open()) { 90 0 : logFileStream << logMessage << std::endl; 91 : } 92 0 : } 93 0 : } 94 : 95 0 : std::string gpmp::core::Logger::getLogPrefix(LogLevel level) { 96 0 : switch (level) { 97 0 : case DEBUG: 98 0 : return "[DEBUG]"; 99 0 : case INFO: 100 0 : return "[INFO]"; 101 0 : case WARNING: 102 0 : return "[WARN]"; 103 0 : case ERROR: 104 0 : return "[ERR]"; 105 0 : default: 106 0 : return "[UNKN]"; 107 : } 108 : } 109 : 110 0 : std::string gpmp::core::Logger::getCurrentTimestamp() { 111 0 : auto now = std::chrono::system_clock::now(); 112 0 : auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( 113 0 : now.time_since_epoch()) % 114 0 : 1000; 115 0 : std::time_t nowTime = std::chrono::system_clock::to_time_t(now); 116 0 : std::tm tm = *std::localtime(&nowTime); 117 : 118 0 : std::ostringstream oss; 119 : oss << std::put_time(&tm, "%H:%M:%S") << '.' << std::setfill('0') 120 0 : << std::setw(3) << ms.count(); 121 : 122 0 : return oss.str(); 123 0 : } 124 : 125 0 : std::string gpmp::core::Logger::formatLogMessage(const std::string &prefix, 126 : const std::string &message) { 127 0 : std::string logMessage = prefix + " " + message; 128 0 : if (enableTimestamp) { 129 0 : logMessage = getCurrentTimestamp() + " " + logMessage; 130 : } 131 0 : return logMessage; 132 0 : } 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 : }*/