From 09a543d9f5ac51c30b834bdb2ddd735d767f9289 Mon Sep 17 00:00:00 2001 From: denes Date: Wed, 6 Nov 2019 13:43:21 +0100 Subject: [PATCH] prefix with time --- ArgParser.hpp | 26 +++++++++++++++++++------- CMakeLists.txt | 2 +- Logger.hpp | 20 ++++++++++++++++++-- main.cpp | 6 ++++-- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/ArgParser.hpp b/ArgParser.hpp index c7e9d89..ff4ab4e 100644 --- a/ArgParser.hpp +++ b/ArgParser.hpp @@ -9,7 +9,12 @@ class ArgParser { public: - ArgParser() : m_use_console(false), m_logFile() {} + ArgParser() + : m_print_to_console(false) + , m_print_timestamp(false) + , m_logFile() + {} + void printUsage(bool cout = true) { std::ostringstream oss; @@ -18,6 +23,7 @@ public: << "Logs the input (till EOF) to the console or to a file.\n" << "\n" << "Options:\n" + << " -t, prefix lines with timestamp\n" << " -c, log to console\n" << " -f FILE log to FILE\n" << " -h print this help\n" @@ -31,13 +37,17 @@ public: else std::cerr << oss.str(); } + int parse(int argc, char* argv[]) { int c; - while ((c = getopt (argc, argv, "cf:h")) != -1) { + while ((c = getopt (argc, argv, "ctf:h")) != -1) { switch (c) { case 'c': - m_use_console = true; + m_print_to_console = true; + break; + case 't': + m_print_timestamp = true; break; case 'f': m_logFile = std::string(optarg); @@ -50,22 +60,24 @@ public: return EXIT_FAILURE; } } - if ((m_use_console && !m_logFile.empty()) || - (!m_use_console && m_logFile.empty())) { + if ((m_print_to_console && !m_logFile.empty()) || + (!m_print_to_console && m_logFile.empty())) { printUsage(false); return EXIT_FAILURE; } return -1; } - bool useConsole() const { return m_use_console; } + bool printToConsole() const { return m_print_to_console; } + bool printTimestamp() const { return m_print_timestamp; } std::string logFile() const { return m_logFile; } private: int m_argc; char** m_argv; - bool m_use_console; + bool m_print_to_console; + bool m_print_timestamp; std::string m_logFile; }; diff --git a/CMakeLists.txt b/CMakeLists.txt index fba0a6d..48033da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project (CppLogger) # Using Catch2's benchmarking tools set(CATCH_HEADER_FILES Logger.hpp Singleton_DCLP.hpp ArgParser.hpp) add_executable(cpplogger main.cpp ${HEADER_FILES}) - +set_target_properties(cpplogger PROPERTIES COMPILE_FLAGS "-ggdb") diff --git a/Logger.hpp b/Logger.hpp index ff44ab2..9766d4d 100644 --- a/Logger.hpp +++ b/Logger.hpp @@ -2,18 +2,34 @@ #define LOGGER_HPP #include -#include // move +#include #include "Singleton_DCLP.hpp" class Logger : public Singleton_DCLP { public: + Logger() : m_os(nullptr), m_print_time_stamp(false) {} + void setStream(std::ostream* os) { m_os = os; } - std::ostream* getStream() { return m_os; } + std::ostream* getStream() + { + if (m_print_time_stamp) { + const std::time_t timenow = std::time(nullptr); + std::string s(std::ctime(&timenow)); + s.pop_back(); // removing trailing \n + *m_os << s << " "; + } + + return m_os; + } + + void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; } + private: // ostreams are not copyable, hence storing a pointer only std::ostream* m_os; + bool m_print_time_stamp; }; #define LOG *Logger::getInstance()->getStream() diff --git a/main.cpp b/main.cpp index 787574a..330897c 100644 --- a/main.cpp +++ b/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char* argv[]) if (r != -1) return r; std::ofstream logfile; - if (argparser.useConsole()) { + if (argparser.printToConsole()) { Logger::getInstance()->setStream(&std::cout); } else { logfile.open (argparser.logFile(), @@ -25,11 +25,13 @@ int main(int argc, char* argv[]) Logger::getInstance()->setStream(&logfile); } + Logger::getInstance()->setPrintTimeStamp(argparser.printTimestamp()); + std::string line; while (std::getline(std::cin, line)) LOG << line << std::endl; - if (!argparser.useConsole()) + if (!argparser.printToConsole()) logfile.close(); return EXIT_SUCCESS;