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