summaryrefslogtreecommitdiff
path: root/src/webstat_logger_main.cpp
blob: 7817dc007bba85759426cbf18e80a6e278f01fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "ingestor.hpp"
#include <boost/program_options.hpp>
#include <format>
#include <iostream>
#include <pq-connection.h>
#include <sys/utsname.h>

namespace {
	[[nodiscard]]
	utsname
	getHostDetail()
	{
		utsname uts {};
		if (uname(&uts)) {
			throw std::runtime_error(std::format("Failed to get hostname (uts: {}:{})", errno, strerror(errno)));
		}
		return uts;
	}
}

int
main(int argc, char ** argv)
{
	namespace po = boost::program_options;
	po::options_description opts("WebStat logger");

	std::string dbType;
	std::string dbConnStr;
	unsigned int dbMax = 4;
	unsigned int dbKeep = 2;

	// clang-format off
	opts.add_options()
		("help,h", "Show this help message")
		("config,c", po::value<std::string>(), "Read config from this config file")
		("db.type", po::value(&dbType)->default_value("postgresql"),
		 "Database connection type")
		("db.wr.connstr,D", po::value(&dbConnStr)->default_value("dbname=webstat user=webstat"),
		 "Database connection string (read/write)")
		("db.wr.max", po::value(&dbMax)->default_value(4),
		 "Maximum number of concurrent write/read write DB connections")
		("db.wr.keep", po::value(&dbKeep)->default_value(2),
		 "Number of write/read write DB connections to keep open")
		;
	// clang-format on
	po::variables_map optVars;
	po::store(po::command_line_parser(argc, argv).options(opts).run(), optVars);
	if (const auto & rcPath = optVars.find("config"); rcPath != optVars.end()) {
		po::store(po::parse_config_file(rcPath->second.as<std::string>().c_str(), opts), optVars);
	}

	// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
	if (optVars.contains("help")) {
		std::cout << opts << '\n';
		return EXIT_FAILURE;
	}
	po::notify(optVars);

	auto pool = std::make_shared<DB::ConnectionPool>(dbMax, dbKeep, std::move(dbType), std::move(dbConnStr));
	WebStat::Ingestor {getHostDetail(), pool}.ingestLog(stdin);
	return EXIT_SUCCESS;
}