]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/STEERBase/AliLog.cxx
Merge branch 'flatdev' of https://git.cern.ch/reps/AliRoot into flatdev
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliLog.cxx
index 5258eb8d0428302ad60842e9408dc455978b0a18..02701b4a0158000e5acc1ef011eab232dc4a3f49 100644 (file)
 
 #include "AliLog.h"
 
+using std::endl;
+using std::cout;
+using std::ostream;
+using std::cerr;
+using std::ofstream;
+using std::ios;
 ClassImp(AliLog)
 
 // implementation of a singleton here
 AliLog* AliLog::fgInstance = NULL;
 
 Bool_t AliLog::fgDebugEnabled = kTRUE;
+Bool_t AliLog::fgCoreEnabled = kFALSE;
 
 /**
  * get root logger singleton instance
@@ -371,6 +378,10 @@ void AliLog::ReadEnvSettings()
     fPrintRepetitions = on;
     AliDebug(3, Form("printing of message repetitions %sabled", ((on) ? "en" : "dis")));
   }
+  if (gSystem->Getenv("ALIROOT_FORCE_COREDUMP")){
+    EnableCoreDump(kTRUE);
+  }
+
 }
 
 
@@ -407,6 +418,21 @@ void AliLog::EnableDebug(Bool_t enabled)
   fgDebugEnabled = enabled;
 }
 
+void AliLog::EnableCoreDump(Bool_t enabled)
+{
+// enable or disable debug output
+
+  fgCoreEnabled = enabled;
+  gSystem->ResetSignal(kSigFloatingException,enabled);
+  gSystem->ResetSignal(kSigSegmentationViolation,enabled);
+  if (enabled) printf("Core dump enabled\n");
+  else { 
+    printf("Core dump disabled\n");
+  }
+}
+
+
+
 //_____________________________________________________________________________
 void AliLog::SetGlobalLogLevel(EType_t type)
 {
@@ -913,13 +939,17 @@ void AliLog::Message(UInt_t level, const char* message,
     delete fgInstance;
     if (gSystem) {
       gSystem->StackTrace();
+      if (fgCoreEnabled) MakeCoreDump("core.AliRoot");
       gSystem->Abort();
     } else {
+      if (fgCoreEnabled) MakeCoreDump("core.AliRoot");
       ::abort();
     }
   }
 }
 
+
+
 //_____________________________________________________________________________
 void AliLog::Debug(UInt_t level, const char* message, 
                   const char* module, const char* className,
@@ -976,13 +1006,13 @@ Int_t AliLog::RedirectTo(FILE* stream, EType_t type, UInt_t level,
   // redirect stream
   if ((type == kDebug) && (level > 0)) level--;
   if (type + level > GetLogLevel(module, className)) { // /dev/null
-    freopen("/dev/null", "a", stream);
+    if(!freopen("/dev/null", "a", stream)) AliWarning("Cannot reopen /dev/null");
   } else if (fOutputTypes[type] == 0) {         // stdout
     if (stream != stdout) dup2(fileno(stdout), fileno(stream));
   } else if (fOutputTypes[type] == 1) {         // stderr
     if (stream != stderr) dup2(fileno(stderr), fileno(stream));
   } else if (fOutputTypes[type] == 2) {         // file
-    freopen(fFileNames[type], "a", stream);
+    if(!freopen(fFileNames[type], "a", stream)) AliWarning(Form("Cannot reopen %s",fFileNames[type].Data()));
   } else if (fOutputTypes[type] == 3) {         // external C++ stream
     // redirection is not possible for external C++ streams
   }
@@ -1167,3 +1197,29 @@ void  AliLog::PrintString(Int_t type, FILE* stream, const char* format, ...)
   }
   va_end(ap);
 }
+
+
+void AliLog::MakeCoreDump(const char *fout){
+  //
+  // Functionality to make a program snapshot 
+  //   gcore - Generate a core file for a running process 
+  //   gcore dmake a current snapshot, program can continue further
+  //   We assum that gcore is installed
+  //   for details see:  man gcore
+  //
+  // Example use - make default core file for current process:  AliLog::MakeCoreDump(0)
+  //
+  //
+  // Automatic core dump creation in case of the AliFatal can be specified using
+  // static void  EnableCoreDump(Bool_t enabled);
+  // Core dump is created in addition to the stack trace ()  
+  // marian.ivanov@cern.ch
+  //
+  if (!gSystem) return;
+  printf("AliLog::MakeCoreDump\n");
+  if (fout){
+    gSystem->Exec(Form("gcore -o %s  %d",fout, gSystem->GetPid()));
+  }else{
+    gSystem->Exec(Form("gcore   %d", gSystem->GetPid()));
+  }
+}