From 32131e3e55ee4f0b8908de81c3c9b13d71b53b2d Mon Sep 17 00:00:00 2001 From: denes Date: Mon, 11 Nov 2019 14:57:03 +0100 Subject: [PATCH] print owner of process --- ArgParser.hpp | 7 +++++-- Logger.hpp | 21 ++++++++++++++++++++- main.cpp | 1 + 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ArgParser.hpp b/ArgParser.hpp index c6d8f0e..d17dc46 100644 --- a/ArgParser.hpp +++ b/ArgParser.hpp @@ -50,11 +50,14 @@ public: case 'c': m_print_to_console = true; break; + case 'f': + m_logFile = std::string(optarg); + break; case 't': m_timestamp = true; break; - case 'f': - m_logFile = std::string(optarg); + case 'u': + m_username = true; break; case 'h': printUsage(); diff --git a/Logger.hpp b/Logger.hpp index feafe17..6fc1b06 100644 --- a/Logger.hpp +++ b/Logger.hpp @@ -1,6 +1,10 @@ #ifndef LOGGER_HPP #define LOGGER_HPP + +#include +#include + #include #include @@ -13,11 +17,16 @@ class Logger : public Singleton_DCLP public: class Endl {}; - Logger() : m_os(nullptr), m_buffer(), m_print_time_stamp(false) {} + Logger() : + m_os(nullptr), + m_buffer(), + m_print_time_stamp(false), + m_print_user_name(false) {} void setStream(std::ostream* os) { m_os = os; } void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; } + void setPrintUserName(bool b = true) { m_print_user_name = b; } template Logger &operator<<(const T &x) { @@ -42,12 +51,22 @@ private: s.pop_back(); // removing trailing \n *m_os << s << " "; } + if (m_print_user_name) { +#ifdef linux + const uid_t uid = getuid(); + const struct passwd* p = getpwuid(uid); + *m_os << p->pw_name << " "; +#elif _win32 + *m_os << "n00b "; +#endif + } } // ostreams are not copyable, hence storing a pointer only std::ostream* m_os; std::ostringstream m_buffer; bool m_print_time_stamp; + bool m_print_user_name; }; #define LOG *Logger::getInstance() diff --git a/main.cpp b/main.cpp index 0cc7d37..29a30c2 100644 --- a/main.cpp +++ b/main.cpp @@ -26,6 +26,7 @@ int main(int argc, char* argv[]) } Logger::getInstance()->setPrintTimeStamp(argparser.printTimestamp()); + Logger::getInstance()->setPrintUserName(argparser.printUsername()); std::string line; while (std::getline(std::cin, line))