print owner of process

master
denes 5 years ago
parent 17455f2084
commit 32131e3e55
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

@ -50,11 +50,14 @@ public:
case 'c': case 'c':
m_print_to_console = true; m_print_to_console = true;
break; break;
case 'f':
m_logFile = std::string(optarg);
break;
case 't': case 't':
m_timestamp = true; m_timestamp = true;
break; break;
case 'f': case 'u':
m_logFile = std::string(optarg); m_username = true;
break; break;
case 'h': case 'h':
printUsage(); printUsage();

@ -1,6 +1,10 @@
#ifndef LOGGER_HPP #ifndef LOGGER_HPP
#define LOGGER_HPP #define LOGGER_HPP
#include <sys/types.h>
#include <pwd.h>
#include <ctime> #include <ctime>
#include <ostream> #include <ostream>
@ -13,11 +17,16 @@ class Logger : public Singleton_DCLP<Logger>
public: public:
class Endl {}; 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 setStream(std::ostream* os) { m_os = os; }
void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; } void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; }
void setPrintUserName(bool b = true) { m_print_user_name = b; }
template <class T> template <class T>
Logger &operator<<(const T &x) { Logger &operator<<(const T &x) {
@ -42,12 +51,22 @@ private:
s.pop_back(); // removing trailing \n s.pop_back(); // removing trailing \n
*m_os << s << " "; *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 // ostreams are not copyable, hence storing a pointer only
std::ostream* m_os; std::ostream* m_os;
std::ostringstream m_buffer; std::ostringstream m_buffer;
bool m_print_time_stamp; bool m_print_time_stamp;
bool m_print_user_name;
}; };
#define LOG *Logger::getInstance() #define LOG *Logger::getInstance()

@ -26,6 +26,7 @@ int main(int argc, char* argv[])
} }
Logger::getInstance()->setPrintTimeStamp(argparser.printTimestamp()); Logger::getInstance()->setPrintTimeStamp(argparser.printTimestamp());
Logger::getInstance()->setPrintUserName(argparser.printUsername());
std::string line; std::string line;
while (std::getline(std::cin, line)) while (std::getline(std::cin, line))

Loading…
Cancel
Save