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.

83 lines
1.6 KiB

5 years ago
#ifndef ARGPARSER_HPP
#define ARGPARSER_HPP
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
// #include <ostream>
// #include <utility> // move
// #include "Singleton_DCLP.hpp"
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 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