artdaq_mfextensions  v1_02_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/make_ParameterSet.h"
12 #include "fhiclcpp/ParameterSet.h"
13 
14 #include <boost/program_options.hpp>
15 #include <boost/filesystem.hpp>
16 
17 using namespace boost;
18 namespace BFS = boost::filesystem;
19 namespace po = boost::program_options;
20 
21 #include <iostream>
22 #include <algorithm>
23 #include <iterator>
24 #include <fstream>
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()
46  ("help,h", "display help message")
47  ("severity,s",
48  po::value<std::string>(&severity)->default_value("info"),
49  "severity of the message (error, warning, info, debug)")
50  ("category,g",
51  po::value<std::vector<std::string>>(&vcat)->default_value(vcat_def, "null"),
52  "message id / categories")
53  ("application,a",
54  po::value<std::string>(&application)->default_value("msgsenderApplication"),
55  "issuing application name")
56  ("config,c",
57  po::value<std::string>(&conf)->default_value(""),
58  "MessageFacility configuration file")
59  ("dump,d", po::bool_switch(&dump)->default_value(false));
60 
61  po::options_description hidden("Hidden options");
62  hidden.add_options()
63  ("message", po::value<std::vector<std::string>>(&messages), "message text");
64 
65  po::options_description desc;
66  desc.add(cmdopt).add(hidden);
67 
68  po::positional_options_description p;
69  p.add("message", -1);
70 
71  po::variables_map vm;
72  po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);
73  po::notify(vm);
74 
75  if (vm.count("help"))
76  {
77  std::cout << "Usage: msglogger [options] <message text>\n";
78  std::cout << cmdopt;
79  return 0;
80  }
81  }
82  catch (std::exception& e)
83  {
84  std::cerr << "error: " << e.what() << "\n";
85  return 1;
86  }
87  catch (...)
88  {
89  std::cerr << "Exception of unknown type!\n";
90  return 1;
91  }
92 
93  std::vector<std::string>::iterator it;
94 
95  // must have message text
96  if (messages.size() == 0)
97  {
98  std::cout << "Message text is missing!\n";
99  std::cout << "Use \"msglogger --help\" for help messages\n";
100  return 1;
101  }
102 
103  if (application.empty())
104  {
105  std::cout << "Application name is missing!\n";
106  std::cout << "Message cannot be issued without specifying the application name.\n";
107  return 1;
108  }
109 
110  // build message text string
111  it = messages.begin();
112  while (it != messages.end())
113  {
114  message += *it + " ";
115  ++it;
116  }
117 
118  // checking severity...
119  transform(severity.begin(), severity.end(), severity.begin(), ::toupper);
120  if ((severity != "ERROR") && (severity != "WARNING")
121  && (severity != "INFO") && (severity != "DEBUG"))
122  {
123  std::cerr << "Unknown severity level!\n";
124  return 1;
125  }
126 
127  // checking categories..
128  it = vcat.begin();
129  while (it != vcat.end())
130  {
131  cat += *it + ((it == vcat.end() - 1) ? "" : "|");
132  ++it;
133  }
134 
135  // preparing parameterset for detinations...
136  fhicl::ParameterSet pset;
137 
138  std::ifstream logfhicl(conf);
139  if (logfhicl.is_open())
140  {
141  std::stringstream fhiclstream;
142  fhiclstream << logfhicl.rdbuf();
143  std::string pstr(fhiclstream.str());
144  fhicl::make_ParameterSet(pstr, pset);
145  }
146 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // an indication of a switch from s48 to s50
147  else
148  {
149  pset = mf::MessageFacilityService::logConsole();
150  }
151 # endif
152 
153  // start up message facility service
154 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
155  mf::StartMessageFacility(pset);
156 # else
157  mf::StartMessageFacility(mf::MessageFacilityService::MultiThread, pset);
158 # endif
159  if (dump)
160  {
161  std::cout << pset.to_indented_string() << std::endl;
162  }
163 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // an indication of a switch from s48 to s50
164  mf::SetModuleName("msgsenderModule");
165  mf::SetContext("msgsenderContext");
166 # endif
167  mf::SetApplicationName(application);
168 
169  // logging message...
170  if (severity == "ERROR")
171  mf::LogError(cat) << message;
172  else if (severity == "WARNING")
173  mf::LogWarning(cat) << message;
174  else if (severity == "INFO")
175  mf::LogInfo(cat) << message;
176  else if (severity == "DEBUG")
177  mf::LogDebug(cat) << message;
178 
179 
180  return 0;
181 }