Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TError.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Fons Rademakers 29/07/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13Error handling routines.
14
15This file defines a number of global error handling routines:
16Warning(), Error(), SysError() and Fatal(). They all take a
17location string (where the error happened) and a printf style format
18string plus vararg's. In the end these functions call an
19errorhandler function. Initially the MinimalErrorHandler, which is supposed
20to be replaced by the proper DefaultErrorHandler()
21*/
22
23#include "TError.h"
24
25#include <cstdarg>
26#include <cstdio>
27#include <cstdlib>
28#include <cerrno>
29#include <string>
30
31// Deprecated
33
34const Int_t kUnset = -1;
35const Int_t kPrint = 0;
36const Int_t kInfo = 1000;
37const Int_t kWarning = 2000;
38const Int_t kError = 3000;
39const Int_t kBreak = 4000;
40const Int_t kSysError = 5000;
41const Int_t kFatal = 6000;
42
46
47const char *kAssertMsg = "%s violated at line %d of `%s'";
48const char *kCheckMsg = "%s not true at line %d of `%s'";
49
51
52
54{
56 return h;
57}
58
59
60namespace ROOT {
61namespace Internal {
62
64{
66}
67
69{
70 auto oldHandler = GetErrorSystemMsgHandlerRef();
72 return oldHandler;
73}
74
75/// A very simple error handler that is usually replaced by the TROOT default error handler.
76/// The minimal error handler is not serialized across threads, so that output of multi-threaded programs
77/// can get scrambled
78/// @note `abort()` is only called if `abort_bool` is `true` and `level >= gErrorIgnoreLevel`
79void MinimalErrorHandler(Int_t level, Bool_t abort_bool, const char *location, const char *msg)
80{
81 if (level < gErrorIgnoreLevel)
82 return;
83
84 if (level >= kBreak)
85 fprintf(stderr, "\n *** Break *** ");
86 fprintf(stderr, "<%s>: %s\n", location ? location : "unspecified location", msg);
87 fflush(stderr);
88 if (abort_bool) {
89 fprintf(stderr, "aborting\n");
90 fflush(stderr);
91 abort();
92 }
93}
94
95} // namespace Internal
96} // namespace ROOT
97
98
99////////////////////////////////////////////////////////////////////////////////
100/// Set an errorhandler function. Returns the old handler.
101
103{
105 gErrorHandler = newhandler;
106 return oldhandler;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Returns the current error handler function.
111
113{
114 return gErrorHandler;
115}
116
117
118////////////////////////////////////////////////////////////////////////////////
119/// General error handler function. It calls the user set error handler.
120
121void ErrorHandler(Int_t level, const char *location, const char *fmt, std::va_list ap)
122{
123 thread_local Int_t buf_size(256);
124 thread_local char *buf_storage(nullptr);
125
126 char small_buf[256];
127 char *buf = buf_storage ? buf_storage : small_buf;
128
129 std::va_list ap_copy;
130 va_copy(ap_copy, ap);
131
132 if (!fmt)
133 fmt = "no error message provided";
134
135 Int_t n = vsnprintf(buf, buf_size, fmt, ap_copy);
136 if (n >= buf_size) {
137 va_end(ap_copy);
138
139 buf_size = n + 1;
140 if (buf != &(small_buf[0]))
141 delete[] buf;
142 buf_storage = buf = new char[buf_size];
143
144 // Try again with a sufficiently large buffer
145 va_copy(ap_copy, ap);
146 vsnprintf(buf, buf_size, fmt, ap_copy);
147 }
148 va_end(ap_copy);
149
150 std::string bp = buf;
151 if (level >= kSysError && level < kFatal) {
152 bp.push_back(' ');
155 else
156 bp += std::string("(errno: ") + std::to_string(errno) + ")";
157 }
158
159 if (level != kFatal)
160 gErrorHandler(level, level >= gErrorAbortLevel, location, bp.c_str());
161 else
162 gErrorHandler(level, kTRUE, location, bp.c_str());
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// This function can be used in abstract base classes in case one does
167/// not want to make the class a "real" (in C++ sense) ABC. If this
168/// function is called it will warn the user that the function should
169/// have been overridden.
170
171void AbstractMethod(const char *method)
172{
173 Warning(method, "this method must be overridden!");
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// This function can be used in classes that should override a certain
178/// function, but in the inherited class the function makes no sense.
179
180void MayNotUse(const char *method)
181{
182 Warning(method, "may not use this method");
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Use this function to declare a function obsolete. Specify as of which version
187/// the method is obsolete and as from which version it will be removed.
188
189void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
190{
191 Warning(function, "obsolete as of %s and will be removed from %s", asOfVers, removedFromVers);
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Use this function in case an error occurred.
196
197void Error(const char *location, const char *fmt, ...)
198{
199 std::va_list ap;
200 va_start(ap, fmt);
201 ErrorHandler(kError, location, fmt, ap);
202 va_end(ap);
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Use this function in case a system (OS or GUI) related error occurred.
207
208void SysError(const char *location, const char *fmt, ...)
209{
210 std::va_list ap;
211 va_start(ap, fmt);
212 ErrorHandler(kSysError, location, fmt, ap);
213 va_end(ap);
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Use this function in case an error occurred.
218
219void Break(const char *location, const char *fmt, ...)
220{
221 std::va_list ap;
222 va_start(ap, fmt);
223 ErrorHandler(kBreak, location, fmt, ap);
224 va_end(ap);
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// Use this function for informational messages.
229
230void Info(const char *location, const char *fmt, ...)
231{
232 std::va_list ap;
233 va_start(ap, fmt);
234 ErrorHandler(kInfo, location, fmt, ap);
235 va_end(ap);
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Use this function in warning situations.
240
241void Warning(const char *location, const char *fmt, ...)
242{
243 std::va_list ap;
244 va_start(ap, fmt);
245 ErrorHandler(kWarning, location, fmt, ap);
246 va_end(ap);
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Use this function in case of a fatal error. It will abort the program.
251
252/// @warning Fatal() *will* not abort the program if `gErrorIgnoreLevel > kFatal`
253/// - but for all reasonable settings it *will* abort.
254// So let's be reasonable wrt Coverity:
255// coverity[+kill]
256void Fatal(const char *location, const char *fmt, ...)
257{
258 std::va_list ap;
259 va_start(ap, fmt);
260 ErrorHandler(kFatal, location, fmt, ap);
261 va_end(ap);
262}
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Int_t gErrorAbortLevel
Definition TError.cxx:44
const char * kAssertMsg
Definition TError.cxx:47
const Int_t kPrint
Definition TError.cxx:35
const Int_t kError
Definition TError.cxx:38
const Int_t kSysError
Definition TError.cxx:40
void Error(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:197
void Break(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:219
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:112
const Int_t kUnset
Definition TError.cxx:34
void Info(const char *location, const char *fmt,...)
Use this function for informational messages.
Definition TError.cxx:230
static ErrorHandlerFunc_t gErrorHandler
Definition TError.cxx:50
const Int_t kFatal
Definition TError.cxx:41
const Int_t kBreak
Definition TError.cxx:39
void SysError(const char *location, const char *fmt,...)
Use this function in case a system (OS or GUI) related error occurred.
Definition TError.cxx:208
TVirtualMutex * gErrorMutex
Error handling routines.
Definition TError.cxx:32
static ROOT::Internal::ErrorSystemMsgHandlerFunc_t & GetErrorSystemMsgHandlerRef()
Definition TError.cxx:53
void AbstractMethod(const char *method)
This function can be used in abstract base classes in case one does not want to make the class a "rea...
Definition TError.cxx:171
const Int_t kWarning
Definition TError.cxx:37
void ErrorHandler(Int_t level, const char *location, const char *fmt, std::va_list ap)
General error handler function. It calls the user set error handler.
Definition TError.cxx:121
void Warning(const char *location, const char *fmt,...)
Use this function in warning situations.
Definition TError.cxx:241
void MayNotUse(const char *method)
This function can be used in classes that should override a certain function, but in the inherited cl...
Definition TError.cxx:180
void Fatal(const char *location, const char *fmt,...)
Use this function in case of a fatal error. It will abort the program.
Definition TError.cxx:256
Int_t gErrorIgnoreLevel
Definition TError.cxx:43
const char * kCheckMsg
Definition TError.cxx:48
void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
Use this function to declare a function obsolete.
Definition TError.cxx:189
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition TError.cxx:102
const Int_t kInfo
Definition TError.cxx:36
Bool_t gPrintViaErrorHandler
Definition TError.cxx:45
const Int_t kBreak
Definition TError.cxx:39
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition TError.h:69
Int_t gErrorIgnoreLevel
Definition TError.cxx:43
#define va_copy(x, y)
Definition civetweb.c:1000
This class implements a mutex interface.
const Int_t n
Definition legend1.C:16
std::function< const char *()> ErrorSystemMsgHandlerFunc_t
Retrieves the error string associated with the last system error.
Definition TError.h:58
void MinimalErrorHandler(int level, Bool_t abort, const char *location, const char *msg)
A very simple error handler that is usually replaced by the TROOT default error handler.
Definition TError.cxx:79
ErrorSystemMsgHandlerFunc_t SetErrorSystemMsgHandler(ErrorSystemMsgHandlerFunc_t h)
Returns the previous system error message handler.
Definition TError.cxx:68
ErrorSystemMsgHandlerFunc_t GetErrorSystemMsgHandler()
Definition TError.cxx:63
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.