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 BFS = boost::filesystem;
19 namespace po = boost::program_options;
26 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.push_back(
"");
42 po::options_description cmdopt(
"Allowed options");
43 cmdopt.add_options()(
"help,h",
"display help message")(
"severity,s",
44 po::value<std::string>(&severity)->default_value(
"info"),
45 "severity of the message (error, warning, info, debug)")(
46 "category,g", po::value<std::vector<std::string>>(&vcat)->default_value(vcat_def,
"null"),
47 "message id / categories")(
"application,a",
48 po::value<std::string>(&application)->default_value(
"msgsenderApplication"),
49 "issuing application name")(
50 "config,c", po::value<std::string>(&conf)->default_value(
""),
"MessageFacility configuration file")(
51 "dump,d", po::bool_switch(&dump)->default_value(
false));
53 po::options_description hidden(
"Hidden options");
54 hidden.add_options()(
"message", po::value<std::vector<std::string>>(&messages),
"message text");
56 po::options_description desc;
57 desc.add(cmdopt).add(hidden);
59 po::positional_options_description p;
63 po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);
66 if (vm.count(
"help")) {
67 std::cout <<
"Usage: msglogger [options] <message text>\n";
71 }
catch (std::exception& e) {
72 std::cerr <<
"error: " << e.what() <<
"\n";
75 std::cerr <<
"Exception of unknown type!\n";
79 std::vector<std::string>::iterator it;
82 if (messages.size() == 0) {
83 std::cout <<
"Message text is missing!\n";
84 std::cout <<
"Use \"msglogger --help\" for help messages\n";
88 if (application.empty()) {
89 std::cout <<
"Application name is missing!\n";
90 std::cout <<
"Message cannot be issued without specifying the application name.\n";
95 it = messages.begin();
96 while (it != messages.end()) {
102 transform(severity.begin(), severity.end(), severity.begin(), ::toupper);
103 if ((severity !=
"ERROR") && (severity !=
"WARNING") && (severity !=
"INFO") && (severity !=
"DEBUG")) {
104 std::cerr <<
"Unknown severity level!\n";
110 while (it != vcat.end()) {
111 cat += *it + ((it == vcat.end() - 1) ?
"" :
"|");
116 fhicl::ParameterSet pset;
118 std::ifstream logfhicl(conf);
119 if (logfhicl.is_open()) {
120 std::stringstream fhiclstream;
121 fhiclstream << logfhicl.rdbuf();
122 std::string pstr(fhiclstream.str());
123 fhicl::make_ParameterSet(pstr, pset);
127 mf::StartMessageFacility(pset);
129 std::cout << pset.to_indented_string() << std::endl;
131 mf::SetApplicationName(application);
134 if (severity ==
"ERROR")
135 mf::LogError(cat) << message;
136 else if (severity ==
"WARNING")
137 mf::LogWarning(cat) << message;
138 else if (severity ==
"INFO")
139 mf::LogInfo(cat) << message;
140 else if (severity ==
"DEBUG")
141 mf::LogDebug(cat) << message;