#ifndef ARGPARSER_HPP #define ARGPARSER_HPP #include #include #include class ArgParser { public: ArgParser() : m_print_to_console(false) , m_logFile() , m_timestamp(false) , m_hostname(false) , m_username(false) {} void printUsage(bool cout = true) { std::ostringstream oss; oss << "Usage: cpplogger [OPTION]\n" << "Logs the input (till EOF) to the console or to a file.\n" << "\n" << "Options:\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, specify one.\n" << "\n" << "Homepage: \n" << "Author: Denes Matetelki \n"; if (cout) std::cout << oss.str(); else std::cerr << oss.str(); } int parse(int argc, char* argv[]) { int c; while ((c = getopt (argc, argv, "cf:tnuh")) != -1) { switch (c) { case 'c': m_print_to_console = true; break; case 'f': m_logFile = std::string(optarg); break; case 't': m_timestamp = true; break; case 'u': m_username = true; break; case 'h': printUsage(); return EXIT_SUCCESS; default: printUsage(false); return EXIT_FAILURE; } } if ((m_print_to_console && !m_logFile.empty()) || (!m_print_to_console && m_logFile.empty())) { printUsage(false); return EXIT_FAILURE; } return -1; } bool printToConsole() const { return m_print_to_console; } 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: int m_argc; char** m_argv; bool m_print_to_console; std::string m_logFile; bool m_timestamp; bool m_hostname; bool m_username; }; // #define LOG *Logger::getInstance()->getStream() #endif // ARGPARSER_HPP