artdaq_mfextensions  v1_03_03a
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  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.push_back("");
40 
41  try {
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")) {
67  std::cout << "Usage: msglogger [options] <message text>\n";
68  std::cout << cmdopt;
69  return 0;
70  }
71  } catch (std::exception& e) {
72  std::cerr << "error: " << e.what() << "\n";
73  return 1;
74  } catch (...) {
75  std::cerr << "Exception of unknown type!\n";
76  return 1;
77  }
78 
79  std::vector<std::string>::iterator it;
80 
81  // must have message text
82  if (messages.size() == 0) {
83  std::cout << "Message text is missing!\n";
84  std::cout << "Use \"msglogger --help\" for help messages\n";
85  return 1;
86  }
87 
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";
91  return 1;
92  }
93 
94  // build message text string
95  it = messages.begin();
96  while (it != messages.end()) {
97  message += *it + " ";
98  ++it;
99  }
100 
101  // checking severity...
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";
105  return 1;
106  }
107 
108  // checking categories..
109  it = vcat.begin();
110  while (it != vcat.end()) {
111  cat += *it + ((it == vcat.end() - 1) ? "" : "|");
112  ++it;
113  }
114 
115  // preparing parameterset for detinations...
116  fhicl::ParameterSet pset;
117 
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);
124  }
125 
126  // start up message facility service
127  mf::StartMessageFacility(pset);
128  if (dump) {
129  std::cout << pset.to_indented_string() << std::endl;
130  }
131  mf::SetApplicationName(application);
132 
133  // logging message...
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;
142 
143  return 0;
144 }