1 #include <CLI/CLI.hpp>
2 #include <iostream>
3 #include <phosphor-logging/log.hpp>
4 #include <sdbusplus/bus.hpp>
5 #include <sdeventplus/event.hpp>
6 #include <systemd_target_parser.hpp>
7 #include <vector>
8 
9 using phosphor::logging::level;
10 using phosphor::logging::log;
11 
12 bool gVerbose = false;
13 
14 void dump_targets(const TargetErrorData& targetData)
15 {
16     std::cout << "## Data Structure of Json ##" << std::endl;
17     for (const auto& [target, value] : targetData)
18     {
19         std::cout << target << " " << value.errorToLog << std::endl;
20         std::cout << "    ";
21         for (auto& eToMonitor : value.errorsToMonitor)
22         {
23             std::cout << eToMonitor << ", ";
24         }
25         std::cout << std::endl;
26     }
27     std::cout << std::endl;
28 }
29 
30 void print_usage(void)
31 {
32     std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with "
33                  "target/error mappings"
34               << std::endl;
35     return;
36 }
37 
38 int main(int argc, char* argv[])
39 {
40     auto bus = sdbusplus::bus::new_default();
41     auto event = sdeventplus::Event::get_default();
42     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
43     std::vector<std::string> filePaths;
44 
45     CLI::App app{"OpenBmc systemd target monitor"};
46     app.add_option("-f,--file", filePaths,
47                    "Full path to json file(s) with target/error mappings");
48     app.add_flag("-v", gVerbose, "Enable verbose output");
49 
50     CLI11_PARSE(app, argc, argv);
51 
52     if (filePaths.empty())
53     {
54         log<level::ERR>("No input files");
55         print_usage();
56         exit(-1);
57     }
58 
59     TargetErrorData targetData = parseFiles(filePaths);
60 
61     if (targetData.size() == 0)
62     {
63         log<level::ERR>("Invalid input files, no targets found");
64         print_usage();
65         exit(-1);
66     }
67 
68     if (gVerbose)
69     {
70         dump_targets(targetData);
71     }
72 
73     // TODO - Begin monitoring for systemd unit changes and logging appropriate
74     //        errors
75 
76     return event.loop();
77 }
78