prefix with time

master
denes 5 years ago
parent 6e6d2289c1
commit 09a543d9f5
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

@ -9,7 +9,12 @@
class ArgParser class ArgParser
{ {
public: 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) void printUsage(bool cout = true)
{ {
std::ostringstream oss; std::ostringstream oss;
@ -18,6 +23,7 @@ public:
<< "Logs the input (till EOF) to the console or to a file.\n" << "Logs the input (till EOF) to the console or to a file.\n"
<< "\n" << "\n"
<< "Options:\n" << "Options:\n"
<< " -t, prefix lines with timestamp\n"
<< " -c, log to console\n" << " -c, log to console\n"
<< " -f FILE log to FILE\n" << " -f FILE log to FILE\n"
<< " -h print this help\n" << " -h print this help\n"
@ -31,13 +37,17 @@ public:
else else
std::cerr << oss.str(); std::cerr << oss.str();
} }
int parse(int argc, char* argv[]) int parse(int argc, char* argv[])
{ {
int c; int c;
while ((c = getopt (argc, argv, "cf:h")) != -1) { while ((c = getopt (argc, argv, "ctf:h")) != -1) {
switch (c) { switch (c) {
case 'c': case 'c':
m_use_console = true; m_print_to_console = true;
break;
case 't':
m_print_timestamp = true;
break; break;
case 'f': case 'f':
m_logFile = std::string(optarg); m_logFile = std::string(optarg);
@ -50,22 +60,24 @@ public:
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
if ((m_use_console && !m_logFile.empty()) || if ((m_print_to_console && !m_logFile.empty()) ||
(!m_use_console && m_logFile.empty())) { (!m_print_to_console && m_logFile.empty())) {
printUsage(false); printUsage(false);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return -1; 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; } std::string logFile() const { return m_logFile; }
private: private:
int m_argc; int m_argc;
char** m_argv; char** m_argv;
bool m_use_console; bool m_print_to_console;
bool m_print_timestamp;
std::string m_logFile; std::string m_logFile;
}; };

@ -4,4 +4,4 @@ project (CppLogger)
# Using Catch2's benchmarking tools # Using Catch2's benchmarking tools
set(CATCH_HEADER_FILES Logger.hpp Singleton_DCLP.hpp ArgParser.hpp) set(CATCH_HEADER_FILES Logger.hpp Singleton_DCLP.hpp ArgParser.hpp)
add_executable(cpplogger main.cpp ${HEADER_FILES}) add_executable(cpplogger main.cpp ${HEADER_FILES})
set_target_properties(cpplogger PROPERTIES COMPILE_FLAGS "-ggdb")

@ -2,18 +2,34 @@
#define LOGGER_HPP #define LOGGER_HPP
#include <ostream> #include <ostream>
#include <utility> // move #include <ctime>
#include "Singleton_DCLP.hpp" #include "Singleton_DCLP.hpp"
class Logger : public Singleton_DCLP<Logger> class Logger : public Singleton_DCLP<Logger>
{ {
public: public:
Logger() : m_os(nullptr), m_print_time_stamp(false) {}
void setStream(std::ostream* os) { m_os = os; } 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: private:
// 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;
bool m_print_time_stamp;
}; };
#define LOG *Logger::getInstance()->getStream() #define LOG *Logger::getInstance()->getStream()

@ -12,7 +12,7 @@ int main(int argc, char* argv[])
if (r != -1) return r; if (r != -1) return r;
std::ofstream logfile; std::ofstream logfile;
if (argparser.useConsole()) { if (argparser.printToConsole()) {
Logger::getInstance()->setStream(&std::cout); Logger::getInstance()->setStream(&std::cout);
} else { } else {
logfile.open (argparser.logFile(), logfile.open (argparser.logFile(),
@ -25,11 +25,13 @@ int main(int argc, char* argv[])
Logger::getInstance()->setStream(&logfile); Logger::getInstance()->setStream(&logfile);
} }
Logger::getInstance()->setPrintTimeStamp(argparser.printTimestamp());
std::string line; std::string line;
while (std::getline(std::cin, line)) while (std::getline(std::cin, line))
LOG << line << std::endl; LOG << line << std::endl;
if (!argparser.useConsole()) if (!argparser.printToConsole())
logfile.close(); logfile.close();
return EXIT_SUCCESS; return EXIT_SUCCESS;

Loading…
Cancel
Save