]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTLogging.cxx
coverity 17742 fixed
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.cxx
index d2121e2a9d92030e4b0e5ede6f5dd270b67a728f..ed7fc8cbe152fef400ceef9054dae512ea632b5c 100644 (file)
@@ -1,11 +1,11 @@
 // @(#) $Id$
 
 /**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ * This file is property of and copyright by the ALICE HLT Project        * 
+ * ALICE Experiment at CERN, All rights reserved.                         *
  *                                                                        *
- * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
- *          Timm Steinbeck <timm@kip.uni-heidelberg.de>                   *
- *          for The ALICE Off-line Project.                               *
+ * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
+ *                  for The ALICE HLT Project.                            *
  *                                                                        *
  * Permission to use, copy, modify and distribute this software and its   *
  * documentation strictly for non-commercial purposes is hereby granted   *
 using namespace std;
 #endif
 
-#ifndef NOALIROOT_LOGGING
-#include "AliLog.h"
-#endif
 #include "AliHLTStdIncludes.h"
 #include "AliHLTLogging.h"
+#include "AliHLTComponentHandler.h"
 #include "TString.h"
-#include "TArrayC.h"
 #include "Varargs.h"
 #include <string>
 #include <sstream>
 #include <iostream>
 
-/** target stream for AliRoot logging methods */
-ostringstream gLogstr;
-
-#ifndef NOALIROOT_LOGGING
-/**
- * Notification callback for AliRoot logging methods
- */
-void LogNotification(AliLog::EType_t level, const char* message)
-{
-  AliHLTLogging hltlog;
-  hltlog.SwitchAliLog(0);
-  hltlog.Logging(kHLTLogInfo, "NotificationHandler", "AliLog", gLogstr.str().c_str());
-  gLogstr.clear();
-  string empty("");
-  gLogstr.str(empty);
-}
-#endif
-
-/**
- * The global logging buffer.
- * The buffer is created with an initial size and grown dynamically on
- * demand.
- */
-TArrayC gAliHLTLoggingTarget(200);
-
-/** the maximum size of the buffer */
-const int gALIHLTLOGGING_MAXBUFFERSIZE=10000;
-
 /** ROOT macro for the implementation of ROOT specific class methods */
-ClassImp(AliHLTLogging)
+ClassImp(AliHLTLogging);
 
 AliHLTLogging::AliHLTLogging()
   :
-  //fLocalLogFilter(kHLTLogDefault),
-  fLocalLogFilter(kHLTLogAll),
+  fLocalLogFilter(fgLocalLogDefault),
   fpDefaultKeyword(NULL),
   fpCurrentKeyword(NULL)
 {
@@ -100,35 +68,99 @@ AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
   return *this;
 }
 
-AliHLTComponentLogSeverity AliHLTLogging::fGlobalLogFilter=kHLTLogAll;
-AliHLTfctLogging AliHLTLogging::fLoggingFunc=NULL;
+ostringstream AliHLTLogging::fgLogstr;
+AliHLTComponentLogSeverity AliHLTLogging::fgGlobalLogFilter=kHLTLogAll;
+AliHLTComponentLogSeverity AliHLTLogging::fgLocalLogDefault=kHLTLogAll;
+AliHLTfctLogging AliHLTLogging::fgLoggingFunc=NULL;
+AliHLTLogging::AliHLTDynamicMessage AliHLTLogging::fgAliLoggingFunc=NULL;
 int AliHLTLogging::fgUseAliLog=1;
 
+TString AliHLTLogging::fgBlackList="";
+TString AliHLTLogging::fgWhiteList="";
+
 AliHLTLogging::~AliHLTLogging()
 {
   // see header file for class documentation
 }
 
