00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "messagefacility/MessageLogger/MessageLogger.h"
00010
00011 #include "fhiclcpp/make_ParameterSet.h"
00012 #include "fhiclcpp/ParameterSet.h"
00013
00014 #include <boost/program_options.hpp>
00015 #include <boost/filesystem.hpp>
00016
00017 using namespace boost;
00018 namespace BFS = boost::filesystem;
00019 namespace po = boost::program_options;
00020
00021 #include <iostream>
00022 #include <algorithm>
00023 #include <iterator>
00024 #include <fstream>
00025
00026 int main(int ac, char* av[])
00027 {
00028 std::string severity;
00029 std::string application;
00030 std::string message;
00031 std::string cat;
00032 std::string conf;
00033 bool dump;
00034
00035 std::vector<std::string> messages;
00036 std::vector<std::string> vcat;
00037
00038 std::vector<std::string> vcat_def;
00039
00040 vcat_def.push_back("");
00041
00042 try
00043 {
00044 po::options_description cmdopt("Allowed options");
00045 cmdopt.add_options()
00046 ("help,h", "display help message")
00047 ("severity,s",
00048 po::value<std::string>(&severity)->default_value("info"),
00049 "severity of the message (error, warning, info, debug)")
00050 ("category,g",
00051 po::value<std::vector<std::string>>(&vcat)->default_value(vcat_def, "null"),
00052 "message id / categories")
00053 ("application,a",
00054 po::value<std::string>(&application)->default_value("msgsenderApplication"),
00055 "issuing application name")
00056 ("config,c",
00057 po::value<std::string>(&conf)->default_value(""),
00058 "MessageFacility configuration file")
00059 ("dump,d", po::bool_switch(&dump)->default_value(false));
00060
00061 po::options_description hidden("Hidden options");
00062 hidden.add_options()
00063 ("message", po::value<std::vector<std::string>>(&messages), "message text");
00064
00065 po::options_description desc;
00066 desc.add(cmdopt).add(hidden);
00067
00068 po::positional_options_description p;
00069 p.add("message", -1);
00070
00071 po::variables_map vm;
00072 po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);
00073 po::notify(vm);
00074
00075 if (vm.count("help"))
00076 {
00077 std::cout << "Usage: msglogger [options] <message text>\n";
00078 std::cout << cmdopt;
00079 return 0;
00080 }
00081 }
00082 catch (std::exception& e)
00083 {
00084 std::cerr << "error: " << e.what() << "\n";
00085 return 1;
00086 }
00087 catch (...)
00088 {
00089 std::cerr << "Exception of unknown type!\n";
00090 return 1;
00091 }
00092
00093 std::vector<std::string>::iterator it;
00094
00095
00096 if (messages.size() == 0)
00097 {
00098 std::cout << "Message text is missing!\n";
00099 std::cout << "Use \"msglogger --help\" for help messages\n";
00100 return 1;
00101 }
00102
00103 if (application.empty())
00104 {
00105 std::cout << "Application name is missing!\n";
00106 std::cout << "Message cannot be issued without specifying the application name.\n";
00107 return 1;
00108 }
00109
00110
00111 it = messages.begin();
00112 while (it != messages.end())
00113 {
00114 message += *it + " ";
00115 ++it;
00116 }
00117
00118
00119 transform(severity.begin(), severity.end(), severity.begin(), ::toupper);
00120 if ((severity != "ERROR") && (severity != "WARNING")
00121 && (severity != "INFO") && (severity != "DEBUG"))
00122 {
00123 std::cerr << "Unknown severity level!\n";
00124 return 1;
00125 }
00126
00127
00128 it = vcat.begin();
00129 while (it != vcat.end())
00130 {
00131 cat += *it + ((it == vcat.end() - 1) ? "" : "|");
00132 ++it;
00133 }
00134
00135
00136 fhicl::ParameterSet pset;
00137
00138 std::ifstream logfhicl(conf);
00139 if (logfhicl.is_open())
00140 {
00141 std::stringstream fhiclstream;
00142 fhiclstream << logfhicl.rdbuf();
00143 std::string pstr(fhiclstream.str());
00144 fhicl::make_ParameterSet(pstr, pset);
00145 }
00146
00147
00148 mf::StartMessageFacility(pset);
00149 if (dump)
00150 {
00151 std::cout << pset.to_indented_string() << std::endl;
00152 }
00153 mf::SetApplicationName(application);
00154
00155
00156 if (severity == "ERROR")
00157 mf::LogError(cat) << message;
00158 else if (severity == "WARNING")
00159 mf::LogWarning(cat) << message;
00160 else if (severity == "INFO")
00161 mf::LogInfo(cat) << message;
00162 else if (severity == "DEBUG")
00163 mf::LogDebug(cat) << message;
00164
00165
00166 return 0;
00167 }