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