BCLog.cxx
1 /*
2  * Copyright (C) 2007-2018, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 // ---------------------------------------------------------
10 
11 #include "config.h"
12 
13 #include "BCLog.h"
14 
15 #include <TError.h>
16 #include <TROOT.h>
17 
18 #include <iostream>
19 #include <iomanip>
20 
21 
22 std::ofstream BCLog::fOutputStream;
23 
24 BCLog::LogLevel BCLog::fMinimumLogLevelFile = BCLog::debug;
25 
26 BCLog::LogLevel BCLog::fMinimumLogLevelScreen = BCLog::summary;
27 
28 bool BCLog::fFirstOutputDone = false;
29 
30 bool BCLog::fPrefix = true;
31 
32 std::string BCLog::fVersion = VERSION;
33 
34 // ---------------------------------------------------------
35 
37 {
38  // suppress the ROOT Info printouts
39  gErrorIgnoreLevel = 2000;
40 }
41 
42 
43 // ---------------------------------------------------------
44 
45 void BCLog::OpenLog(const std::string& filename, BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen)
46 {
47  // suppress the ROOT Info printouts
48  gErrorIgnoreLevel = 2000;
49 
50  // first close and flush and existing log file
52 
53  // open log file
54  BCLog::fOutputStream.open(filename.data());
55 
56  if (!BCLog::fOutputStream.is_open()) {
57  std::cerr << " Could not open log file " << filename << ". " << std::endl;
58  return;
59  }
60 
61  // set log level
62  BCLog::SetLogLevelFile(loglevelfile);
63  BCLog::SetLogLevelScreen(loglevelscreen);
64 
65  BCLog::Out(BCLog::summary, BCLog::summary, "Opening logfile " + filename);
66 }
67 
68 // ---------------------------------------------------------
69 
70 void BCLog::Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const std::string& message)
71 {
72  // if this is the first call to Out(), call StartupInfo() first
73  if (!fFirstOutputDone)
75 
76  // open log file if not opened
77  if (BCLog::IsOpen()) {
78  // write message in to log file
79  if (loglevelfile >= BCLog::fMinimumLogLevelFile)
80  BCLog::fOutputStream << BCLog::ToString(loglevelfile) << message << std::endl;
81  }
82 
83  // write message to screen
84  if (loglevelscreen >= BCLog::fMinimumLogLevelScreen)
85  std::cout << BCLog::ToString(loglevelscreen) << message << std::endl;
86 }
87 
88 // ---------------------------------------------------------
89 
91 {
92  const char* message = Form(
93  " +------------------------------------------------------+\n"
94  " | |\n"
95  " | BAT version %-12s |\n"
96  " | Copyright (C) 2007-2018, the BAT core developer team |\n"
97  " | All rights reserved. |\n"
98  " | |\n"
99  " | For the licensing terms see doc/COPYING |\n"
100  " | For documentation see http://mpp.mpg.de/bat |\n"
101  " | Please cite: DOI 10.1016/j.cpc.2009.06.026 |\n"
102  " | http://arxiv.org/abs/0808.2552 |\n"
103  " | |\n"
104  " +------------------------------------------------------+\n",
105  BCLog::fVersion.data());
106 
107  // write message to screen
108  if (BCLog::fMinimumLogLevelScreen < BCLog::nothing)
109  std::cout << message << std::endl;
110 
111  if (BCLog::IsOpen() && BCLog::fMinimumLogLevelFile < BCLog::nothing)
112  BCLog::fOutputStream << message;
113 
114  fFirstOutputDone = true;
115 }
116 
117 // ---------------------------------------------------------
118 
119 std::string BCLog::ToString(BCLog::LogLevel loglevel)
120 {
121  if (!fPrefix)
122  return "";
123 
124  switch (loglevel) {
125  case debug:
126  return "Debug : ";
127  case detail:
128  return "Detail : ";
129  case summary:
130  return "Summary : ";
131  case warning:
132  return "Warning : ";
133  case error:
134  return "Error : ";
135  default:
136  return "";
137  }
138 }
Print everything, including debug info.
Definition: BCLog.h:60
static void CloseLog()
Closes the log file.
Definition: BCLog.h:149
Print only results summary, warnings, and errors.
Definition: BCLog.h:62
static void Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const std::string &message)
Writes string to the file and screen log if the log level is equal or greater than the minimum...
Definition: BCLog.cxx:70
LogLevel
Enumerator for the amount of details to put into the log file.
Definition: BCLog.h:59
Print all details of operation.
Definition: BCLog.h:61
static void SetLogLevelFile(BCLog::LogLevel loglevel)
Sets the minimum log level for file output.
Definition: BCLog.h:104
BCLog()
Constructor.
Definition: BCLog.cxx:36
static void StartupInfo()
Writes startup information onto screen and into a logfile.
Definition: BCLog.cxx:90
static bool IsOpen()
Definition: BCLog.h:144
Print nothing.
Definition: BCLog.h:65
static void SetLogLevelScreen(BCLog::LogLevel loglevel)
Sets the minimum log level for screen output.
Definition: BCLog.h:110
static void OpenLog(const std::string &filename="log.txt", BCLog::LogLevel loglevelfile=BCLog::debug, BCLog::LogLevel loglevelscreen=BCLog::summary)
Opens log file and sets minimum log levels for file and screen output.
Definition: BCLog.cxx:45
Print only warnings and errors.
Definition: BCLog.h:63
Print only errors.
Definition: BCLog.h:64
static std::string ToString(BCLog::LogLevel)
Converts a log level to a string.
Definition: BCLog.cxx:119