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