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