+// the array will be grown dynamically, this is just an initial size 
+TArrayC AliHLTLogging::fgAliHLTLoggingTarget(200);
+// the maximum size of the array
+const int AliHLTLogging::fgkALIHLTLOGGINGMAXBUFFERSIZE=10000;
+
 int AliHLTLogging::Init(AliHLTfctLogging pFun) 
 {
   // see header file for class documentation
-  if (fLoggingFunc!=NULL && fLoggingFunc!=pFun) {
-    (*fLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");    
+  if (fgLoggingFunc!=NULL && fgLoggingFunc!=pFun) {
+    (*fgLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");    
   }
-  fLoggingFunc=pFun;
-  // older versions of AliLog does not support the notification callback and
-  // stringstreams, but they support the logging macros in general
-#ifndef NOALIROOT_LOGGING
-#ifndef NO_ALILOG_NOTIFICATION
-  AliLog* log=new AliLog;
-  log->SetLogNotification(LogNotification);
-  log->SetStreamOutput(&gLogstr);
-#endif // NO_ALILOG_NOTIFICATION
-#endif // NOALIROOT_LOGGING
+  fgLoggingFunc=pFun;
   
   return 0;
 }
 
+int AliHLTLogging::InitAliLogTrap(AliHLTComponentHandler* pHandler)
+{
+  // see header file for class documentation
+  // init the AliRoot logging trap
+  // AliLog messages are redirected to PubSub,
+  int iResult=0;
+  if (pHandler) {
+    // set temporary loglevel of component handler
+    AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
+    pHandler->SetLocalLoggingLevel(kHLTLogError);
+
+    // load library containing AliRoot dependencies and initialization handler
+    pHandler->LoadLibrary(ALILOG_WRAPPER_LIBRARY, 0/* do not activate agents */);
+
+    // restore loglevel
+    pHandler->SetLocalLoggingLevel(loglevel);
+
+    // find the symbol
+    InitAliDynamicMessageCallback pFunc=(InitAliDynamicMessageCallback)pHandler->FindSymbol(ALILOG_WRAPPER_LIBRARY, "InitAliDynamicMessageCallback"); 
+    if (pFunc) {
+      iResult=(*pFunc)();
+    } else {
+      Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogTrap", "init logging",
+             "can not initialize AliLog callback");
+      iResult=-ENOSYS;
+    }
+  } else {
+    iResult=-EINVAL;
+  }
+  
+  return iResult;
+}
+
+int AliHLTLogging::InitAliLogFunc(AliHLTComponentHandler* pHandler)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (pHandler) {
+    // set temporary loglevel of component handler
+    AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
+    pHandler->SetLocalLoggingLevel(kHLTLogError);
+
+    // load library containing AliRoot dependencies and initialization handler
+    pHandler->LoadLibrary(ALILOG_WRAPPER_LIBRARY, 0/* do not activate agents */);
+
+    // restore loglevel
+    pHandler->SetLocalLoggingLevel(loglevel);
+
+    // find the symbol
+    fgAliLoggingFunc=(AliHLTLogging::AliHLTDynamicMessage)pHandler->FindSymbol(ALILOG_WRAPPER_LIBRARY, "AliDynamicMessage");
+    if (fgAliLoggingFunc==NULL) {
+      Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogFunc", "init logging",
+             "symbol lookup failure: can not find AliDynamicMessage, switching to HLT logging system");
+      iResult=-ENOSYS;
+    }
+  } else {
+    iResult=-EINVAL;
+  }
+  
+  return iResult;
+}
+
 int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
                           const char* origin, const char* keyword,
                           const char* message) 
@@ -159,12 +191,15 @@ int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
   case kHLTLogFatal:
     strSeverity="fatal";
     break;
+  case kHLTLogImportant:
+    strSeverity="notify";
+    break;
   default:
     break;
   }
   TString out="HLT Log ";
   out+=strSeverity;
-  if (origin) {out+=": "; out+=origin;}
+  if (origin && origin[0]!=0) {out+=": <"; out+=origin; out+="> ";}
   out+=" "; out+=message;
   if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
     out+=" ("; out+=keyword; out +=")";
@@ -173,31 +208,31 @@ int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
   return iResult;
 }
 
-#ifndef NOALIROOT_LOGGING
+#if 0
 int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity, 
