Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 3 | |
| 4 | #include "config.hpp" |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 5 | #include "service.hpp" |
| 6 | #include "version.hpp" |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 7 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 8 | #include <getopt.h> |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 9 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 10 | /** @brief Print version info. */ |
| 11 | static void printVersion() |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 12 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 13 | puts("Host logger service rev." HOSTLOGGER_VERSION "."); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 14 | } |
| 15 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 16 | /** |
| 17 | * @brief Print help usage info. |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 18 | * |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 19 | * @param[in] app application's file name |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 20 | */ |
| 21 | static void printHelp(const char* app) |
| 22 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 23 | 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 Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 28 | } |
| 29 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 30 | /** @brief Application entry point. */ |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 31 | int main(int argc, char* argv[]) |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 32 | { |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 33 | // clang-format off |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 34 | const struct option longOpts[] = { |
| 35 | { "version", no_argument, nullptr, 'v' }, |
| 36 | { "help", no_argument, nullptr, 'h' }, |
| 37 | { nullptr, 0, nullptr, 0 } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 38 | }; |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 39 | // clang-format on |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 40 | 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 Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 44 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 45 | switch (val) |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 46 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 47 | case 'v': |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 48 | printVersion(); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 49 | return EXIT_SUCCESS; |
| 50 | case 'h': |
| 51 | printHelp(argv[0]); |
| 52 | return EXIT_SUCCESS; |
| 53 | default: |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 54 | fprintf(stderr, "Invalid argument: %s\n", argv[optind - 1]); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 55 | return EXIT_FAILURE; |
| 56 | } |
| 57 | } |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 58 | if (optind < argc) |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 59 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 60 | fprintf(stderr, "Unexpected argument: %s\n", argv[optind - 1]); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 61 | return EXIT_FAILURE; |
| 62 | } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 63 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 64 | 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 Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 73 | return EXIT_FAILURE; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 74 | } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 75 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame^] | 76 | return EXIT_SUCCESS; |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 77 | } |