blob: 756440ff6893a348436f8d2b65c5230217d589e8 [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
Nan Zhou042b5ba2021-06-18 09:32:45 -07004#include "buffer_service.hpp"
Artem Senichevefd5d742018-10-24 16:14:04 +03005#include "config.hpp"
Artem Senicheve8837d52020-06-07 11:59:04 +03006#include "service.hpp"
Nan Zhou042b5ba2021-06-18 09:32:45 -07007#include "stream_service.hpp"
Artem Senicheve8837d52020-06-07 11:59:04 +03008#include "version.hpp"
Patrick Venture4d5a5dc2018-11-14 08:51:13 -08009
Artem Senichevefd5d742018-10-24 16:14:04 +030010#include <getopt.h>
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080011
Nan Zhou042b5ba2021-06-18 09:32:45 -070012#include <phosphor-logging/log.hpp>
13
Artem Senicheve8837d52020-06-07 11:59:04 +030014/** @brief Print version info. */
15static void printVersion()
Artem Senichevefd5d742018-10-24 16:14:04 +030016{
Artem Senicheve8837d52020-06-07 11:59:04 +030017 puts("Host logger service rev." HOSTLOGGER_VERSION ".");
Artem Senichevefd5d742018-10-24 16:14:04 +030018}
19
Artem Senicheve8837d52020-06-07 11:59:04 +030020/**
21 * @brief Print help usage info.
Artem Senichevefd5d742018-10-24 16:14:04 +030022 *
Artem Senicheve8837d52020-06-07 11:59:04 +030023 * @param[in] app application's file name
Artem Senichevefd5d742018-10-24 16:14:04 +030024 */
25static void printHelp(const char* app)
26{
Artem Senicheve8837d52020-06-07 11:59:04 +030027 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 Senichevefd5d742018-10-24 16:14:04 +030032}
33
Artem Senichevefd5d742018-10-24 16:14:04 +030034/** @brief Application entry point. */
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080035int main(int argc, char* argv[])
Artem Senichevefd5d742018-10-24 16:14:04 +030036{
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080037 // clang-format off
Artem Senicheve8837d52020-06-07 11:59:04 +030038 const struct option longOpts[] = {
39 { "version", no_argument, nullptr, 'v' },
40 { "help", no_argument, nullptr, 'h' },
41 { nullptr, 0, nullptr, 0 }
Artem Senichevefd5d742018-10-24 16:14:04 +030042 };
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080043 // clang-format on
Artem Senicheve8837d52020-06-07 11:59:04 +030044 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 Venture4d5a5dc2018-11-14 08:51:13 -080048 {
Artem Senicheve8837d52020-06-07 11:59:04 +030049 switch (val)
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080050 {
Artem Senichevefd5d742018-10-24 16:14:04 +030051 case 'v':
Artem Senicheve8837d52020-06-07 11:59:04 +030052 printVersion();
Artem Senichevefd5d742018-10-24 16:14:04 +030053 return EXIT_SUCCESS;
54 case 'h':
55 printHelp(argv[0]);
56 return EXIT_SUCCESS;
57 default:
Artem Senicheve8837d52020-06-07 11:59:04 +030058 fprintf(stderr, "Invalid argument: %s\n", argv[optind - 1]);
Artem Senichevefd5d742018-10-24 16:14:04 +030059 return EXIT_FAILURE;
60 }
61 }
Artem Senicheve8837d52020-06-07 11:59:04 +030062 if (optind < argc)
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080063 {
Artem Senicheve8837d52020-06-07 11:59:04 +030064 fprintf(stderr, "Unexpected argument: %s\n", argv[optind - 1]);
Artem Senichevefd5d742018-10-24 16:14:04 +030065 return EXIT_FAILURE;
66 }
Artem Senichevefd5d742018-10-24 16:14:04 +030067
Artem Senicheve8837d52020-06-07 11:59:04 +030068 try
69 {
Nan Zhou042b5ba2021-06-18 09:32:45 -070070 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 Senicheve8837d52020-06-07 11:59:04 +030092 }
93 catch (const std::exception& ex)
94 {
95 fprintf(stderr, "%s\n", ex.what());
Artem Senichevefd5d742018-10-24 16:14:04 +030096 return EXIT_FAILURE;
Artem Senicheve8837d52020-06-07 11:59:04 +030097 }
Artem Senichevefd5d742018-10-24 16:14:04 +030098
Artem Senicheve8837d52020-06-07 11:59:04 +030099 return EXIT_SUCCESS;
Artem Senichevefd5d742018-10-24 16:14:04 +0300100}