denes 5 years ago
parent 09a543d9f5
commit 98cc8b0b65
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

@ -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 <denes@matetelki.com>\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

@ -9,10 +9,29 @@
class Logger : public Singleton_DCLP<Logger>
{
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 <class T>
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

@ -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();

Loading…
Cancel
Save