53 ClassImp(TMVA::MsgLogger);
57 const UInt_t TMVA::MsgLogger::fgMaxSourceSize = 25;
59 const std::string TMVA::MsgLogger::fgPrefix =
"";
60 const std::string TMVA::MsgLogger::fgSuffix =
": ";
61 #if __cplusplus > 199711L
62 std::atomic<Bool_t> TMVA::MsgLogger::fgInhibitOutput{kFALSE};
63 std::atomic<const std::map<TMVA::EMsgType, std::string>*> TMVA::MsgLogger::fgTypeMap{0};
64 std::atomic<const std::map<TMVA::EMsgType, std::string>*> TMVA::MsgLogger::fgColorMap{0};
66 Bool_t TMVA::MsgLogger::fgInhibitOutput = kFALSE;
67 const std::map<TMVA::EMsgType, std::string>* TMVA::MsgLogger::fgTypeMap = 0;
68 const std::map<TMVA::EMsgType, std::string>* TMVA::MsgLogger::fgColorMap = 0;
70 static std::unique_ptr<const std::map<TMVA::EMsgType, std::string> > gOwnTypeMap;
71 static std::unique_ptr<const std::map<TMVA::EMsgType, std::string> > gOwnColorMap;
74 void TMVA::MsgLogger::InhibitOutput() { fgInhibitOutput = kTRUE; }
75 void TMVA::MsgLogger::EnableOutput() { fgInhibitOutput = kFALSE; }
79 TMVA::MsgLogger::MsgLogger(
const TObject* source, EMsgType minType )
80 : fObjSource ( source ),
91 TMVA::MsgLogger::MsgLogger(
const std::string& source, EMsgType minType )
93 fStrSource ( source ),
103 TMVA::MsgLogger::MsgLogger( EMsgType minType )
105 fStrSource (
"Unknown" ),
106 fActiveType( kINFO ),
115 TMVA::MsgLogger::MsgLogger(
const MsgLogger& parent )
116 : std::basic_ios<MsgLogger::char_type, MsgLogger::traits_type>(),
117 std::ostringstream(),
128 TMVA::MsgLogger::~MsgLogger()
135 TMVA::MsgLogger& TMVA::MsgLogger::operator= (
const MsgLogger& parent )
137 if (&parent !=
this) {
138 fObjSource = parent.fObjSource;
139 fStrSource = parent.fStrSource;
140 fActiveType = parent.fActiveType;
141 fMinType = parent.fMinType;
150 std::string TMVA::MsgLogger::GetFormattedSource()
const
152 std::string source_name;
153 if (fActiveType == kHEADER)
155 source_name = fStrSource;
157 if (fActiveType == kWARNING)
159 source_name =
"<WARNING>";
161 if (source_name.size() > fgMaxSourceSize) {
162 source_name = source_name.substr( 0, fgMaxSourceSize - 3 );
163 source_name +=
"...";
172 std::string TMVA::MsgLogger::GetPrintedSource()
const
174 std::string source_name = GetFormattedSource();
175 if (source_name.size() < fgMaxSourceSize)
176 for (std::string::size_type i=source_name.size(); i<fgMaxSourceSize; i++) source_name.push_back(
' ' );
178 return fgPrefix + source_name + fgSuffix;
184 void TMVA::MsgLogger::Send()
187 std::string source_name = GetFormattedSource();
189 std::string message = this->str();
190 std::string::size_type previous_pos = 0, current_pos = 0;
194 current_pos = message.find(
'\n', previous_pos );
195 std::string line = message.substr( previous_pos, current_pos - previous_pos );
197 std::ostringstream message_to_send;
199 message_to_send.setf( std::ios::adjustfield, std::ios::left );
200 message_to_send.width( fgMaxSourceSize );
201 message_to_send << source_name << fgSuffix << line;
202 std::string msg = message_to_send.str();
203 this->WriteMsg( fActiveType, msg );
205 if (current_pos == message.npos)
break;
206 previous_pos = current_pos + 1;
219 void TMVA::MsgLogger::WriteMsg( EMsgType type,
const std::string& line )
const
221 if ( (type < fMinType || fgInhibitOutput) && type!=kFATAL )
return;
223 std::map<EMsgType, std::string>::const_iterator stype;
225 if ((stype = fgTypeMap.load()->find( type )) != fgTypeMap.load()->end()) {
226 if (!gConfig().IsSilent() || type==kFATAL) {
227 if (gConfig().UseColor()) {
229 if (type == kHEADER || type ==kWARNING)
230 std::cout << fgPrefix << line << std::endl;
231 else if (type == kINFO || type == kVERBOSE)
233 std::cout << line << std::endl;
236 std::cout << fgColorMap.load()->find( type )->second <<
"<" << stype->second <<
">" << line <<
"\033[0m" << std::endl;
241 if (type == kINFO) std::cout << fgPrefix << line << std::endl;
242 else std::cout << fgPrefix <<
"<" << stype->second <<
"> " << line << std::endl;
248 if (type == kFATAL) {
249 std::cout <<
"***> abort program execution" << std::endl;
250 throw std::runtime_error(
"FATAL error");
260 TMVA::MsgLogger& TMVA::MsgLogger::Endmsg( MsgLogger& logger )
269 void TMVA::MsgLogger::InitMaps()
272 std::map<TMVA::EMsgType, std::string>*tmp =
new std::map<TMVA::EMsgType, std::string>();
274 (*tmp)[kVERBOSE] = std::string(
"VERBOSE");
275 (*tmp)[kDEBUG] = std::string(
"DEBUG");
276 (*tmp)[kINFO] = std::string(
"INFO");
277 (*tmp)[kWARNING] = std::string(
"WARNING");
278 (*tmp)[kERROR] = std::string(
"ERROR");
279 (*tmp)[kFATAL] = std::string(
"FATAL");
280 (*tmp)[kSILENT] = std::string(
"SILENT");
281 (*tmp)[kHEADER] = std::string(
"HEADER");
282 const std::map<TMVA::EMsgType, std::string>* expected=0;
283 if(fgTypeMap.compare_exchange_strong(expected,tmp)) {
285 gOwnTypeMap.reset(tmp);
293 std::map<TMVA::EMsgType, std::string>*tmp =
new std::map<TMVA::EMsgType, std::string>();
295 (*tmp)[kVERBOSE] = std::string(
"");
296 (*tmp)[kDEBUG] = std::string(
"\033[34m");
297 (*tmp)[kINFO] = std::string(
"");
298 (*tmp)[kWARNING] = std::string(
"\033[1;31m");
299 (*tmp)[kERROR] = std::string(
"\033[31m");
300 (*tmp)[kFATAL] = std::string(
"\033[37;41;1m");
301 (*tmp)[kSILENT] = std::string(
"\033[30m");
303 const std::map<TMVA::EMsgType, std::string>* expected=0;
304 if(fgColorMap.compare_exchange_strong(expected,tmp)) {
306 gOwnColorMap.reset(tmp);