• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCLog.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2010, Daniel Kollar and Kevin Kroeninger.
00003  * All rights reserved.
00004  *
00005  * For the licensing terms see doc/COPYING.
00006  */
00007 
00008 // ---------------------------------------------------------
00009 
00010 #include <fstream>
00011 
00012 #include <TROOT.h>
00013 #include <TError.h>
00014 
00015 #include "config.h"
00016 
00017 #include "BAT/BCLog.h"
00018 
00019 std::ofstream BCLog::fOutputStream;
00020 
00021 BCLog::LogLevel BCLog::fMinimumLogLevelFile = BCLog::debug;
00022 
00023 BCLog::LogLevel BCLog::fMinimumLogLevelScreen = BCLog::summary;
00024 
00025 bool BCLog::fFirstOutputDone = false;
00026 
00027 const char * BCLog::fVersion = VERSION;
00028 
00029 int BCLog::fHindex = 0;
00030 
00031 // ---------------------------------------------------------
00032 
00033 BCLog::BCLog()
00034 {
00035    // suppress the ROOT Info printouts
00036    gErrorIgnoreLevel=2000;
00037 }
00038 
00039 // ---------------------------------------------------------
00040 
00041 BCLog::~BCLog()
00042 {}
00043 
00044 // ---------------------------------------------------------
00045 
00046 void BCLog::OpenLog(const char * filename, BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen)
00047 {
00048    // suppress the ROOT Info printouts
00049    gErrorIgnoreLevel=2000;
00050 
00051    // open log file
00052    BCLog::fOutputStream.open(filename);
00053 
00054    if (!BCLog::fOutputStream.is_open())
00055    {
00056       std::cerr << " Could not open log file " << filename << ". " << std::endl;
00057       return;
00058    }
00059 
00060    // set log level
00061    BCLog::SetLogLevelFile(loglevelfile);
00062    BCLog::SetLogLevelScreen(loglevelscreen);
00063 
00064    BCLog::Out(BCLog::summary,BCLog::summary,Form("Opening logfile %s",filename));
00065 }
00066 
00067 // ---------------------------------------------------------
00068 
00069 void BCLog::OpenLog(const char * filename)
00070 {
00071    BCLog::OpenLog(filename, BCLog::debug, BCLog::summary);
00072 }
00073 
00074 // ---------------------------------------------------------
00075 
00076 void BCLog::OpenLog()
00077 {
00078    BCLog::OpenLog("log.txt", BCLog::debug, BCLog::summary);
00079 }
00080 
00081 // ---------------------------------------------------------
00082 
00083 bool BCLog::IsOpen()
00084 {
00085    return BCLog::fOutputStream.is_open();
00086 }
00087 
00088 // ---------------------------------------------------------
00089 
00090 void BCLog::CloseLog()
00091 {
00092    BCLog::fOutputStream.close();
00093 }
00094 
00095 // ---------------------------------------------------------
00096 
00097 void BCLog::Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const char * message)
00098 {
00099    // if this is the first call to Out(), call StartupInfo() first
00100    if(!fFirstOutputDone)
00101       BCLog::StartupInfo();
00102 
00103    // open log file if not opened
00104    if (BCLog::IsOpen())
00105    {
00106       // write message in to log file
00107       if (loglevelfile >= BCLog::fMinimumLogLevelFile)
00108          BCLog::fOutputStream << BCLog::ToString(loglevelfile) << " : " << message << std::endl;
00109    }
00110 
00111    // write message to screen
00112    if (loglevelscreen >= BCLog::fMinimumLogLevelScreen)
00113       std::cout << BCLog::ToString(loglevelscreen) << " : " << message << std::endl;
00114 }
00115 
00116 // ---------------------------------------------------------
00117 
00118 void BCLog::Out(const char * message)
00119 {
00120    BCLog::Out(BCLog::fMinimumLogLevelFile, BCLog::fMinimumLogLevelScreen, message);
00121 }
00122 
00123 // ---------------------------------------------------------
00124 
00125 void BCLog::StartupInfo()
00126 {
00127    char * message = Form(
00128          " +------------------------------\n"
00129          " |\n"
00130          " |     Running with BAT\n"
00131          " |      Version %s\n"
00132          " |\n"
00133          " | http://www.mppmu.mpg.de/bat\n"
00134          " +------------------------------\n",
00135          BCLog::fVersion);
00136 
00137    if (BCLog::IsOpen() && BCLog::fMinimumLogLevelFile<BCLog::nothing)
00138       BCLog::fOutputStream << message;
00139 
00140    if (BCLog::fMinimumLogLevelScreen<BCLog::nothing)
00141       std::cout << message;
00142 
00143    fFirstOutputDone = true;
00144 }
00145 
00146 // ---------------------------------------------------------
00147 
00148 const char * BCLog::ToString(BCLog::LogLevel loglevel)
00149 {
00150    switch (loglevel)
00151    {
00152       case debug:
00153          return "Debug  ";
00154       case detail:
00155          return "Detail ";
00156       case summary:
00157          return "Summary";
00158       case warning:
00159          return "Warning";
00160       case error:
00161          return "Error  ";
00162       default:
00163          return "";
00164    }
00165 }
00166 
00167 // ---------------------------------------------------------

Generated on Mon Aug 30 2010 22:14:54 for Bayesian Analysis Toolkit by  doxygen 1.7.1