From 6d7793cfa3e6db49292008b18eea05aff816cecd Mon Sep 17 00:00:00 2001 From: hristov Date: Tue, 16 Feb 2010 08:49:05 +0000 Subject: [PATCH] Making AliLog true instanton (Jimmy) --- EVE/alieve_main/alieve_main.cxx | 6 +- HLT/rec/AliHLTDynamicAliLog.cxx | 2 +- HMPID/AliHMPIDReconHTA.cxx | 6 +- ITS/ITSSPDFOda.cxx | 8 +- ITS/ITSSPDPHYSda.cxx | 6 +- ITS/ITSSPDSCANda.cxx | 5 +- STEER/AliLog.cxx | 204 ++++++++++++++++++++++---------- STEER/AliLog.h | 50 ++++++-- 8 files changed, 195 insertions(+), 92 deletions(-) diff --git a/EVE/alieve_main/alieve_main.cxx b/EVE/alieve_main/alieve_main.cxx index 6b3f633aa1d..41d9809aa20 100644 --- a/EVE/alieve_main/alieve_main.cxx +++ b/EVE/alieve_main/alieve_main.cxx @@ -64,8 +64,8 @@ int main(int argc, char **argv) } gROOT->SetMacroPath(macPath); - // How to hadle AliLog properly? - AliLog *log = new AliLog; + // get a logger instance + AliLog *log = AliLog::GetRootLogger(); TRint *app = new TRint("App", &argc, argv); #if ROOT_VERSION_CODE >= ROOT_VERSION(5,25,4) || defined XXX_LATEST_ROOT @@ -90,7 +90,7 @@ int main(int argc, char **argv) app->Terminate(0); - delete log; + // delete log; return 0; } diff --git a/HLT/rec/AliHLTDynamicAliLog.cxx b/HLT/rec/AliHLTDynamicAliLog.cxx index 18150101e3d..5ae47775ac4 100644 --- a/HLT/rec/AliHLTDynamicAliLog.cxx +++ b/HLT/rec/AliHLTDynamicAliLog.cxx @@ -134,7 +134,7 @@ extern "C" int InitAliDynamicMessageCallback() // older versions of AliLog does not support the notification callback and // stringstreams, but they support the logging macros in general #ifndef NO_ALILOG_NOTIFICATION - AliLog* log=new AliLog; + AliLog* log = AliLog::GetRootLogger(); log->SetLogNotification(LogNotification); log->SetStreamOutput(&AliHLTLogging::fgLogstr); log->SetPrintScope(true); diff --git a/HMPID/AliHMPIDReconHTA.cxx b/HMPID/AliHMPIDReconHTA.cxx index 073e889de5f..b3378cb6965 100644 --- a/HMPID/AliHMPIDReconHTA.cxx +++ b/HMPID/AliHMPIDReconHTA.cxx @@ -527,7 +527,11 @@ void AliHMPIDReconHTA::FunMinPhot(Int_t &/* */,Double_t* /* */,Double_t &f,Doubl meanCkov1/=nClAcc; Double_t rms2 = (meanCkov3 - meanCkov*meanCkov*nClAcc)/nClAcc; - AliLog::Instance(); + + // get a logger instance + // what for?? + AliLog::GetRootLogger(); + if(nClAcc!=nClAccStep1) pRecHTA->SetFitStatus(kTRUE); else pRecHTA->SetFitStatus(kFALSE); pRecHTA->SetCkovFit(meanCkov1); diff --git a/ITS/ITSSPDFOda.cxx b/ITS/ITSSPDFOda.cxx index ecd03e821d9..48c60d34c19 100644 --- a/ITS/ITSSPDFOda.cxx +++ b/ITS/ITSSPDFOda.cxx @@ -85,11 +85,9 @@ int main(int argc, char **argv) { "TStreamerInfo()"); // turn off annoying warning messages - new AliLog; - AliLog::Instance()->SetGlobalDebugLevel(-20); - - - + // NB: Should not be handled here + AliLog logger = AliLog::GetRootLogger(); + logger->SetGlobalDebugLevel(-20); // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters********* diff --git a/ITS/ITSSPDPHYSda.cxx b/ITS/ITSSPDPHYSda.cxx index 5ebf3a4c7ed..a9792cc7bf7 100644 --- a/ITS/ITSSPDPHYSda.cxx +++ b/ITS/ITSSPDPHYSda.cxx @@ -97,9 +97,9 @@ int main(int argc, char **argv) { "TStreamerInfo()"); // turn off annoying warning messages - new AliLog; - AliLog::Instance()->SetGlobalDebugLevel(-20); - + // NB: Should not be handled here + AliLog logger = AliLog::GetRootLogger(); + logger->SetGlobalDebugLevel(-20); // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters********* UInt_t nrTuningParams = 0; diff --git a/ITS/ITSSPDSCANda.cxx b/ITS/ITSSPDSCANda.cxx index 9e67ed98354..85132e74e6c 100644 --- a/ITS/ITSSPDSCANda.cxx +++ b/ITS/ITSSPDSCANda.cxx @@ -85,8 +85,9 @@ int main(int argc, char **argv) { "TStreamerInfo()"); // turn off annoying warning messages - new AliLog; - AliLog::Instance()->SetGlobalDebugLevel(-20); + // NB: Should not be handled here + AliLog logger = AliLog::GetRootLogger(); + logger->SetGlobalDebugLevel(-20); // calib scan types enum calib_types{MINTH,MEANTH,DAC,UNIMA,NOISE,DELAY}; diff --git a/STEER/AliLog.cxx b/STEER/AliLog.cxx index 025852d202a..5258eb8d042 100644 --- a/STEER/AliLog.cxx +++ b/STEER/AliLog.cxx @@ -45,13 +45,40 @@ ClassImp(AliLog) - +// implementation of a singleton here AliLog* AliLog::fgInstance = NULL; Bool_t AliLog::fgDebugEnabled = kTRUE; +/** + * get root logger singleton instance + */ +AliLog *AliLog::GetRootLogger() +{ + if (fgInstance == NULL) + { + // creating singleton + fgInstance = new AliLog; + } + + return fgInstance; +} -//_____________________________________________________________________________ +/** + * delete the root logger singleton instance + */ +void AliLog::DeleteRootLogger() +{ + if (fgInstance != NULL) + { + delete fgInstance; + fgInstance = NULL; + } +} + +/** + * default private constructor + */ AliLog::AliLog() : TObject(), fGlobalLogLevel(kInfo), @@ -69,7 +96,8 @@ AliLog::AliLog() : { // default constructor: set default values - for (Int_t iType = kFatal; iType < kMaxType; iType++) { + for (Int_t iType = kFatal; iType < kMaxType; iType++) + { fOutputTypes[iType] = 0; fFileNames[iType] = ""; fOutputFiles[iType] = NULL; @@ -82,6 +110,7 @@ AliLog::AliLog() : fPrintLocation[iType] = (iType == kDebug); } + // TO BE REVIEWED // replace the previous instance by this one if (fgInstance) delete fgInstance; fgInstance = this; @@ -92,31 +121,41 @@ AliLog::AliLog() : ReadEnvSettings(); } -//_____________________________________________________________________________ +/** + * private destructor + */ AliLog::~AliLog() { // destructor: clean up and reset instance pointer if (fRepetitions > 0) PrintRepetitions(); - for (Int_t i = 0; i < fModuleDebugLevels.GetEntriesFast(); i++) { + for (Int_t i = 0; i < fModuleDebugLevels.GetEntriesFast(); i++) + { if (fModuleDebugLevels[i]) fModuleDebugLevels[i]->Delete(); } + fClassDebugLevels.Delete(); - for (Int_t i = 0; i < fClassDebugLevels.GetEntriesFast(); i++) { + + for (Int_t i = 0; i < fClassDebugLevels.GetEntriesFast(); i++) + { if (fClassDebugLevels[i]) fClassDebugLevels[i]->Delete(); } + fClassDebugLevels.Delete(); - for (Int_t iType = kFatal; iType < kMaxType; iType++) { + for (Int_t iType = kFatal; iType < kMaxType; iType++) + { CloseFile(iType); } + fflush(stderr); fflush(stdout); fgInstance = NULL; } +// NOT IMPLEMENTED!? //_____________________________________________________________________________ AliLog::AliLog(const AliLog& log) : TObject(log), @@ -138,6 +177,7 @@ AliLog::AliLog(const AliLog& log) : Fatal("AliLog", "copy constructor not implemented"); } +// NOT IMPLEMENTED!? //_____________________________________________________________________________ AliLog& AliLog::operator = (const AliLog& /*log*/) { @@ -148,48 +188,63 @@ AliLog& AliLog::operator = (const AliLog& /*log*/) } +/** + * gSystem see TSystem.h + * gEnv see TEnv.h + * + * LOG_NO_DEBUG: fgDebugEnabled <- false + * AliRoot.AliLog.EnableDebug + * AliRoot.AliLog.GlobalLogLevel + */ //_____________________________________________________________________________ void AliLog::ReadEnvSettings() { // load settings from the root configuration file (.rootrc) // and from environment variables - static const char* typeNames[kMaxType] = - {"kFatal", "kError", "kWarning", "kInfo", "kDebug"}; + static const char* typeNames[kMaxType] = {"kFatal", "kError", "kWarning", "kInfo", "kDebug"}; // debug en- or disabling - if (gSystem->Getenv("LOG_NO_DEBUG")) { + if (gSystem->Getenv("LOG_NO_DEBUG")) + { fgDebugEnabled = kFALSE; - } else if (gEnv->Defined("AliRoot.AliLog.EnableDebug")) { - fgDebugEnabled = gEnv->GetValue("AliRoot.AliLog.EnableDebug", - fgDebugEnabled); + } + else if (gEnv->Defined("AliRoot.AliLog.EnableDebug")) + { + fgDebugEnabled = gEnv->GetValue("AliRoot.AliLog.EnableDebug", fgDebugEnabled); AliInfo(Form("debug %sabled", ((fgDebugEnabled) ? "en" : "dis"))); } // global log level - if (gEnv->Defined("AliRoot.AliLog.GlobalLogLevel")) { + if (gEnv->Defined("AliRoot.AliLog.GlobalLogLevel")) + { const char* type = gEnv->GetValue("AliRoot.AliLog.GlobalLogLevel", ""); - for (Int_t iType = kFatal; iType < kMaxType; iType++) { + + for (Int_t iType = kFatal; iType < kMaxType; iType++) + { if (strcmp(type, typeNames[iType]) == 0) fGlobalLogLevel = iType; } + AliDebug(3, Form("global log level set to %d", fGlobalLogLevel)); } // global debug level - if (gEnv->Defined("AliRoot.AliLog.GlobalDebugLevel")) { - Int_t level = gEnv->GetValue("AliRoot.AliLog.GlobalDebugLevel", - Int_t(fGlobalLogLevel - kDebugOffset)); + if (gEnv->Defined("AliRoot.AliLog.GlobalDebugLevel")) + { + Int_t level = gEnv->GetValue("AliRoot.AliLog.GlobalDebugLevel", Int_t(fGlobalLogLevel - kDebugOffset)); if (level < -kDebugOffset) level = kDebugOffset; fGlobalLogLevel = kDebugOffset + level; - AliDebug(3, Form("global debug level set to %d", - fGlobalLogLevel - kDebugOffset)); + AliDebug(3, Form("global debug level set to %d", fGlobalLogLevel - kDebugOffset)); } // module debug level - if (gEnv->Defined("AliRoot.AliLog.ModuleDebugLevel")) { + if (gEnv->Defined("AliRoot.AliLog.ModuleDebugLevel")) + { TString levels = gEnv->GetValue("AliRoot.AliLog.ModuleDebugLevel", ""); char* p = const_cast(levels.Data()); - while (const char* module = strtok(p, " ")) { + + while (const char* module = strtok(p, " ")) + { p = NULL; char* pos = const_cast(index(module, ':')); if (!pos) continue; @@ -201,102 +256,120 @@ void AliLog::ReadEnvSettings() } // class debug level - if (gEnv->Defined("AliRoot.AliLog.ClassDebugLevel")) { + if (gEnv->Defined("AliRoot.AliLog.ClassDebugLevel")) + { TString levels = gEnv->GetValue("AliRoot.AliLog.ClassDebugLevel", ""); char* p = const_cast(levels.Data()); - while (const char* className = strtok(p, " ")) { + + while (const char* className = strtok(p, " ")) + { p = NULL; char* pos = const_cast(index(className, ':')); if (!pos) continue; *(pos++) = '\0'; Int_t level = atoi(pos); SetClassDebugLevel(className, level); - AliDebug(3, Form("debug level for class %s set to %d", - className, level)); + AliDebug(3, Form("debug level for class %s set to %d", className, level)); } } // general output stream - if (gEnv->Defined("AliRoot.AliLog.Output")) { + if (gEnv->Defined("AliRoot.AliLog.Output")) + { TString stream = gEnv->GetValue("AliRoot.AliLog.Output", "Standard"); - if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) { + + if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) + { SetStandardOutput(); AliDebug(3, "output stream set to standard output for all types"); - } else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) { + } + else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) + { SetErrorOutput(); AliDebug(3, "output stream set to error output for all types"); - } else if (!stream.IsNull()) { + } + else if (!stream.IsNull()) + { SetFileOutput(stream); - AliDebug(3, Form("output stream set to file %s for all types", - stream.Data())); + AliDebug(3, Form("output stream set to file %s for all types", stream.Data())); } } // individual output streams - for (Int_t iType = kFatal; iType < kMaxType; iType++) { + for (Int_t iType = kFatal; iType < kMaxType; iType++) + { TString name("AliRoot.AliLog.Output."); name += &typeNames[iType][1]; - if (gEnv->Defined(name)) { + + if (gEnv->Defined(name)) + { TString stream = gEnv->GetValue(name, "Standard"); - if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) { + + if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) + { SetStandardOutput(EType_t(iType)); - AliDebug(3, Form("output stream set to standard output for type %s", - typeNames[iType])); - } else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) { + AliDebug(3, Form("output stream set to standard output for type %s", typeNames[iType])); + } + else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) + { SetErrorOutput(EType_t(iType)); - AliDebug(3, Form("output stream set to error output for type %s", - typeNames[iType])); - } else if (!stream.IsNull()) { + AliDebug(3, Form("output stream set to error output for type %s", typeNames[iType])); + } + else if (!stream.IsNull()) + { SetFileOutput(EType_t(iType), stream); - AliDebug(3, Form("output stream set to file %s for type %s", - stream.Data(), typeNames[iType])); + AliDebug(3, Form("output stream set to file %s for type %s", stream.Data(), typeNames[iType])); } } } // handling of root error messages - if (gEnv->Defined("AliRoot.AliLog.HandleRootMessages")) { + if (gEnv->Defined("AliRoot.AliLog.HandleRootMessages")) + { Bool_t on = gEnv->GetValue("AliRoot.AliLog.HandleRootMessages", kTRUE); SetHandleRootMessages(on); - AliDebug(3, Form("handling of root messages %sabled", - ((on) ? "en" : "dis"))); + AliDebug(3, Form("handling of root messages %sabled", ((on) ? "en" : "dis"))); } // printout settings - static const char* settingNames[4] = - {"Type", "Module", "Scope", "Location"}; - Bool_t* settings[] = - {fPrintType, fPrintModule, fPrintScope, fPrintLocation}; - for (Int_t iSetting = 0; iSetting < 4; iSetting++) { + static const char* settingNames[4] = {"Type", "Module", "Scope", "Location"}; + Bool_t* settings[] = {fPrintType, fPrintModule, fPrintScope, fPrintLocation}; + + for (Int_t iSetting = 0; iSetting < 4; iSetting++) + { TString name("AliRoot.AliLog.Print"); name += settingNames[iSetting]; - if (gEnv->Defined(name)) { + + if (gEnv->Defined(name)) + { Bool_t on = gEnv->GetValue(name, settings[iSetting][0]); - for (Int_t iType = kFatal; iType < kMaxType; iType++) { + + for (Int_t iType = kFatal; iType < kMaxType; iType++) + { settings[iSetting][iType] = on; } - AliDebug(3, Form("printing of %s %sabled for all types", - settingNames[iSetting], ((on) ? "en" : "dis"))); + AliDebug(3, Form("printing of %s %sabled for all types", settingNames[iSetting], ((on) ? "en" : "dis"))); } - for (Int_t iType = kFatal; iType < kMaxType; iType++) { + for (Int_t iType = kFatal; iType < kMaxType; iType++) + { TString nameType = name + "." + &typeNames[iType][1]; - if (gEnv->Defined(nameType)) { + + if (gEnv->Defined(nameType)) + { Bool_t on = gEnv->GetValue(nameType, settings[iSetting][iType]); settings[iSetting][iType] = on; - AliDebug(3, Form("printing of %s %sabled for type %s", - settingNames[iSetting], ((on) ? "en" : "dis"), - typeNames[iType])); + AliDebug(3, Form("printing of %s %sabled for type %s", settingNames[iSetting], ((on) ? "en" : "dis"), typeNames[iType])); } } } // repetition of messages - if (gEnv->Defined("AliRoot.AliLog.PrintRepetitions")) { + if (gEnv->Defined("AliRoot.AliLog.PrintRepetitions")) + { Bool_t on = gEnv->GetValue("AliRoot.AliLog.PrintRepetitions", kTRUE); fPrintRepetitions = on; - AliDebug(3, Form("printing of message repetitions %sabled", - ((on) ? "en" : "dis"))); + AliDebug(3, Form("printing of message repetitions %sabled", ((on) ? "en" : "dis"))); } } @@ -307,7 +380,8 @@ void AliLog::RootErrorHandler(Int_t level, Bool_t abort, { // new error handler for messages from root - switch (level) { + switch (level) + { case ::kFatal : level = kFatal; break; case ::kSysError : DefaultErrorHandler(level, abort, location, message); @@ -324,6 +398,7 @@ void AliLog::RootErrorHandler(Int_t level, Bool_t abort, } +// DEPRECATED: USE A CONFIGURATION FILE INSTEAD //_____________________________________________________________________________ void AliLog::EnableDebug(Bool_t enabled) { @@ -337,6 +412,7 @@ void AliLog::SetGlobalLogLevel(EType_t type) { // set the global debug level + // TO BE DELETED if (!fgInstance) new AliLog; fgInstance->fGlobalLogLevel = type; } diff --git a/STEER/AliLog.h b/STEER/AliLog.h index 14d689d9dfe..d9056d0e2b0 100644 --- a/STEER/AliLog.h +++ b/STEER/AliLog.h @@ -5,25 +5,45 @@ /* $Id$ */ -/// -/// class for logging debug, info and error messages -/// - #include #include #include +// deprecation macro +#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) +#define ALIROOT_DEPRECATED(func) func __attribute__ ((deprecated)) +#elif defined(_MSC_VER) && _MSC_VER >= 1300 +#define ALIROOT_DEPRECATED(func) __declspec(deprecated) func +# else +#define ALIROOT_DEPRECATED(func) func +#endif -class AliLog: public TObject { +/** + * class for logging debug, info and error messages + */ +class AliLog: public TObject +{ public: - AliLog(); - virtual ~AliLog(); - static AliLog* Instance() {return fgInstance;} + // Log4j log levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL enum EType_t {kFatal = 0, kError, kWarning, kInfo, kDebug, kMaxType}; typedef void (*AliLogNotification)(EType_t type, const char* message ); + // NB: singleton constructor & destructor should not be public! + // ALIROOT_DEPRECATED(AliLog()); + // ALIROOT_DEPRECATED(virtual ~AliLog()); + + // NB: singleton deprecated static instance method + // ALIROOT_DEPRECATED(static AliLog* Instance() {return fgInstance;};) + + // get root logger singleton instance + static AliLog *GetRootLogger(); + // delete root logger singleton instance + static void DeleteRootLogger(); + + // NB: the following functions should not be static + // NB: deprecated: logging configuration should be made through to a configuration file static void EnableDebug(Bool_t enabled); static void SetGlobalLogLevel(EType_t type); static Int_t GetGlobalLogLevel(); @@ -86,6 +106,12 @@ class AliLog: public TObject { const char* function, const char* file, Int_t line); private: + + // constructor is made private for implementing a singleton + AliLog(); + virtual ~AliLog(); + + // NOT IMPLEMENTED? AliLog(const AliLog& log); AliLog& operator = (const AliLog& log); @@ -118,7 +144,6 @@ class AliLog: public TObject { enum {kDebugOffset = kDebug-1}; static AliLog* fgInstance; //! pointer to current instance - static Bool_t fgDebugEnabled; // flag for debug en-/disabling UInt_t fGlobalLogLevel; // global logging level @@ -151,14 +176,14 @@ class AliLog: public TObject { }; -// module name +// module name macro #ifdef _MODULE_ #define MODULENAME() _MODULE_ #else #define MODULENAME() "NoModule" #endif -// function name +// function name macro #if defined(__GNUC__) || defined(__ICC) || defined(__ECC) || defined(__APPLE__) #define FUNCTIONNAME() __FUNCTION__ // #elif defined(__HP_aCC) || defined(__alpha) || defined(__DECCXX) @@ -194,7 +219,7 @@ class AliLog: public TObject { // inspired by log4cxx code (see log4cxx/Logger.h) // implements GCC branch prediction for increasing logging performance #if !defined(ALIROOT_UNLIKELY) -#if __GNUC__ >= 3 +#if defined(__GNUC__) && (__GNUC__ >= 3) /** Provides optimization hint to the compiler to optimize for the expression being false. @@ -296,7 +321,6 @@ Logs a message to a specified logger with the DEBUG level. #define AliInfoClassStream() AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__) #define AliInfoGeneralStream(scope) AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__) - // warning messages #ifdef LOG_NO_WARNING #define AliWarning(message) -- 2.39.3