artdaq_mfextensions  v1_08_02
msgsender.cc
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 int main(int ac, char* av[])
25 {
26  std::string severity;
27  std::string application;
28  std::string message;
29  std::string cat;
30  std::string conf;
31  bool dump;
32 
33  std::vector<std::string> messages;
34  std::vector<std::string> vcat;
35 
36  std::vector<std::string> vcat_def;
37 
38  vcat_def.emplace_back("");
39 
40  try
41  {
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));
52 
53  po::options_description hidden("Hidden options");
54  hidden.add_options()("message", po::value<std::vector<std::string>>(&messages), "message text");
55 
56  po::options_description desc;
57  desc.add(cmdopt).add(hidden);
58 
59  po::positional_options_description p;
60  p.add("message", -1);
61 
62  po::variables_map vm;
63  po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);
64  po::notify(vm);
65 
66  if (vm.count("help") != 0u)
67  {
68  std::cout << "Usage: msglogger [options] <message text>\n";
69  std::cout << cmdopt;
70  return 0;
71  }
72  }
73  catch (std::exception& e)
74  {
75  std::cerr << "error: " << e.what() << "\n";
76  return 1;
77  }
78  catch (...)
79  {
80  std::cerr << "Exception of unknown type!\n";
81  return 1;
82  }
83 
84  std::vector<std::string>::iterator it;
85 
86  // must have message text
87  if (messages.empty())
88  {
89  std::cout << "Message text is missing!\n";
90  std::cout << "Use \"msglogger --help\" for help messages\n";
91  return 1;
92  }
93 
94  if (application.empty())
95  {
96  std::cout << "Application name is missing!\n";
97  std::cout << "Message cannot be issued without specifying the application name.\n";
98  return 1;
99  }
100 
101  // build message text string
102  it = messages.begin();
103  while (it != messages.end())
104  {
105  message += *it + " ";
106  ++it;
107  }
108 
109  // checking severity...
110  transform(severity.begin(), severity.end(), severity.begin(), ::toupper);
111  if ((severity != "ERROR") && (severity != "WARNING") && (severity != "INFO") && (severity != "DEBUG"))
112  {
113  std::cerr << "Unknown severity level!\n";
114  return 1;
115  }
116 
117  // checking categories..
118  it = vcat.begin();
119  while (it != vcat.end())
120  {
121  cat += *it + ((it == vcat.end() - 1) ? "" : "|");
122  ++it;
123  }
124 
125  // preparing parameterset for detinations...
126  fhicl::ParameterSet pset;
127 
128  std::ifstream logfhicl(conf);
129  if (logfhicl.is_open())
130  {
131  std::stringstream fhiclstream;
132  fhiclstream << logfhicl.rdbuf();
133  std::string pstr(fhiclstream.str());
134  pset = fhicl::ParameterSet::make(pstr);
135  }
136 
137  // start up message facility service
138  mf::StartMessageFacility(pset);
139  if (dump)
140  {
141  std::cout << pset.to_indented_string() << std::endl;
142  }
143  mf::SetApplicationName(application);
144 
145  // logging message...
146  if (severity == "ERROR")
147  {
148  mf::LogError(cat) << message;
149  }
150  else if (severity == "WARNING")
151  {
152  mf::LogWarning(cat) << message;
153  }
154  else if (severity == "INFO")
155  {
156  mf::LogInfo(cat) << message;
157  }
158  else if (severity == "DEBUG")
159  {
160  mf::LogDebug(cat) << message;
161  }
162 
163  return 0;
164 }