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 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame^] | 4 | #include "buffer_service.hpp" |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 5 | #include "config.hpp" |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 6 | #include "service.hpp" |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame^] | 7 | #include "stream_service.hpp" |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 8 | #include "version.hpp" |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 9 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 10 | #include <getopt.h> |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 11 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame^] | 12 | #include <phosphor-logging/log.hpp> |
| 13 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 14 | /** @brief Print version info. */ |
| 15 | static void printVersion() |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 16 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 17 | puts("Host logger service rev." HOSTLOGGER_VERSION "."); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 18 | } |
| 19 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 20 | /** |
| 21 | * @brief Print help usage info. |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 22 | * |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 23 | * @param[in] app application's file name |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 24 | */ |
| 25 | static void printHelp(const char* app) |
| 26 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 27 | printVersion(); |
| 28 | puts("Copyright (c) 2020 YADRO."); |
| 29 | printf("Usage: %s [OPTION...]\n", app); |
| 30 | puts(" -v, --version Print version and exit"); |
| 31 | puts(" -h, --help Print this help and exit"); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 32 | } |
| 33 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 34 | /** @brief Application entry point. */ |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 35 | int main(int argc, char* argv[]) |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 36 | { |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 37 | // clang-format off |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 38 | const struct option longOpts[] = { |
| 39 | { "version", no_argument, nullptr, 'v' }, |
| 40 | { "help", no_argument, nullptr, 'h' }, |
| 41 | { nullptr, 0, nullptr, 0 } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 42 | }; |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 43 | // clang-format on |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 44 | const char* shortOpts = "vh"; |
| 45 | opterr = 0; // prevent native error messages |
| 46 | int val; |
| 47 | while ((val = getopt_long(argc, argv, shortOpts, longOpts, nullptr)) != -1) |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 48 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 49 | switch (val) |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 50 | { |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 51 | case 'v': |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 52 | printVersion(); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 53 | return EXIT_SUCCESS; |
| 54 | case 'h': |
| 55 | printHelp(argv[0]); |
| 56 | return EXIT_SUCCESS; |
| 57 | default: |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 58 | fprintf(stderr, "Invalid argument: %s\n", argv[optind - 1]); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 59 | return EXIT_FAILURE; |
| 60 | } |
| 61 | } |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 62 | if (optind < argc) |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame] | 63 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 64 | fprintf(stderr, "Unexpected argument: %s\n", argv[optind - 1]); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 65 | return EXIT_FAILURE; |
| 66 | } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 67 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 68 | try |
| 69 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame^] | 70 | Config config; |
| 71 | DbusLoop dbus_loop; |
| 72 | HostConsole host_console(config.socketId); |
| 73 | using phosphor::logging::level; |
| 74 | using phosphor::logging::log; |
| 75 | if (config.mode == Mode::streamMode) |
| 76 | { |
| 77 | log<level::INFO>("HostLogger is in stream mode."); |
| 78 | StreamService service(config.streamDestination, dbus_loop, |
| 79 | host_console); |
| 80 | service.run(); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | log<level::INFO>("HostLogger is in buffer mode."); |
| 85 | LogBuffer logBuffer(config.bufMaxSize, config.bufMaxTime); |
| 86 | FileStorage fileStorage(config.outDir, config.socketId, |
| 87 | config.maxFiles); |
| 88 | BufferService service(config, dbus_loop, host_console, logBuffer, |
| 89 | fileStorage); |
| 90 | service.run(); |
| 91 | } |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 92 | } |
| 93 | catch (const std::exception& ex) |
| 94 | { |
| 95 | fprintf(stderr, "%s\n", ex.what()); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 96 | return EXIT_FAILURE; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 97 | } |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 98 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 99 | return EXIT_SUCCESS; |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 100 | } |