-                             const char* origin_class, const char* origin_func,
+                             const char* originClass, const char* originFunc,
                              const char* file, int line, const char* message) 
 {
   // see header file for class documentation
 
   switch (severity) {
   case kHLTLogBenchmark: 
-    AliLog::Message(AliLog::kInfo, message, "HLT", origin_class, origin_func, file, line);
+    AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
     break;
   case kHLTLogDebug:
-    AliLog::Message(AliLog::kDebug, message, "HLT", origin_class, origin_func, file, line);
+    AliLog::Message(AliLog::kDebug, message, "HLT", originClass, originFunc, file, line);
     break;
   case kHLTLogInfo:
-    AliLog::Message(AliLog::kInfo, message, "HLT", origin_class, origin_func, file, line);
+    AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
     break;
   case kHLTLogWarning:
-    AliLog::Message(AliLog::kWarning, message, "HLT", origin_class, origin_func, file, line);
+    AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
     break;
   case kHLTLogError:
-    AliLog::Message(AliLog::kError, message, "HLT", origin_class, origin_func, file, line);
+    AliLog::Message(AliLog::kError, message, "HLT", originClass, originFunc, file, line);
     break;
   case kHLTLogFatal:
-    AliLog::Message(AliLog::kWarning, message, "HLT", origin_class, origin_func, file, line);
+    AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
     break;
   default:
     break;
@@ -206,46 +241,84 @@ int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity,
 }
 #endif
 
-const char* AliHLTLogging::BuildLogString(const char *format, va_list ap
+const char* AliHLTLogging::BuildLogString(const char *format, va_list &ap, bool bAppend
 {
   // see header file for class documentation
 
   int iResult=0;
+#ifdef R__VA_COPY
   va_list bap;
   R__VA_COPY(bap, ap);
+#endif //R__VA_COPY
 
   // take the first argument from the list as format string if no
   // format was given
   const char* fmt = format;
   if (fmt==NULL) fmt=va_arg(ap, const char*);
 
-  gAliHLTLoggingTarget[0]=0;
+  unsigned int iOffset=0;
+  if (bAppend) {
+    iOffset=strlen(fgAliHLTLoggingTarget.GetArray());
+  } else {
+    fgAliHLTLoggingTarget[0]=0;
+  }
   while (fmt!=NULL) {
-    iResult=vsnprintf(gAliHLTLoggingTarget.GetArray(), gAliHLTLoggingTarget.GetSize(), fmt, ap);
+    iResult=vsnprintf(fgAliHLTLoggingTarget.GetArray()+iOffset, fgAliHLTLoggingTarget.GetSize()-iOffset, fmt, ap);
     if (iResult==-1)
       // for compatibility with older version of vsnprintf
-      iResult=gAliHLTLoggingTarget.GetSize()*2;
-    else if (iResult<gAliHLTLoggingTarget.GetSize())
+      iResult=fgAliHLTLoggingTarget.GetSize()*2;
+    else
+      iResult+=iOffset;
+
+    if (iResult<fgAliHLTLoggingTarget.GetSize())
+      // everything in the limit
       break;
 
     // terminate if buffer is already at the limit
-    if (gAliHLTLoggingTarget.GetSize()>=gALIHLTLOGGING_MAXBUFFERSIZE) {
-      gAliHLTLoggingTarget[gAliHLTLoggingTarget.GetSize()-1]=0;
+    if (fgAliHLTLoggingTarget.GetSize()>=fgkALIHLTLOGGINGMAXBUFFERSIZE) 
+    {
+      fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
       break;
     }
 
     // check limitation and grow the buffer
-    if (iResult>gALIHLTLOGGING_MAXBUFFERSIZE) iResult=gALIHLTLOGGING_MAXBUFFERSIZE;
-    gAliHLTLoggingTarget.Set(iResult+1);
+    if (iResult>fgkALIHLTLOGGINGMAXBUFFERSIZE) iResult=fgkALIHLTLOGGINGMAXBUFFERSIZE;
+    fgAliHLTLoggingTarget.Set(iResult+1);
 
     // copy the original list and skip the first argument if this was the format string
+#ifdef R__VA_COPY
     va_end(ap);
     R__VA_COPY(ap, bap);
+#else
+    fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
+    break;
+#endif //R__VA_COPY
     if (format==NULL) va_arg(ap, const char*);
   }     
+#ifdef R__VA_COPY
   va_end(bap);
+#endif //R__VA_COPY
 
-  return gAliHLTLoggingTarget.GetArray();
+  return fgAliHLTLoggingTarget.GetArray();
+}
+
+const char* AliHLTLogging::SetLogString(const void* p, const char* pfmt, const char *format, ...)
+{
+  // see header file for class documentation
+  if (!p || !pfmt) return NULL;
+  TString formatstr=format;
+  TString pstr;
+#ifdef __DEBUG
+  pstr.Form(pfmt, p);
+#endif
+  formatstr.ReplaceAll("_pfmt_", pstr);
+  va_list args;
+  va_start(args, format);
+
+  const char* message=BuildLogString(formatstr.Data(), args);
+  va_end(args);
+
+  return message;
 }
 
 int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
@@ -257,14 +330,12 @@ int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
   if (iResult>0) {
     va_list args;
     va_start(args, format);
-    if (fLoggingFunc) {
-      iResult = (*fLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
+    if (fgLoggingFunc) {
+      iResult = (*fgLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
     } else {
-#ifndef NOALIROOT_LOGGING
-      if (fgUseAliLog)
-       iResult=AliMessage(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
+      if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
+       iResult=(*fgAliLoggingFunc)(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
       else
-#endif
         iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
     }
     va_end(args);
@@ -273,40 +344,46 @@ int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
 }
 
 int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity, 
-                                 const char* origin_class, const char* origin_func,
+                                 const char* originClass, const char* originFunc,
                                  const char* file, int line,  ... ) const
 {
   // see header file for class documentation
+  int iResult=0;
 
-  if (file==NULL && line==0) {
-    // this is currently just to get rid of the warning "unused parameter"
+  va_list args;
+  va_start(args, line);
+
+  iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args ));
+  va_end(args);
+
+  return iResult;
+}
+
+int AliHLTLogging::SendMessage(AliHLTComponentLogSeverity severity, 
+                              const char* originClass, const char* originFunc,
+                              const char* file, int line,
+                              const char* message) const
+{
+  // see header file for class documentation
+  int iResult=0;
+  const char* separator="";
+  TString origin;
+  if (originClass) {
+    origin+=originClass;
+    separator="::";
+  }
+  if (originFunc) {
+    origin+=separator;
+    origin+=originFunc;
   }
-  int iResult=CheckFilter(severity);
-  if (iResult>0) {
-    const char* separator="";
-    TString origin;
-    if (origin_class) {
-       origin+=origin_class;
-       separator="::";
-    }
-    if (origin_func) {
-       origin+=separator;
-       origin+=origin_func;
-    }
-    va_list args;
-    va_start(args, line);
 
-    if (fLoggingFunc) {
-      iResult=(*fLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
-    } else {
-#ifndef NOALIROOT_LOGGING
-      if (fgUseAliLog)
-       iResult=AliMessage(severity, origin_class, origin_func, file, line, AliHLTLogging::BuildLogString(NULL, args ));
-      else
-#endif
-       iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
-    }
-    va_end(args);
+  if (fgLoggingFunc) {
+    iResult=(*fgLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), message);
+  } else {
+    if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
+      iResult=(*fgAliLoggingFunc)(severity, originClass, originFunc, file, line, message);
+    else
+      iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), message);
   }
   return iResult;
 }
@@ -315,7 +392,7 @@ int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
 {
   // see header file for class documentation
 
-  int iResult=severity==kHLTLogNone || (severity&fGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
+  int iResult=severity==kHLTLogNone || ((severity&fgGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0);
   return iResult;
 }
 
@@ -323,7 +400,14 @@ void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
 {
   // see header file for class documentation
 
-  fGlobalLogFilter=level;
+  fgGlobalLogFilter=level;
+}
+
+AliHLTComponentLogSeverity AliHLTLogging::GetGlobalLoggingLevel()
+{
+  // see header file for class documentation
+
+  return fgGlobalLogFilter;
 }
 
 void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
@@ -332,3 +416,42 @@ void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
 
   fLocalLogFilter=level;
 }
+
+
+AliHLTComponentLogSeverity AliHLTLogging::GetLocalLoggingLevel()
+{
+  // see header file for class documentation
+
+  return fLocalLogFilter;
+}
+
+void AliHLTLogging::SetLocalLoggingDefault(AliHLTComponentLogSeverity level)
+{
+  // see header file for class documentation
+  fgLocalLogDefault=level;
+}
+
+int AliHLTLogging::CheckGroup(const char* /*originClass*/) const
+{
+  // see header file for class documentation
+
+  return 1;
+}
+
+int AliHLTLogging::SetBlackList(const char* classnames)
+{
+  // see header file for class documentation
+
+  if (classnames)
+    fgBlackList=classnames;
+  return 0;
+}
+
+int AliHLTLogging::SetWhiteList(const char* classnames)
+{
+  // see header file for class documentation
+
+  if (classnames)
+    fgWhiteList=classnames;
+  return 0;
+}