From 98cc8b0b6519a8cc6c477040ff2171ab79f0076b Mon Sep 17 00:00:00 2001 From: denes Date: Mon, 11 Nov 2019 14:26:32 +0100 Subject: [PATCH] END --- ArgParser.hpp | 24 ++++++++++++++++-------- Logger.hpp | 29 ++++++++++++++++++++++------- main.cpp | 2 +- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/ArgParser.hpp b/ArgParser.hpp index ff4ab4e..c6d8f0e 100644 --- a/ArgParser.hpp +++ b/ArgParser.hpp @@ -11,8 +11,10 @@ class ArgParser public: ArgParser() : m_print_to_console(false) - , m_print_timestamp(false) , m_logFile() + , m_timestamp(false) + , m_hostname(false) + , m_username(false) {} void printUsage(bool cout = true) @@ -23,12 +25,14 @@ 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" + << " -t, prefix lines with timestamp\n" + << " -n, prefix lines with hostname\n" + << " -u, prefix lines with user name\n" << " -h print this help\n" << "\n" - << "Option -c and -f are mutually exclusive.\n" + << "Option -c and -f are mutually exclusive, specify one.\n" << "\n" << "Denes Matetelki \n"; @@ -41,13 +45,13 @@ public: int parse(int argc, char* argv[]) { int c; - while ((c = getopt (argc, argv, "ctf:h")) != -1) { + while ((c = getopt (argc, argv, "cf:tnuh")) != -1) { switch (c) { case 'c': m_print_to_console = true; break; case 't': - m_print_timestamp = true; + m_timestamp = true; break; case 'f': m_logFile = std::string(optarg); @@ -69,7 +73,9 @@ public: } bool printToConsole() const { return m_print_to_console; } - bool printTimestamp() const { return m_print_timestamp; } + bool printTimestamp() const { return m_timestamp; } + bool printHostname() const { return m_hostname; } + bool printUsername() const { return m_username; } std::string logFile() const { return m_logFile; } private: @@ -77,10 +83,12 @@ private: char** m_argv; bool m_print_to_console; - bool m_print_timestamp; std::string m_logFile; + bool m_timestamp; + bool m_hostname; + bool m_username; }; -#define LOG *Logger::getInstance()->getStream() +// #define LOG *Logger::getInstance()->getStream() #endif // ARGPARSER_HPP diff --git a/Logger.hpp b/Logger.hpp index 9766d4d..34d7da0 100644 --- a/Logger.hpp +++ b/Logger.hpp @@ -9,10 +9,29 @@ class Logger : public Singleton_DCLP { public: + class Endl {}; + Logger() : m_os(nullptr), m_print_time_stamp(false) {} void setStream(std::ostream* os) { m_os = os; } - std::ostream* getStream() + std::ostream* getStream() { return m_os; } + + void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; } + + template + Logger &operator<<(const T &x) { + decorate(); + *m_os << x; + return *this; + } + + Logger& operator<<(const Logger::Endl& e) { + *m_os << std::endl; + return *this; + } + +private: + void decorate() { if (m_print_time_stamp) { const std::time_t timenow = std::time(nullptr); @@ -20,18 +39,14 @@ public: 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() +#define LOG *Logger::getInstance() +#define END Logger::Endl(); #endif // LOGGER_HPP diff --git a/main.cpp b/main.cpp index 330897c..0cc7d37 100644 --- a/main.cpp +++ b/main.cpp @@ -29,7 +29,7 @@ int main(int argc, char* argv[]) std::string line; while (std::getline(std::cin, line)) - LOG << line << std::endl; + LOG << line << END; if (!argparser.printToConsole()) logfile.close();