9 #include "messagefacility/MessageLogger/MessageLogger.h"
11 #include "fhiclcpp/ParameterSet.h"
12 #include "fhiclcpp/make_ParameterSet.h"
14 #include <boost/filesystem.hpp>
15 #include <boost/program_options.hpp>
17 using namespace boost;
18 namespace po = boost::program_options;
25 int main(
int ac,
char* av[])
28 std::string application;
34 std::vector<std::string> messages;
35 std::vector<std::string> vcat;
37 std::vector<std::string> vcat_def;
39 vcat_def.emplace_back(
"");
43 po::options_description cmdopt(
"Allowed options");
44 cmdopt.add_options()(
"help,h",
"display help message")(
"severity,s",
45 po::value<std::string>(&severity)->default_value(
"info"),
46 "severity of the message (error, warning, info, debug)")(
47 "category,g", po::value<std::vector<std::string>>(&vcat)->default_value(vcat_def,
"null"),
48 "message id / categories")(
"application,a",
49 po::value<std::string>(&application)->default_value(
"msgsenderApplication"),
50 "issuing application name")(
51 "config,c", po::value<std::string>(&conf)->default_value(
""),
"MessageFacility configuration file")(
52 "dump,d", po::bool_switch(&dump)->default_value(
false));
54 po::options_description hidden(
"Hidden options");
55 hidden.add_options()(
"message", po::value<std::vector<std::string>>(&messages),
"message text");
57 po::options_description desc;
58 desc.add(cmdopt).add(hidden);
60 po::positional_options_description p;
64 po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);
67 if (vm.count(
"help") != 0u)
69 std::cout <<
"Usage: msglogger [options] <message text>\n";
74 catch (std::exception& e)
76 std::cerr <<
"error: " << e.what() <<
"\n";
81 std::cerr <<
"Exception of unknown type!\n";
85 std::vector<std::string>::iterator it;
90 std::cout <<
"Message text is missing!\n";
91 std::cout <<
"Use \"msglogger --help\" for help messages\n";
95 if (application.empty())
97 std::cout <<
"Application name is missing!\n";
98 std::cout <<
"Message cannot be issued without specifying the application name.\n";
103 it = messages.begin();
104 while (it != messages.end())
106 message += *it +
" ";
111 transform(severity.begin(), severity.end(), severity.begin(), ::toupper);
112 if ((severity !=
"ERROR") && (severity !=
"WARNING") && (severity !=
"INFO") && (severity !=
"DEBUG"))
114 std::cerr <<
"Unknown severity level!\n";
120 while (it != vcat.end())
122 cat += *it + ((it == vcat.end() - 1) ?
"" :
"|");
127 fhicl::ParameterSet pset;
129 std::ifstream logfhicl(conf);
130 if (logfhicl.is_open())
132 std::stringstream fhiclstream;
133 fhiclstream << logfhicl.rdbuf();
134 std::string pstr(fhiclstream.str());
135 fhicl::make_ParameterSet(pstr, pset);
139 mf::StartMessageFacility(pset);
142 std::cout << pset.to_indented_string() << std::endl;
144 mf::SetApplicationName(application);
147 if (severity ==
"ERROR")
149 mf::LogError(cat) << message;
151 else if (severity ==
"WARNING")
153 mf::LogWarning(cat) << message;
155 else if (severity ==
"INFO")
157 mf::LogInfo(cat) << message;
159 else if (severity ==
"DEBUG")
161 mf::LogDebug(cat) << message;