GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Messenger.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <iostream>
12 #include <vector>
13 #include <iomanip>
14 
15 #include "libxml/parser.h"
16 #include "libxml/xmlmemory.h"
17 #include "log4cpp/SimpleLayout.hh"
18 
19 #include <TSystem.h>
20 
25 
26 using std::setw;
27 using std::setfill;
28 using std::cout;
29 using std::endl;
30 using std::vector;
31 
32 using namespace genie;
33 
34 bool genie::gAbortingInErr = false;
35 
36 //____________________________________________________________________________
38 //____________________________________________________________________________
40 {
41  fInstance = 0;
42 }
43 //____________________________________________________________________________
45 {
46  fInstance = 0;
47 }
48 //____________________________________________________________________________
50 {
51  if(fInstance == 0) {
52 
53  // the first thing that get's printed in a GENIE session is the banner
55 
56  static Messenger::Cleaner cleaner;
58 
59  fInstance = new Messenger;
60 
61  log4cpp::Appender * appender;
62  appender = new log4cpp::OstreamAppender("default", &cout);
63  const char* layoutenv = gSystem->Getenv("GMSGLAYOUT");
64  std::string layoutstr = (layoutenv) ? string(layoutenv) : "BASIC";
65  if ( layoutstr == "SIMPLE" )
66  appender->setLayout(new log4cpp::SimpleLayout());
67  else
68  appender->setLayout(new log4cpp::BasicLayout());
69 
70 
71  log4cpp::Category & MSG = log4cpp::Category::getRoot();
72 
73  MSG.setAdditivity(false);
74  MSG.addAppender(appender);
75 
76  fInstance->Configure(); // set user-defined priority levels
77  }
78  return fInstance;
79 }
80 //____________________________________________________________________________
81 log4cpp::Category & Messenger::operator () (const char * stream)
82 {
83  log4cpp::Category & MSG = log4cpp::Category::getInstance(stream);
84 
85  return MSG;
86 }
87 //____________________________________________________________________________
89  const char * stream, log4cpp::Priority::Value priority)
90 {
91  log4cpp::Category & MSG = log4cpp::Category::getInstance(stream);
92 
93  MSG.setPriority(priority);
94 }
95 //____________________________________________________________________________
97 {
98 // The Configure() method will look for priority level xml config files, read
99 // the priority levels and set them.
100 // By default, first it will look for the $GENIE/config/Messenger.xml file.
101 // Look at this file for the XML schema.
102 
103  string msg_config_file = utils::xml::GetXMLFilePath("Messenger.xml");
104 
105  // parse & set the default priority levels
106  bool ok = this->SetPrioritiesFromXmlFile(msg_config_file);
107  if(!ok) {
108  SLOG("Messenger", pERROR)
109  << "Priority levels from: " << msg_config_file << " were not set!";
110  }
111 
112 }
113 //____________________________________________________________________________
115 {
116 // Reads input XML config file and sets the priority levels.
117 // The input can be a colection of XML files, delimited with a ":".
118 // The full path for each file should be given.
119 // In case of multiple files, all files are read.
120 // The later each file is, the higher priority it has (if the same mesg
121 // stream is listed twice with conflicting priority, the last one is used.).
122 //
123 
124  if(filenames.size()==0) return false;
125 
126  vector<string> filename_vec = utils::str::Split(filenames, ":");
127  if(filename_vec.size() == 0) return false;
128 
129  vector<string>::const_iterator filename_iter;
130  for(filename_iter = filename_vec.begin();
131  filename_iter != filename_vec.end(); ++filename_iter)
132  {
133  // look in all known XML locations, just like primary file
134  string filename = utils::xml::GetXMLFilePath(*filename_iter);
135 
136  SLOG("Messenger", pNOTICE)
137  << "Reading msg stream priorities from XML file: " << filename;
138 
139  xmlDocPtr xml_doc = xmlParseFile(filename.c_str());
140  if(xml_doc==NULL) {
141  SLOG("Messenger", pERROR)
142  << "XML file could not be parsed! [file: " << filename << "]";
143  return false;
144  }
145 
146  xmlNodePtr xml_root = xmlDocGetRootElement(xml_doc);
147  if(xml_root==NULL) {
148  SLOG("Messenger", pERROR)
149  << "XML doc. has null root element! [file: " << filename << "]";
150  xmlFreeDoc(xml_doc);
151  return false;
152  }
153 
154  if( xmlStrcmp(xml_root->name, (const xmlChar *) "messenger_config") ) {
155  SLOG("Messenger", pERROR)
156  << "XML doc. has invalid root element! [file: " << filename << "]";
157  xmlFreeNode(xml_root);
158  xmlFreeDoc(xml_doc);
159  return false;
160  }
161 
162  xmlNodePtr xml_msgp = xml_root->xmlChildrenNode; // <priority>'s
163 
164  // loop over all xml tree nodes that are children of the <spline> node
165  while (xml_msgp != NULL) {
166 
167  // enter everytime you find a <priority> tag
168  if( (!xmlStrcmp(xml_msgp->name, (const xmlChar *) "priority")) ) {
169 
170  string msgstream = utils::str::TrimSpaces(
171  utils::xml::GetAttribute(xml_msgp, "msgstream"));
172  string priority =
173  utils::xml::TrimSpaces( xmlNodeListGetString(
174  xml_doc, xml_msgp->xmlChildrenNode, 1));
175  log4cpp::Priority::Value pv = this->PriorityFromString(priority);
176  this->SetPriorityLevel(msgstream.c_str(), pv);
177  SLOG("Messenger", pINFO)
178  << "Set priority level: " << setfill('.')
179  << setw(24) << msgstream << " --> " << priority;
180  }
181  xml_msgp = xml_msgp->next;
182  }//xml_msgp != NULL
183 
184  //xmlFree(xml_msgp);
185  xmlFreeNode(xml_msgp);
186  xmlFreeDoc(xml_doc);
187  }//filename_iter
188 
189  return true;
190 }
191 //____________________________________________________________________________
192 log4cpp::Priority::Value Messenger::PriorityFromString(string p)
193 {
194  if ( p.find("DEBUG") != string::npos ) return log4cpp::Priority::DEBUG;
195  if ( p.find("INFO") != string::npos ) return log4cpp::Priority::INFO;
196  if ( p.find("NOTICE") != string::npos ) return log4cpp::Priority::NOTICE;
197  if ( p.find("WARN") != string::npos ) return log4cpp::Priority::WARN;
198  if ( p.find("ERROR") != string::npos ) return log4cpp::Priority::ERROR;
199  if ( p.find("CRIT") != string::npos ) return log4cpp::Priority::CRIT;
200  if ( p.find("ALERT") != string::npos ) return log4cpp::Priority::ALERT;
201  if ( p.find("FATAL") != string::npos ) return log4cpp::Priority::FATAL;
202 
203  SLOG("Messenger", pWARN)
204  << "Unknown priority = " << p << " - Setting to INFO";
205  return log4cpp::Priority::INFO;
206 }
207 //____________________________________________________________________________
#define pERROR
Definition: Messenger.h:59
void DummyMethodAndSilentCompiler()
Definition: Messenger.h:280
string TrimSpaces(xmlChar *xmls)
virtual ~Messenger()
Definition: Messenger.cxx:44
log4cpp::Priority::Value PriorityFromString(string priority)
Definition: Messenger.cxx:192
log4cpp::Category & operator()(const char *stream)
Definition: Messenger.cxx:81
void Configure(void)
Definition: Messenger.cxx:96
bool SetPrioritiesFromXmlFile(string filename)
Definition: Messenger.cxx:114
static Messenger * Instance(void)
Definition: Messenger.cxx:49
string GetXMLFilePath(string basename)
#define pINFO
Definition: Messenger.h:62
#define pWARN
Definition: Messenger.h:60
string TrimSpaces(string input)
Definition: StringUtils.cxx:18
A more convenient interface to the log4cpp Message Service.
Definition: Messenger.h:258
void PrintBanner(void)
Definition: PrintUtils.cxx:114
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
void SetPriorityLevel(const char *stream, log4cpp::Priority::Value p)
Definition: Messenger.cxx:88
#define pNOTICE
Definition: Messenger.h:61
#define SLOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a short string (using the FUNCTION and...
Definition: Messenger.h:84
string GetAttribute(xmlNodePtr xml_cur, string attr_name)
bool gAbortingInErr
Definition: Messenger.cxx:34
static Messenger * fInstance
Definition: Messenger.h:273