blob: cb8d2bc48184b7286585cd286fa5b48debbc3d20 [file] [log] [blame]
Artem Senicheve8837d52020-06-07 11:59:04 +03001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2020 YADRO
Artem Senichevefd5d742018-10-24 16:14:04 +03003
4#include "config.hpp"
Artem Senicheve8837d52020-06-07 11:59:04 +03005#include "service.hpp"
6#include "version.hpp"
Patrick Venture4d5a5dc2018-11-14 08:51:13 -08007
Artem Senichevefd5d742018-10-24 16:14:04 +03008#include <getopt.h>
Patrick Venture4d5a5dc2018-11-14 08:51:13 -08009
Artem Senicheve8837d52020-06-07 11:59:04 +030010/** @brief Print version info. */
11static void printVersion()
Artem Senichevefd5d742018-10-24 16:14:04 +030012{
Artem Senicheve8837d52020-06-07 11:59:04 +030013 puts("Host logger service rev." HOSTLOGGER_VERSION ".");
Artem Senichevefd5d742018-10-24 16:14:04 +030014}
15
Artem Senicheve8837d52020-06-07 11:59:04 +030016/**
17 * @brief Print help usage info.
Artem Senichevefd5d742018-10-24 16:14:04 +030018 *
Artem Senicheve8837d52020-06-07 11:59:04 +030019 * @param[in] app application's file name
Artem Senichevefd5d742018-10-24 16:14:04 +030020 */
21static void printHelp(const char* app)
22{
Artem Senicheve8837d52020-06-07 11:59:04 +030023 printVersion();
24 puts("Copyright (c) 2020 YADRO.");
25 printf("Usage: %s [OPTION...]\n", app);
26 puts(" -v, --version Print version and exit");
27 puts(" -h, --help Print this help and exit");
Artem Senichevefd5d742018-10-24 16:14:04 +030028}
29
Artem Senichevefd5d742018-10-24 16:14:04 +030030/** @brief Application entry point. */
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080031int main(int argc, char* argv[])
Artem Senichevefd5d742018-10-24 16:14:04 +030032{
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080033 // clang-format off
Artem Senicheve8837d52020-06-07 11:59:04 +030034 const struct option longOpts[] = {
35 { "version", no_argument, nullptr, 'v' },
36 { "help", no_argument, nullptr, 'h' },
37 { nullptr, 0, nullptr, 0 }
Artem Senichevefd5d742018-10-24 16:14:04 +030038 };
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080039 // clang-format on
Artem Senicheve8837d52020-06-07 11:59:04 +030040 const char* shortOpts = "vh";
41 opterr = 0; // prevent native error messages
42 int val;
43 while ((val = getopt_long(argc, argv, shortOpts, longOpts, nullptr)) != -1)
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080044 {
Artem Senicheve8837d52020-06-07 11:59:04 +030045 switch (val)
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080046 {
Artem Senichevefd5d742018-10-24 16:14:04 +030047 case 'v':
Artem Senicheve8837d52020-06-07 11:59:04 +030048 printVersion();
Artem Senichevefd5d742018-10-24 16:14:04 +030049 return EXIT_SUCCESS;
50 case 'h':
51 printHelp(argv[0]);
52 return EXIT_SUCCESS;
53 default:
Artem Senicheve8837d52020-06-07 11:59:04 +030054 fprintf(stderr, "Invalid argument: %s\n", argv[optind - 1]);
Artem Senichevefd5d742018-10-24 16:14:04 +030055 return EXIT_FAILURE;
56 }
57 }
Artem Senicheve8837d52020-06-07 11:59:04 +030058 if (optind < argc)
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080059 {
Artem Senicheve8837d52020-06-07 11:59:04 +030060 fprintf(stderr, "Unexpected argument: %s\n", argv[optind - 1]);
Artem Senichevefd5d742018-10-24 16:14:04 +030061 return EXIT_FAILURE;
62 }
Artem Senichevefd5d742018-10-24 16:14:04 +030063
Artem Senicheve8837d52020-06-07 11:59:04 +030064 try
65 {
66 Config cfg;
67 Service svc(cfg);
68 svc.run();
69 }
70 catch (const std::exception& ex)
71 {
72 fprintf(stderr, "%s\n", ex.what());
Artem Senichevefd5d742018-10-24 16:14:04 +030073 return EXIT_FAILURE;
Artem Senicheve8837d52020-06-07 11:59:04 +030074 }
Artem Senichevefd5d742018-10-24 16:14:04 +030075
Artem Senicheve8837d52020-06-07 11:59:04 +030076 return EXIT_SUCCESS;
Artem Senichevefd5d742018-10-24 16:14:04 +030077}