logiops/src/logid/logid.cpp

180 lines
5.1 KiB
C++
Raw Normal View History

#include <algorithm>
#include <cstdio>
#include <cstdlib>
2020-04-13 02:25:39 +00:00
#include <mutex>
#include "util.h"
#include "DeviceMonitor.h"
2020-04-13 02:25:39 +00:00
#include "logid.h"
#define evdev_name "logid"
2019-08-09 18:25:40 +00:00
#define DEFAULT_CONFIG_FILE "/etc/logid.cfg"
2020-04-11 00:53:13 +00:00
#ifndef LOGIOPS_VERSION
#define LOGIOPS_VERSION "null"
#warning Version is undefined!
#endif
2019-10-05 01:56:17 +00:00
using namespace logid;
2020-04-12 23:44:12 +00:00
std::string config_file = DEFAULT_CONFIG_FILE;
2019-10-05 01:56:17 +00:00
LogLevel logid::global_verbosity = INFO;
// Configuration* logid::global_config;
DeviceMonitor* logid::finder;
2020-04-13 02:25:39 +00:00
bool logid::kill_logid = false;
std::mutex logid::finder_reloading;
2019-08-09 02:13:50 +00:00
enum class Option
{
None,
Verbose,
Config,
2020-04-11 00:53:13 +00:00
Help,
Version
2019-08-09 02:13:50 +00:00
};
/*
2020-04-13 02:25:39 +00:00
void logid::reload()
{
log_printf(INFO, "Reloading logid...");
finder_reloading.lock();
finder->stop();
Configuration* old_config = global_config;
global_config = new Configuration(config_file.c_str());
delete(old_config);
delete(finder);
finder = new DeviceMonitor();
2020-04-13 02:25:39 +00:00
finder_reloading.unlock();
}
*/
2020-04-13 02:25:39 +00:00
2020-04-12 23:44:12 +00:00
void read_cli_options(int argc, char** argv)
{
2019-08-09 02:13:50 +00:00
for(int i = 1; i < argc; i++)
{
Option option = Option::None;
if(argv[i][0] == '-') // This argument is an option
2019-08-09 02:13:50 +00:00
{
switch(argv[i][1]) // Set option
2019-08-09 02:13:50 +00:00
{
case '-': // Full option name
{
std::string op_str = argv[i];
if (op_str == "--verbose") option = Option::Verbose;
if (op_str == "--config") option = Option::Config;
if (op_str == "--help") option = Option::Help;
2020-04-11 00:53:13 +00:00
if (op_str == "--version") option = Option::Version;
2019-08-09 02:13:50 +00:00
break;
}
case 'v': // Verbosity
option = Option::Verbose;
break;
2020-04-11 00:53:13 +00:00
case 'V': //Version
option = Option::Version;
break;
2019-08-09 02:13:50 +00:00
case 'c': // Config file path
option = Option::Config;
break;
case 'h': // Help
option = Option::Help;
break;
default:
log_printf(WARN, "%s is not a valid option, ignoring.", argv[1]);
}
switch(option)
{
case Option::Verbose:
{
if (++i >= argc)
{
global_verbosity = DEBUG; // Assume debug verbosity
break;
}
std::string loglevel = argv[i];
2019-10-06 01:27:17 +00:00
try { global_verbosity = stringToLogLevel(argv[i]); }
2019-08-09 02:13:50 +00:00
catch (std::invalid_argument &e)
{
if (argv[i][0] == '-')
{
2019-08-09 02:13:50 +00:00
global_verbosity = DEBUG; // Assume debug verbosity
i--; // Go back to last argument to continue loop.
}
2019-08-09 02:13:50 +00:00
else
{
log_printf(WARN, e.what());
printf("Valid verbosity levels are: Debug, Info, Warn/Warning, or Error.\n");
2020-04-12 23:44:12 +00:00
exit(EXIT_FAILURE);
2019-08-09 02:13:50 +00:00
}
}
break;
}
case Option::Config:
{
if (++i >= argc)
{
log_printf(ERROR, "Config file is not specified.");
2020-04-12 23:44:12 +00:00
exit(EXIT_FAILURE);
2019-08-09 02:13:50 +00:00
}
config_file = argv[i];
break;
}
case Option::Help:
2020-04-11 00:53:13 +00:00
{
printf(R"(logid version %s
Usage: %s [options]
2019-08-09 02:13:50 +00:00
Possible options are:
-v,--verbose [level] Set log level to debug/info/warn/error (leave blank for debug)
2020-04-11 00:53:13 +00:00
-V,--version Print version number
2019-08-09 02:13:50 +00:00
-c,--config [file path] Change config file from default at %s
-h,--help Print this message.
2020-04-11 00:53:13 +00:00
)", LOGIOPS_VERSION, argv[0], DEFAULT_CONFIG_FILE);
2019-08-09 02:13:50 +00:00
2020-04-12 23:44:12 +00:00
exit(EXIT_SUCCESS);
2020-04-11 00:53:13 +00:00
}
case Option::Version:
{
printf("%s\n", LOGIOPS_VERSION);
2020-04-12 23:44:12 +00:00
exit(EXIT_SUCCESS);
2020-04-11 00:53:13 +00:00
}
2019-08-09 02:13:50 +00:00
case Option::None:
break;
}
}
}
2020-04-12 23:44:12 +00:00
}
int main(int argc, char** argv)
{
read_cli_options(argc, argv);
2019-08-09 02:13:50 +00:00
/*
// Read config
2019-08-09 18:25:40 +00:00
try { global_config = new Configuration(config_file.c_str()); }
catch (std::exception &e) { global_config = new Configuration(); }
//Create an evdev device called 'logid'
try { global_evdev = new EvdevDevice(evdev_name); }
catch(std::system_error& e)
{
log_printf(ERROR, "Could not create evdev device: %s", e.what());
return EXIT_FAILURE;
}
*/
// Scan devices, create listeners, handlers, etc.
finder = new DeviceMonitor();
2020-04-13 02:25:39 +00:00
while(!kill_logid)
{
finder_reloading.lock();
finder_reloading.unlock();
finder->run();
}
return EXIT_SUCCESS;
2020-04-13 02:25:39 +00:00
}