22 #include "TVirtualMutex.h"
25 #ifndef _NO_IMPLEMENTATION_
26 #define _NO_IMPLEMENTATION_ { \
27 const char *text = __FUNCTION__; \
28 NDC::push(_T("NO IMPLEMENTATION")); \
29 PrintLogger(text,'D',""); \
34 #define COLOR_NORMAL "\033[0m"
35 #define COLOR_RED "\033[31m"
36 #define COLOR_GREEN "\033[32m"
37 #define COLOR_YELLOW "\033[33m"
38 #define COLOR_BLUE "\033[34m"
39 #define COLOR_PINK "\033[35m"
41 #include "StLoggerManager.h"
42 bool StLoggerManager::mColorEnabled = kFALSE;
44 #include <log4cxx/basicconfigurator.h>
45 #include <log4cxx/propertyconfigurator.h>
46 #include <log4cxx/helpers/exception.h>
47 #include <log4cxx/ndc.h>
48 #include <log4cxx/consoleappender.h>
49 #include <log4cxx/patternlayout.h>
50 #include <log4cxx/layout.h>
51 #include <log4cxx/xml/domconfigurator.h>
55 #include "StStarLogger/StUCMAppender.h"
57 using namespace log4cxx;
58 using namespace log4cxx::helpers;
59 using namespace log4cxx::spi;
60 using namespace log4cxx::varia;
61 using namespace log4cxx::db;
62 #if (STAR_LOG4CXX_VERSION == 10)
63 using namespace log4cxx::filter;
70 log4cxx::LoggerPtr StLoggerManager::fgQALogger;
71 log4cxx::LoggerPtr &StLoggerManager::fgUCMLogger = *(
new log4cxx::LoggerPtr());
73 const char *StLoggerManager::fgLevels =
"FEWIDQU";
77 static void Log4cxx4RootErrorHandler(Int_t level, Bool_t abort,
const char *location,
const char *msg)
87 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,00,0)
88 if (gErrorIgnoreLevel == kUnset) {
90 R__LOCKGUARD2(gErrorMutex);
95 gErrorIgnoreLevel = 0;
97 TString level = gEnv->GetValue(
"Root.ErrorIgnoreLevel",
"Info");
98 if (!level.CompareTo(
"Info",TString::kIgnoreCase))
99 gErrorIgnoreLevel = kInfo;
100 else if (!level.CompareTo(
"Warning",TString::kIgnoreCase))
101 gErrorIgnoreLevel = kWarning;
102 else if (!level.CompareTo(
"Error",TString::kIgnoreCase))
103 gErrorIgnoreLevel = kError;
104 else if (!level.CompareTo(
"Break",TString::kIgnoreCase))
105 gErrorIgnoreLevel = kBreak;
106 else if (!level.CompareTo(
"SysError",TString::kIgnoreCase))
107 gErrorIgnoreLevel = kSysError;
108 else if (!level.CompareTo(
"Fatal",TString::kIgnoreCase))
109 gErrorIgnoreLevel = kFatal;
113 if (level < gErrorIgnoreLevel)
117 if (level >= kFatal) {
118 LOG_FATAL << location << msg << endm;
119 }
else if (level >= kSysError) {
120 LOG_FATAL << location <<
" : " << msg << endm;
121 }
else if (level >= kBreak) {
122 LOG_FATAL << location <<
" : " << msg << endm;
123 }
else if (level >= kError) {
124 LOG_ERROR << location <<
" : " << msg << endm;
127 }
else if (level >= kInfo) {
128 LOG_INFO << location <<
" : " << msg << endm;
133 gSystem->StackTrace();
142 std::ostream& StLoggerManager::OperatorShift(std::ostream& os,
StMessage* stm) {
144 std::ostringstream &thisStream = Stream();
145 if ( (&thisStream == &os) && (stm == endm) ) {
148 StMessMgr::CurrentMessager()->Print();
149 os.seekp(0);os<< ends;os.seekp(0);
163 StMessMgr* StLoggerManager::mInstance = 0;
171 StLoggerManager::StLoggerManager(
const char *loggerName)
172 :
StMessMgr(), fCurType(0),fAllowRepeat(-1),fLastRepeatCounter(0),fStarOptionFilter(0)
199 for (i=0;i<strlen(fgLevels);i++){
200 fSourceFileNames.push_back(
"");
201 fLineNumbers[i] = -1;
203 fLogger = Logger::getLogger(_T(loggerName));
204 fDefaultLevel = fLogger->getLevel();
206 fCurOpt =
new char[32]; fCurOpt [0] = 0;
212 StLoggerManager::~StLoggerManager() {
218 if (fCurOpt)
delete [] fCurOpt; fCurOpt = 0;
220 if (StLoggerManager::Instance() ==
this) {
227 StMessMgr* StLoggerManager::Instantiate()
228 {
return StLoggerManager::StarLoggerInit(); }
231 StMessMgr* StLoggerManager::Instantiate(
const char *loggerName)
232 {
return StLoggerManager::StarLoggerInit(loggerName); }
235 StMessMgr* StLoggerManager::StarLoggerInit(
const char *loggerName)
240 StMessMgr* StLoggerManager::StarLoggerInit() {
247 const char *proEnv = 0;
249 String propertyFile =
"log4j.xml";
251 TString Color = gEnv->GetValue(
"Logger.Color",
"no");
252 mColorEnabled = Color.CompareTo(
"no",TString::kIgnoreCase);
253 TString fullPropertyFileName = gEnv->GetValue(
"Logger.Configuration",
"log4j.xml");
254 gSystem->ExpandPathName(fullPropertyFileName);
255 String propertyFile = (
const char*)fullPropertyFileName;
256 proEnv = gSystem->Getenv(
"STAR_LOGGER_PROPERTY");
258 StarOptionFilterPtr filter;
259 if (proEnv && proEnv[0] ) propertyFile=proEnv;
260 if (!gSystem->AccessPathName(propertyFile.c_str())) {
261 xml::DOMConfigurator::configure(propertyFile);
265 LoggerPtr root = Logger::getRootLogger();
267 ConsoleAppenderPtr appender =
new ConsoleAppender(
268 new PatternLayout(
"%-3c{2}:%-5p - %m%n"));
270 appender->setName(_T(
"defaultAppender"));
271 root->addAppender(appender);
273 root->setLevel(LOG4CXX_LEVEL_INFO);
275 LoggerPtr root = Logger::getRootLogger();
277 fgQALogger = Logger::getLogger(
"QA");
278 fgUCMLogger = Logger::getLogger(
"UCM");
281 TString ucmenv = gSystem->Getenv(
"LOGGING");
282 if (ucmenv ==
"UCM" && gSystem->Getenv(
"JOBINDEX") && gSystem->Getenv(
"REQUESTID") ) {
283 StUCMAppenderPtr appender(
new StUCMAppender(ucmenv.Data()));
284 appender->setLayout(
new PatternLayout(
"%m"));
285 appender->setName(_T(
"UCM"));
286 fgUCMLogger->addAppender(appender);
287 StringMatchFilterPtr filter(
new StringMatchFilter());
288 filter->setStringToMatch(_T(
"StageID="));
289 filter->setAcceptOnMatch(
true);
290 appender->addFilter(filter);
291 appender->addFilter( DenyAllFilterPtr(
new DenyAllFilter));
294 fgUCMLogger->setLevel(LOG4CXX_LEVEL_DEBUG);
300 mInstance = StarLoggerInit(
"BFC");
303 gMessMgr = mInstance;
306 SetErrorHandler(Log4cxx4RootErrorHandler);
312 std::ostringstream &StLoggerManager::Stream()
318 case 'F':
return fStreams[0];
319 case 'E':
return fStreams[1];
320 case 'W':
return fStreams[2];
321 case 'I':
return fStreams[3];
322 case 'D':
return fStreams[4];
323 case 'Q':
return fStreams[5];
324 default:
return fStreams[6];
329 bool StLoggerManager::isDebugEnabled()
const{
return fLogger->isDebugEnabled(); }
331 bool StLoggerManager::isWarnEnabled()
const{
return fLogger->isWarnEnabled(); }
333 bool StLoggerManager::isErrorEnabled()
const{
return fLogger->isErrorEnabled(); }
335 bool StLoggerManager::isInfoEnabled()
const{
return fLogger->isInfoEnabled(); }
337 bool StLoggerManager::isFatalEnabled()
const{
return fLogger->isFatalEnabled(); }
339 bool StLoggerManager::isEnabledFor()
const{
return true; }
341 bool StLoggerManager::isQAInfoEnabled()
const{
return fgQALogger? fgQALogger->isInfoEnabled():
false; }
343 bool StLoggerManager::isUCMInfoEnabled()
const{
return fgUCMLogger? fgUCMLogger->isInfoEnabled():
false; }
345 std::ostringstream& StLoggerManager::Message(
const char* mess,
const char* type,
346 const char* opt,
const char *sourceFileName,
int lineNumber) {
369 unsigned char typeChar =
'I';
370 if (type && type[0]) typeChar = type[0];
378 if (sourceFileName && sourceFileName[0] ) fSourceFileNames[LevelIndex(fCurType)] = sourceFileName;
379 else fSourceFileNames[LevelIndex(fCurType)].clear();
380 fLineNumbers[LevelIndex(fCurType)] = lineNumber;
382 if (mess && std::char_traits<char>::length(mess) > 0) {
383 BuildMessage(mess, typeChar, opt, sourceFileName, lineNumber);
391 void StLoggerManager::BuildMessage(
const char* mess,
unsigned char type,
392 const char* opt,
const char *sourceFileName,
int lineNumber) {
414 PrintLogger(mess,type,opt,sourceFileName,lineNumber);
417 void StLoggerManager::PrintLogger(
const char* mess,
unsigned char type,
418 const char* opt,
const char *sourceFileName,
int lineNumber)
421 unsigned char typeChar =
'I';
423 if (type) typeChar = type;
424 if (!(sourceFileName || fSourceFileNames[LevelIndex(typeChar)].empty() ) )
426 sourceFileName = fSourceFileNames[LevelIndex(typeChar)].c_str();
429 if ( lineNumber == -1)
430 lineNumber = fLineNumbers[LevelIndex(typeChar)];
431 bool canPrint =
true;
432 if (fAllowRepeat >= 0 ) {
434 if ( mess == fLastMessage ) {
435 fLastRepeatCounter++;
436 canPrint = (fAllowRepeat >= fLastRepeatCounter);
438 fLastRepeatCounter= 0;
443 if ( (mess == 0) || (mess[0] == 0)) mess =
".";
445 #if (STAR_LOG4CXX_VERSION >= 10)
450 case 'F': fLogger->fatal (_T(COLOR_PINK + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
451 case 'E': fLogger->error (_T(COLOR_RED + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
452 case 'W': fLogger->warn (_T(COLOR_YELLOW + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
453 case 'I': fLogger->info (_T(COLOR_GREEN + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
454 case 'D': fLogger->debug (_T(COLOR_BLUE + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
455 case 'Q': fgQALogger->info (_T(COLOR_GREEN + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
456 case 'U': fgUCMLogger->info(_T(COLOR_NORMAL + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
457 default: fLogger->info (_T(COLOR_NORMAL + Mess + COLOR_NORMAL),LocationInfo(sourceFileName,
"",lineNumber));
break;
463 case 'F': fLogger->fatal (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
464 case 'E': fLogger->error (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
465 case 'W': fLogger->warn (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
466 case 'I': fLogger->info (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
467 case 'D': fLogger->debug (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
468 case 'Q': fgQALogger->info (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
469 case 'U': fgUCMLogger->info(_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
470 default: fLogger->info (_T(mess),LocationInfo(sourceFileName,
"",lineNumber));
break;
475 if (lineNumber != -1) {
480 void StLoggerManager::Print() {
485 string message = Stream().str();
486 BuildMessage(message.c_str(),fCurType,fCurOpt);
489 int StLoggerManager::PrintList(messVec* list) {
497 StMessage* StLoggerManager::FindMessage(
const char* s1,
const char* s2,
498 const char* s3,
const char* s4, messVec* list) {
503 messVec* StLoggerManager::FindMessageList(
const char* s1,
const char* s2,
504 const char* s3,
const char* s4, messVec* list) {
509 int StLoggerManager::RemoveMessage(
StMessage* mess) {
514 void StLoggerManager::Summary(
size_t nTerms) {
528 void StLoggerManager::MemorySummary() {
536 int StLoggerManager::AddType(
const char* type,
const char* text) {
550 void StLoggerManager::PrintInfo() {
551 fLogger->info(
"**************************************************************\n");
552 fLogger->info(
"* $Id: StLoggerManager.cxx,v 1.50 2013/07/23 20:03:05 dmitry Exp $\n");
554 fLogger->info(
"**************************************************************\n");
563 std::ostringstream& StLoggerManager::Info(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
564 {
return Message(mess,
"I", opt,sourceFileName,lineNumber);}
566 int StLoggerManager::PrintInfos()
573 const messVec* StLoggerManager::GetInfos()
580 StMessage* StLoggerManager::FindInfo(
const char* s1,
const char* s2,
581 const char* s3,
const char* s4)
588 messVec* StLoggerManager::FindInfoList(
const char* s1,
const char* s2,
589 const char* s3,
const char* s4)
600 std::ostringstream& StLoggerManager::Warning(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
601 {
return Message(mess,
"W", opt,sourceFileName,lineNumber);}
603 int StLoggerManager::PrintWarnings()
610 const messVec* StLoggerManager::GetWarnings()
617 StMessage* StLoggerManager::FindWarning(
const char* s1,
const char* s2,
618 const char* s3,
const char* s4)
625 messVec* StLoggerManager::FindWarningList(
const char* s1,
const char* s2,
626 const char* s3,
const char* s4)
637 std::ostringstream& StLoggerManager::Error(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
638 {
return Message(mess,
"E", opt,sourceFileName,lineNumber);}
640 int StLoggerManager::PrintErrors()
647 const messVec* StLoggerManager::GetErrors()
654 StMessage* StLoggerManager::FindError(
const char* s1,
const char* s2,
655 const char* s3,
const char* s4)
662 messVec* StLoggerManager::FindErrorList(
const char* s1,
const char* s2,
663 const char* s3,
const char* s4)
674 std::ostringstream& StLoggerManager::Debug(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
675 {
return Message(mess,
"D", opt,sourceFileName,lineNumber);}
677 int StLoggerManager::PrintDebug()
684 const messVec* StLoggerManager::GetDebugs()
686 _NO_IMPLEMENTATION_;
return 0;
691 StMessage* StLoggerManager::FindDebug(
const char* s1,
const char* s2,
692 const char* s3,
const char* s4)
694 _NO_IMPLEMENTATION_;
return 0;
698 messVec* StLoggerManager::FindDebugList(
const char* s1,
const char* s2,
699 const char* s3,
const char* s4)
701 _NO_IMPLEMENTATION_;
return 0;
709 std::ostringstream& StLoggerManager::QAInfo(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
710 {
return Message(mess,
"Q", opt,sourceFileName,lineNumber);}
715 std::ostringstream& StLoggerManager::UCMInfo(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
716 {
return Message(mess,
"U", opt,sourceFileName,lineNumber);}
718 void StLoggerManager::IgnoreRepeats()
724 void StLoggerManager::AllowRepeats(
int nRepeats)
729 fAllowRepeat = nRepeats;
730 fLastRepeatCounter = 0;
731 StarOptionFilterPtr& filter = ((
StLoggerManager *)mInstance)->GetStarOptionFilter();
733 filter->setRepeatCounterOption(nRepeats);
739 void StLoggerManager::AllowRepeats()
746 int StLoggerManager::PrintQAInfo()
748 _NO_IMPLEMENTATION_;
return 0;
752 const messVec* StLoggerManager::GetQAInfos()
754 _NO_IMPLEMENTATION_;
return 0;
758 StMessage* StLoggerManager::FindQAInfo(
const char* s1,
const char* s2,
759 const char* s3,
const char* s4)
761 _NO_IMPLEMENTATION_;
return 0;
765 messVec* StLoggerManager::FindQAInfoList(
const char* s1,
const char* s2,
766 const char* s3,
const char* s4)
768 _NO_IMPLEMENTATION_;
return 0;
773 int StLoggerManager::PrintUCMInfo()
775 _NO_IMPLEMENTATION_;
return 0;
779 const messVec* StLoggerManager::GetUCMInfos()
781 _NO_IMPLEMENTATION_;
return 0;
785 StMessage* StLoggerManager::FindUCMInfo(
const char* s1,
const char* s2,
786 const char* s3,
const char* s4)
788 _NO_IMPLEMENTATION_;
return 0;
792 messVec* StLoggerManager::FindUCMInfoList(
const char* s1,
const char* s2,
793 const char* s3,
const char* s4)
795 _NO_IMPLEMENTATION_;
return 0;
800 std::ostringstream& StLoggerManager::Fatal(
const char* mess,
const char* opt,
const char *sourceFileName,
int lineNumber)
801 {
return Message(mess,
"F",opt,sourceFileName,lineNumber);}
805 std::ostringstream& StLoggerManager::out(
const char* mess)
806 {
return Message(mess,
"I",
"OP-");}
808 std::ostringstream& StLoggerManager::err(
const char* mess)
809 {
return Message(mess,
"E",
"EP-");}
815 int StLoggerManager::PrintAll()
817 _NO_IMPLEMENTATION_;
return 0;
822 const messVec* StLoggerManager::GetAll()
824 _NO_IMPLEMENTATION_;
return 0;
830 int StLoggerManager::RemoveMessage(
const char* s1,
const char* s2,
831 const char* s3,
const char* s4)
832 {
return RemoveMessage(FindMessage(s1,s2,s3,s4)); }
835 void StLoggerManager::SetLimit(
const char* str,
int n)
837 if (str && str[0] && str[1]) {
838 LoggerPtr root = Logger::getRootLogger();
839 AppenderPtr defaultAppender = root->getAppender(_T(
"defaultAppender"));
840 if (defaultAppender != 0){
842 filter->setRepeatCounterOption(n);
843 filter->setOption(_T(
"StringToCount"),str);
846 defaultAppender->addFilter(filter);
853 int StLoggerManager::GetLimit(
const char* str)
855 _NO_IMPLEMENTATION_;
return -1;
859 void StLoggerManager::ListLimits()
865 void StLoggerManager::Close()
869 if (fgQALogger) fgQALogger->closeNestedAppenders();
870 if (fgUCMLogger) fgUCMLogger->closeNestedAppenders();
873 void StLoggerManager::RemoveLimit(
const char* str)
874 { SetLimit(str,-1); }
876 void StLoggerManager::SwitchOff(
const char* str)
879 void StLoggerManager::SwitchOn(
const char* str)
880 { RemoveLimit(str); }
882 void StLoggerManager::FixOn(
const char* str)
883 { SetLimit(str,-5); }
885 void StLoggerManager::NoLimits()
891 void StLoggerManager::MemoryOn()
897 void StLoggerManager::MemoryOff()
903 int StLoggerManager::ListTypes()
905 _NO_IMPLEMENTATION_;
return 5;
910 void StLoggerManager::SetLevel(Int_t level)
915 fLogger->setLevel(LOG4CXX_LEVEL_FATAL);
918 fLogger->setLevel(LOG4CXX_LEVEL_ERROR);
921 fLogger->setLevel(LOG4CXX_LEVEL_WARN);
924 fLogger->setLevel(LOG4CXX_LEVEL_INFO);
929 fLogger->setLevel(LOG4CXX_LEVEL_DEBUG);
933 fLogger->setLevel(fDefaultLevel);
936 fLogger->setLevel(LOG4CXX_LEVEL_DEBUG);
941 Int_t StLoggerManager::GetLevel(Int_t)
const
945 const LevelPtr &level = fLogger->getLevel();
946 if (level == &LOG4CXX_LEVEL_DEBUG)
return kDebug;
947 else if (level == &LOG4CXX_LEVEL_FATAL)
return kFatal;
948 else if (level == &LOG4CXX_LEVEL_ERROR)
return kError;
949 else if (level == &LOG4CXX_LEVEL_WARN )
return kWarning;
950 else if (level == &LOG4CXX_LEVEL_INFO )
return kInfo;
956 void StLoggerManager:: DestroyInstance()
961 fgUCMLogger->releaseRef();
965 mInstance = 0; gMessMgr = 0;
971 const char *GetName()