You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
#ifndef ARGPARSER_HPP
|
|
|
|
#define ARGPARSER_HPP
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
class ArgParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ArgParser() : m_use_console(false), m_logFile() {}
|
|
|
|
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"
|
|
|
|
<< " -h print this help\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "Option -c and -f are mutually exclusive.\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "Denes Matetelki <denes@matetelki.com>\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:h")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
m_use_console = true;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
m_logFile = std::string(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
printUsage();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
default:
|
|
|
|
printUsage(false);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((m_use_console && !m_logFile.empty()) ||
|
|
|
|
(!m_use_console && m_logFile.empty())) {
|
|
|
|
printUsage(false);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useConsole() const { return m_use_console; }
|
|
|
|
std::string logFile() const { return m_logFile; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_argc;
|
|
|
|
char** m_argv;
|
|
|
|
|
|
|
|
bool m_use_console;
|
|
|
|
std::string m_logFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LOG *Logger::getInstance()->getStream()
|
|
|
|
|
|
|
|
#endif // ARGPARSER_HPP
|