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
34
35const char *kAssertMsg = "%s violated at line %d of `%s'";
36const char *kCheckMsg = "%s not true at line %d of `%s'";
37
39
40
42{
44 return h;
45}
46
47
48namespace ROOT {
49namespace Internal {
50
52{
54}
55
57{
58 auto oldHandler = GetErrorSystemMsgHandlerRef();
60 return oldHandler;
61}
62
63/// A very simple error handler that is usually replaced by the TROOT default error handler.
64/// The minimal error handler is not serialized across threads, so that output of multi-threaded programs
65/// can get scrambled
66/// @note `abort()` is only called if `abort_bool` is `true` and `level >= gErrorIgnoreLevel`
67void MinimalErrorHandler(Int_t level, Bool_t abort_bool, const char *location, const char *msg)
68{
69 if (level < gErrorIgnoreLevel)
70 return;
71
72 if (level >= kBreak)
73 fprintf(stderr, "\n *** Break *** ");
74 fprintf(stderr, "<%s>: %s\n", location ? location : "unspecified location", msg);
75 fflush(stderr);
76 if (abort_bool) {
77 fprintf(stderr, "aborting\n");
78 fflush(stderr);
79 abort();
80 }
81}
82
83} // namespace Internal
84} // namespace ROOT
85
86
87////////////////////////////////////////////////////////////////////////////////
88/// Set an errorhandler function. Returns the old handler.
89
91{
93 gErrorHandler = newhandler;
94 return oldhandler;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Returns the current error handler function.
99
101{
102 return gErrorHandler;
103}
104
105
106////////////////////////////////////////////////////////////////////////////////
107/// General error handler function. It calls the user set error handler.
108
109void ErrorHandler(Int_t level, const char *location, const char *fmt, std::va_list ap)
110{
111 thread_local Int_t buf_size(256);
112 thread_local char *buf_storage(nullptr);
113
114 char small_buf[256];
115 char *buf = buf_storage ? buf_storage : small_buf;
116
117 std::va_list ap_copy;
118 va_copy(ap_copy, ap);
119
120 if (!fmt)
121 fmt = "no error message provided";
122
123 Int_t n = vsnprintf(buf, buf_size, fmt, ap_copy);
124 if (n >= buf_size) {
125 va_end(ap_copy);
126
127 buf_size = n + 1;
128 if (buf != &(small_buf[0]))
129 delete[] buf;
130 buf_storage = buf = new char[buf_size];
131
132 // Try again with a sufficiently large buffer
133 va_copy(ap_copy, ap);
134 vsnprintf(buf, buf_size, fmt, ap_copy);
135 }
136 va_end(ap_copy);
137
138 std::string bp = buf;
139 if (level >= kSysError && level < kFatal) {
140 bp.push_back(' ');
143 else
144 bp += std::string("(errno: ") + std::to_string(errno) + ")";
145 }
146
147 if (level != kFatal)
148 gErrorHandler(level, level >= gErrorAbortLevel, location, bp.c_str());
149 else
150 gErrorHandler(level, kTRUE, location, bp.c_str());
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// This function can be used in abstract base classes in case one does
155/// not want to make the class a "real" (in C++ sense) ABC. If this
156/// function is called it will warn the user that the function should
157/// have been overridden.
158
159void AbstractMethod(const char *method)
160{
161 Warning(method, "this method must be overridden!");
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// This function can be used in classes that should override a certain
166/// function, but in the inherited class the function makes no sense.
167
168void MayNotUse(const char *method)
169{
170 Warning(method, "may not use this method");
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Use this function to declare a function obsolete. Specify as of which version
175/// the method is obsolete and as from which version it will be removed.
176
177void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
178{
179 Warning(function, "obsolete as of %s and will be removed from %s", asOfVers, removedFromVers);
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Use this function in case an error occurred.
184
185void Error(const char *location, const char *fmt, ...)
186{
187 std::va_list ap;
188 va_start(ap, fmt);
189 ErrorHandler(kError, location, fmt, ap);
190 va_end(ap);
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Use this function in case a system (OS or GUI) related error occurred.
195
196void SysError(const char *location, const char *fmt, ...)
197{
198 std::va_list ap;
199 va_start(ap, fmt);
200 ErrorHandler(kSysError, location, fmt, ap);
201 va_end(ap);
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Use this function in case an error occurred.
206
207void Break(const char *location, const char *fmt, ...)
208{
209 std::va_list ap;
210 va_start(ap, fmt);
211 ErrorHandler(kBreak, location, fmt, ap);
212 va_end(ap);
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Use this function for informational messages.
217
218void Info(const char *location, const char *fmt, ...)
219{
220 std::va_list ap;
221 va_start(ap, fmt);
222 ErrorHandler(kInfo, location, fmt, ap);
223 va_end(ap);
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Use this function in warning situations.
228
229void Warning(const char *location, const char *fmt, ...)
230{
231 std::va_list ap;
232 va_start(ap, fmt);
233 ErrorHandler(kWarning, location, fmt, ap);
234 va_end(ap);
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Use this function in case of a fatal error. It will abort the program.
239
240/// @warning Fatal() *will* not abort the program if `gErrorIgnoreLevel > kFatal`
241/// - but for all reasonable settings it *will* abort.
242// So let's be reasonable wrt Coverity:
243// coverity[+kill]
244void Fatal(const char *location, const char *fmt, ...)
245{
246 std::va_list ap;
247 va_start(ap, fmt);
248 ErrorHandler(kFatal, location, fmt, ap);
249 va_end(ap);
250}
#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:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
Int_t gErrorAbortLevel
Definition TError.cxx:32
const char * kAssertMsg
Definition TError.cxx:35
void Error(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Break(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:207
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:100
void Info(const char *location, const char *fmt,...)
Use this function for informational messages.
Definition TError.cxx:218
static ErrorHandlerFunc_t gErrorHandler
Definition TError.cxx:38
void SysError(const char *location, const char *fmt,...)
Use this function in case a system (OS or GUI) related error occurred.
Definition TError.cxx:196
static ROOT::Internal::ErrorSystemMsgHandlerFunc_t & GetErrorSystemMsgHandlerRef()
Definition TError.cxx:41
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:159
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:109
void Warning(const char *location, const char *fmt,...)
Use this function in warning situations.
Definition TError.cxx:229
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:168
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:244
Int_t gErrorIgnoreLevel
Error handling routines.
Definition TError.cxx:31
const char * kCheckMsg
Definition TError.cxx:36
void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
Use this function to declare a function obsolete.
Definition TError.cxx:177
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition TError.cxx:90
Bool_t gPrintViaErrorHandler
Definition TError.cxx:33
constexpr Int_t kError
Definition TError.h:47
constexpr Int_t kFatal
Definition TError.h:50
constexpr Int_t kWarning
Definition TError.h:46
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition TError.h:71
constexpr Int_t kBreak
Definition TError.h:48
constexpr Int_t kInfo
Definition TError.h:45
Int_t gErrorIgnoreLevel
Error handling routines.
Definition TError.cxx:31
constexpr Int_t kSysError
Definition TError.h:49
constexpr Int_t kUnset
Definition TError.h:43
#define va_copy(x, y)
Definition civetweb.c:1000
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:60
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:67
ErrorSystemMsgHandlerFunc_t SetErrorSystemMsgHandler(ErrorSystemMsgHandlerFunc_t h)
Returns the previous system error message handler.
Definition TError.cxx:56
ErrorSystemMsgHandlerFunc_t GetErrorSystemMsgHandler()
Definition TError.cxx:51
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...