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 <cassert>
26#include <cstdarg>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30#include <cerrno>
31#include <string>
32
36
37const char *kAssertMsg = "%s violated at line %d of `%s'";
38const char *kCheckMsg = "%s not true at line %d of `%s'";
39
41
42
48
49
50namespace ROOT {
51namespace Internal {
52
57
64
65/// A very simple error handler that is usually replaced by the TROOT default error handler.
66/// The minimal error handler is not serialized across threads, so that output of multi-threaded programs
67/// can get scrambled
68/// @note `abort()` is only called if `abort_bool` is `true` and `level >= gErrorIgnoreLevel`
69void MinimalErrorHandler(Int_t level, Bool_t abort_bool, const char *location, const char *msg)
70{
71 if (level < gErrorIgnoreLevel)
72 return;
73
74 if (level >= kBreak)
75 fprintf(stderr, "\n *** Break *** ");
76 fprintf(stderr, "<%s>: %s\n", location ? location : "unspecified location", msg);
78 if (abort_bool) {
79 fprintf(stderr, "aborting\n");
81 abort();
82 }
83}
84
85} // namespace Internal
86} // namespace ROOT
87
88
89////////////////////////////////////////////////////////////////////////////////
90/// Set an errorhandler function. Returns the old handler.
91
98
99////////////////////////////////////////////////////////////////////////////////
100/// Returns the current error handler function.
101
106
107
108////////////////////////////////////////////////////////////////////////////////
109/// General error handler function. It calls the user set error handler.
110
111void ErrorHandler(Int_t level, const char *location, const char *fmt, std::va_list ap)
112{
113 if (!fmt)
114 fmt = "no error message provided";
115
116 char smallBuf[256];
117
118 thread_local int bufSize = sizeof(smallBuf);
119 thread_local char *bufDynStorage = nullptr;
120
121 char *buf = bufDynStorage ? bufDynStorage : smallBuf;
122
123 std::va_list apCopy;
124 va_copy(apCopy, ap);
125
126 // Write the user string to the current buffer and, at the same time, figure out exactly how many bytes we need.
127 int nWritten = vsnprintf(buf, bufSize, fmt, ap);
128 int nAdditional = 0;
129 if (level >= kSysError && level < kFatal) {
131 if (sysHandler)
132 nAdditional = strlen(sysHandler()) + 1; // +1 for the whitespace
133 else {
134 nAdditional = snprintf(nullptr, 0, " (errno: %d)", errno);
135 }
136 }
137
138 // nWritten and nAdditional are the number of characters, nRequired is the required
139 // number of bytes, hence the + 1 for the null terminator.
140 int nRequired = nWritten + nAdditional + 1;
141 if (nRequired >= bufSize) {
142 // Not enough space: allocate more space on the heap to fit the string.
143 if (buf != smallBuf)
144 delete[] buf;
145
146 bufSize = std::max(bufSize * 2, nRequired);
147 buf = bufDynStorage = new char[bufSize];
148 // Write the user string again in the new buffer.
149 vsnprintf(buf, bufSize, fmt, apCopy);
150 }
151 va_end(apCopy);
152
154
155 // if necessary, write the additional string.
156 // NOTE: this will overwrite the null byte written by the previous vsnprintf, extending the string.
157 [[maybe_unused]] int nWrittenPost = 0;
158 if (nAdditional > 0) {
160 if (sysHandler) {
162 } else {
163 nWrittenPost = snprintf(buf + nWritten, bufSize - nWritten, " (errno: %d)", errno);
164 }
165 }
166
169
170 if (level != kFatal)
171 gErrorHandler(level, level >= gErrorAbortLevel, location, buf);
172 else
173 gErrorHandler(level, kTRUE, location, buf);
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// This function can be used in abstract base classes in case one does
178/// not want to make the class a "real" (in C++ sense) ABC. If this
179/// function is called it will warn the user that the function should
180/// have been overridden.
181
182void AbstractMethod(const char *method)
183{
184 Warning(method, "this method must be overridden!");
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// This function can be used in classes that should override a certain
189/// function, but in the inherited class the function makes no sense.
190
191void MayNotUse(const char *method)
192{
193 Warning(method, "may not use this method");
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Use this function to declare a function obsolete. Specify as of which version
198/// the method is obsolete and as from which version it will be removed.
199
200void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
201{
202 Warning(function, "obsolete as of %s and will be removed from %s", asOfVers, removedFromVers);
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Use this function in case an error occurred.
207
208void Error(const char *location, const char *fmt, ...)
209{
210 std::va_list ap;
211 va_start(ap, fmt);
212 ErrorHandler(kError, location, fmt, ap);
213 va_end(ap);
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Use this function in case a system (OS or GUI) related error occurred.
218
219void SysError(const char *location, const char *fmt, ...)
220{
221 std::va_list ap;
222 va_start(ap, fmt);
223 ErrorHandler(kSysError, location, fmt, ap);
224 va_end(ap);
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// Use this function in case an error occurred.
229
230void Break(const char *location, const char *fmt, ...)
231{
232 std::va_list ap;
233 va_start(ap, fmt);
234 ErrorHandler(kBreak, location, fmt, ap);
235 va_end(ap);
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Use this function for informational messages.
240
241void Info(const char *location, const char *fmt, ...)
242{
243 std::va_list ap;
244 va_start(ap, fmt);
245 ErrorHandler(kInfo, location, fmt, ap);
246 va_end(ap);
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Use this function in warning situations.
251
252void Warning(const char *location, const char *fmt, ...)
253{
254 std::va_list ap;
255 va_start(ap, fmt);
256 ErrorHandler(kWarning, location, fmt, ap);
257 va_end(ap);
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Use this function in case of a fatal error. It will abort the program.
262
263/// @warning Fatal() *will* not abort the program if `gErrorIgnoreLevel > kFatal`
264/// - but for all reasonable settings it *will* abort.
265// So let's be reasonable wrt Coverity:
266// coverity[+kill]
267void Fatal(const char *location, const char *fmt, ...)
268{
269 std::va_list ap;
270 va_start(ap, fmt);
271 ErrorHandler(kFatal, location, fmt, ap);
272 va_end(ap);
273}
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Int_t gErrorAbortLevel
non-ignored errors with level equal or above this value will call abort(). Default is kSysError+1.
Definition TError.cxx:34
const char * kAssertMsg
Definition TError.cxx:37
void Error(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
void Break(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:230
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:102
void Info(const char *location, const char *fmt,...)
Use this function for informational messages.
Definition TError.cxx:241
static ErrorHandlerFunc_t gErrorHandler
Definition TError.cxx:40
void SysError(const char *location, const char *fmt,...)
Use this function in case a system (OS or GUI) related error occurred.
Definition TError.cxx:219
static ROOT::Internal::ErrorSystemMsgHandlerFunc_t & GetErrorSystemMsgHandlerRef()
Definition TError.cxx:43
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:182
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:111
void Warning(const char *location, const char *fmt,...)
Use this function in warning situations.
Definition TError.cxx:252
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:191
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:267
Int_t gErrorIgnoreLevel
Error handling routines.
Definition TError.cxx:33
const char * kCheckMsg
Definition TError.cxx:38
void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
Use this function to declare a function obsolete.
Definition TError.cxx:200
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition TError.cxx:92
Bool_t gPrintViaErrorHandler
If true, ROOT's Printf will print via the currently active ROOT error handler; if false (default),...
Definition TError.cxx:35
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
errors with level below this value will be ignored. Default is kUnset.
Definition TError.cxx:33
constexpr Int_t kSysError
Definition TError.h:49
constexpr Int_t kUnset
Definition TError.h:43
#define snprintf
Definition civetweb.c:1579
#define va_copy(x, y)
Definition civetweb.c:1023
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:69
ErrorSystemMsgHandlerFunc_t SetErrorSystemMsgHandler(ErrorSystemMsgHandlerFunc_t h)
Returns the previous system error message handler.
Definition TError.cxx:58
ErrorSystemMsgHandlerFunc_t GetErrorSystemMsgHandler()
Definition TError.cxx:53
Namespace for new ROOT classes and functions.