Project
Loading...
Searching...
No Matches
FairLogger.cxx
Go to the documentation of this file.
1/********************************************************************************
2 * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3 * *
4 * This software is distributed under the terms of the *
5 * GNU Lesser General Public Licence (LGPL) version 3, *
6 * copied verbatim in the file "LICENSE" *
7 ********************************************************************************/
8/*
9 * FairLogger.cxx
10 *
11 * Created on: Mar 03, 2011
12 * Author: f.uhlig
13 */
14
15#include "FairLogger.h"
16
17#include <TString.h> // for TString, operator==, etc
18#include <TSystem.h> // for gSystem, TSystem
19#include <iostream> // for cout, cerr
20#include <stdio.h> // for fclose, freopen, remove, etc
21
22FairLogger* FairLogger::instance = nullptr;
23
24const char FairLogger::endl = ' ';
25const char FairLogger::flush = ' ';
26
27FairLogger::FairLogger()
28 : fLogFileName()
29 , fLogFileSeverity("info")
30 , fConsoleSeverity("info")
31 , fBufferSize(1024)
32 , fBufferSizeNeeded(-1)
33 , fDynamicBuffer(fBufferSize)
34 , fBufferPointer(&fDynamicBuffer[0])
35{
36 fair::Logger::OnFatal(LogFatalMessage);
37}
38
40{
41 if (!instance) {
42 instance = new FairLogger();
43 }
44
45 return instance;
46}
47
48void FairLogger::Fatal(const char* file, const char* line, const char* func, const char* format, ...)
49{
50 va_list ap;
51 va_start(ap, format);
52 Log(fair::Severity::fatal, file, line, func, format, ap);
53 va_end(ap);
54}
55
56void FairLogger::Error(const char* file, const char* line, const char* func, const char* format, ...)
57{
58 if (IsLogNeeded(fair::Severity::error)) {
59 va_list ap;
60 va_start(ap, format);
61 Log(fair::Severity::error, file, line, func, format, ap);
62 va_end(ap);
63 }
64}
65
66void FairLogger::Warning(const char* file, const char* line, const char* func, const char* format, ...)
67{
68 if (IsLogNeeded(fair::Severity::warning)) {
69 va_list ap;
70 va_start(ap, format);
71 Log(fair::Severity::warning, file, line, func, format, ap);
72 va_end(ap);
73 }
74}
75
76void FairLogger::Info(const char* file, const char* line, const char* func, const char* format, ...)
77{
78 if (IsLogNeeded(fair::Severity::info)) {
79 va_list ap;
80 va_start(ap, format);
81 Log(fair::Severity::info, file, line, func, format, ap);
82 va_end(ap);
83 }
84}
85
86void FairLogger::Debug(const char* file, const char* line, const char* func, const char* format, ...)
87{
88 if (IsLogNeeded(fair::Severity::debug)) {
89 va_list ap;
90 va_start(ap, format);
91 Log(fair::Severity::debug, file, line, func, format, ap);
92 va_end(ap);
93 }
94}
95
96void FairLogger::Debug1(const char* file, const char* line, const char* func, const char* format, ...)
97{
98 if (IsLogNeeded(fair::Severity::debug1)) {
99 va_list ap;
100 va_start(ap, format);
101 Log(fair::Severity::debug1, file, line, func, format, ap);
102 va_end(ap);
103 }
104}
105
106void FairLogger::Debug2(const char* file, const char* line, const char* func, const char* format, ...)
107{
108 if (IsLogNeeded(fair::Severity::debug2)) {
109 va_list ap;
110 va_start(ap, format);
111 Log(fair::Severity::debug2, file, line, func, format, ap);
112 va_end(ap);
113 }
114}
115
116void FairLogger::Debug3(const char* file, const char* line, const char* func, const char* format, ...)
117{
118 if (IsLogNeeded(fair::Severity::debug3)) {
119 va_list ap;
120 va_start(ap, format);
121 Log(fair::Severity::debug3, file, line, func, format, ap);
122 va_end(ap);
123 }
124}
125
126void FairLogger::Debug4(const char* file, const char* line, const char* func, const char* format, ...)
127{
128 if (IsLogNeeded(fair::Severity::debug4)) {
129 va_list ap;
130 va_start(ap, format);
131 Log(fair::Severity::debug4, file, line, func, format, ap);
132 va_end(ap);
133 }
134}
135
136void FairLogger::Log(fair::Severity severity,
137 const char* file,
138 const char* line,
139 const char* func,
140 const char* format,
141 va_list arglist)
142{
143 // If the format string together with the argument list was used once it can't
144 // be used another time. Don't know why but if we do so the arguments are not
145 // correct any longer and the program may crash (On Mac OSX). Even if it does
146 // not crash it does not work as expected.
147 // To vercome the problem the output is written to a buffer which then can be
148 // used several times.
149
150 while (true) {
151 fBufferSizeNeeded = vsnprintf(fBufferPointer, fBufferSize, format, arglist);
152
153 // Try to vsnprintf into our buffer.
154 // int needed = vsnprintf (buf, size, fmt, ap);
155 // NB. C99 (which modern Linux and OS X follow) says vsnprintf
156 // failure returns the length it would have needed. But older
157 // glibc and current Windows return -1 for failure, i.e., not
158 // telling us how much was needed.
159
160 if (fBufferSizeNeeded <= static_cast<int>(fBufferSize) && fBufferSizeNeeded >= 0) {
161 // It fits fine so we're done.
162 LOGD(severity, file, line, func) << std::string(fBufferPointer, static_cast<size_t>(fBufferSizeNeeded));
163 break;
164 }
165
166 // vsnprintf reported that it wanted to write more characters
167 // than we allotted. So try again using a dynamic buffer. This
168 // doesn't happen very often if we chose our initial size well.
169 fBufferSize = (fBufferSizeNeeded > 0) ? (fBufferSizeNeeded + 1) : (fBufferSize * 2);
170 fDynamicBuffer.resize(fBufferSize);
171 }
172}
173
174bool FairLogger::IsLogNeeded(fair::Severity severity) { return fair::Logger::Logging(severity); }
175
176bool FairLogger::IsLogNeeded(FairLogLevel level) { return fair::Logger::Logging(ConvertLogLevelToString(level)); }
177
178void FairLogger::LogFatalMessage()
179{
180 // Since Fatal indicates a fatal error it is maybe usefull to have
181 // system information from the incident. Since the output on the screen
182 // does not help the noraml user, the stderr is redirected into a special
183 // core dump file for later usage.
184 // Fatal also indicates a problem which is so severe that the process should
185 // not go on, so the process is aborted.
186
187 if (gSystem) {
188 TString corefile = "core_dump_";
189 int pid = gSystem->GetPid();
190 corefile += pid;
191
192 std::cerr << "For later analysis we write a core dump to " << corefile << std::endl;
193
194 if (freopen(corefile, "w", stderr)) {
195 gSystem->StackTrace();
196 fclose(stderr);
197 }
198 }
199
200 throw fair::FatalException("Fatal error occured. Exiting...");
201}
202
ClassImp(FairLogger)
FairLogLevel
Definition FairLogger.h:28
uint16_t pid
Definition RawData.h:2
void Debug3(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(debug3) << content
void Debug(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(debug) << content
void Debug1(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(debug1) << content
void Debug2(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(debug2) << content
static FairLogger * GetLogger()
void Error(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(error) << content
bool IsLogNeeded(fair::Severity severity)
void Info(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(info) << content
void Debug4(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(debug4) << content
void Warning(const char *file, const char *line, const char *func, const char *format,...) __attribute__((deprecated("Use 'LOG(warn) << content
GLint level
Definition glcorearb.h:275
GLenum GLenum severity
Definition glcorearb.h:2513
GLint GLint GLsizei GLint GLenum format
Definition glcorearb.h:275