Line data Source code
1 : //
2 : // msgLogger.cc
3 : // ------------------------------------------------------------------
4 : // Command line appication to send a message through MessageFacility.
5 : //
6 : // ql 03/01/2010
7 : //
8 :
9 : #include "messagefacility/MessageLogger/MessageLogger.h"
10 :
11 : #include "fhiclcpp/ParameterSet.h"
12 :
13 : #include <boost/filesystem.hpp>
14 : #include <boost/program_options.hpp>
15 :
16 : using namespace boost;
17 : namespace po = boost::program_options;
18 :
19 : #include <algorithm>
20 : #include <fstream>
21 : #include <iostream>
22 : #include <iterator>
23 :
24 0 : int main(int ac, char* av[])
25 : {
26 0 : std::string severity;
27 0 : std::string application;
28 0 : std::string message;
29 0 : std::string cat;
30 0 : std::string conf;
31 : bool dump;
32 :
33 0 : std::vector<std::string> messages;
34 0 : std::vector<std::string> vcat;
35 :
36 0 : std::vector<std::string> vcat_def;
37 :
38 0 : vcat_def.emplace_back("");
39 :
40 : try
41 : {
42 0 : po::options_description cmdopt("Allowed options");
43 0 : cmdopt.add_options()("help,h", "display help message")("severity,s",
44 0 : po::value<std::string>(&severity)->default_value("info"),
45 0 : "severity of the message (error, warning, info, debug)")(
46 0 : "category,g", po::value<std::vector<std::string>>(&vcat)->default_value(vcat_def, "null"),
47 0 : "message id / categories")("application,a",
48 0 : po::value<std::string>(&application)->default_value("msgsenderApplication"),
49 0 : "issuing application name")(
50 0 : "config,c", po::value<std::string>(&conf)->default_value(""), "MessageFacility configuration file")(
51 0 : "dump,d", po::bool_switch(&dump)->default_value(false));
52 :
53 0 : po::options_description hidden("Hidden options");
54 0 : hidden.add_options()("message", po::value<std::vector<std::string>>(&messages), "message text");
55 :
56 0 : po::options_description desc;
57 0 : desc.add(cmdopt).add(hidden);
58 :
59 0 : po::positional_options_description p;
60 0 : p.add("message", -1);
61 :
62 0 : po::variables_map vm;
63 0 : po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);
64 0 : po::notify(vm);
65 :
66 0 : if (vm.count("help") != 0u)
67 : {
68 0 : std::cout << "Usage: msglogger [options] <message text>\n";
69 0 : std::cout << cmdopt;
70 0 : return 0;
71 : }
72 0 : }
73 0 : catch (std::exception& e)
74 : {
75 0 : std::cerr << "error: " << e.what() << "\n";
76 0 : return 1;
77 0 : }
78 0 : catch (...)
79 : {
80 0 : std::cerr << "Exception of unknown type!\n";
81 0 : return 1;
82 0 : }
83 :
84 0 : std::vector<std::string>::iterator it;
85 :
86 : // must have message text
87 0 : if (messages.empty())
88 : {
89 0 : std::cout << "Message text is missing!\n";
90 0 : std::cout << "Use \"msglogger --help\" for help messages\n";
91 0 : return 1;
92 : }
93 :
94 0 : if (application.empty())
95 : {
96 0 : std::cout << "Application name is missing!\n";
97 0 : std::cout << "Message cannot be issued without specifying the application name.\n";
98 0 : return 1;
99 : }
100 :
101 : // build message text string
102 0 : it = messages.begin();
103 0 : while (it != messages.end())
104 : {
105 0 : message += *it + " ";
106 0 : ++it;
107 : }
108 :
109 : // checking severity...
110 0 : transform(severity.begin(), severity.end(), severity.begin(), ::toupper);
111 0 : if ((severity != "ERROR") && (severity != "WARNING") && (severity != "INFO") && (severity != "DEBUG"))
112 : {
113 0 : std::cerr << "Unknown severity level!\n";
114 0 : return 1;
115 : }
116 :
117 : // checking categories..
118 0 : it = vcat.begin();
119 0 : while (it != vcat.end())
120 : {
121 0 : cat += *it + ((it == vcat.end() - 1) ? "" : "|");
122 0 : ++it;
123 : }
124 :
125 : // preparing parameterset for detinations...
126 0 : fhicl::ParameterSet pset;
127 :
128 0 : std::ifstream logfhicl(conf);
129 0 : if (logfhicl.is_open())
130 : {
131 0 : std::stringstream fhiclstream;
132 0 : fhiclstream << logfhicl.rdbuf();
133 0 : std::string pstr(fhiclstream.str());
134 0 : pset = fhicl::ParameterSet::make(pstr);
135 0 : }
136 :
137 : // start up message facility service
138 0 : mf::StartMessageFacility(pset);
139 0 : if (dump)
140 : {
141 0 : std::cout << pset.to_indented_string() << std::endl;
142 : }
143 0 : mf::SetApplicationName(application);
144 :
145 : // logging message...
146 0 : if (severity == "ERROR")
147 : {
148 0 : mf::LogError(cat) << message;
149 : }
150 0 : else if (severity == "WARNING")
151 : {
152 0 : mf::LogWarning(cat) << message;
153 : }
154 0 : else if (severity == "INFO")
155 : {
156 0 : mf::LogInfo(cat) << message;
157 : }
158 0 : else if (severity == "DEBUG")
159 : {
160 0 : mf::LogDebug(cat) << message;
161 : }
162 :
163 0 : return 0;
164 0 : }
|