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
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
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);
77 fflush(stderr);
78 if (abort_bool) {
79 fprintf(stderr, "aborting\n");
80 fflush(stderr);
81 abort();
82 }
83}
84
85} // namespace Internal
86} // namespace ROOT
87
88
89////////////////////////////////////////////////////////////////////////////////
90/// Set an errorhandler function. Returns the old handler.
91
93{
95 gErrorHandler = newhandler;
96 return oldhandler;
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Returns the current error handler function.
101
103{
104 return gErrorHandler;
105}
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 thread_local Int_t buf_size(256);
114 thread_local char *buf_storage(nullptr);
115
116 char small_buf[256];
117 char *buf = buf_storage ? buf_storage : small_buf;
118
119 std::va_list ap_copy;
120 va_copy(ap_copy, ap);
121
122 if (!fmt)
123 fmt = "no error message provided";
124
125 Int_t n = vsnprintf(buf, buf_size, fmt, ap_copy);
126 if (n >= buf_size) {
127 va_end(ap_copy);
128
129 buf_size = n + 1;
130 if (buf != &(small_buf[0]))
131 delete[] buf;
132 buf_storage = buf = new char[buf_size];
133
134 // Try again with a sufficiently large buffer
135 va_copy(ap_copy, ap);
136 vsnprintf(buf, buf_size, fmt, ap_copy);
137 }
138 va_end(ap_copy);
139
140 std::string bp = buf;
141 if (level >= kSysError && level < kFatal) {
142 bp.push_back(' ');
145 else
146 bp += std::string("(errno: ") + std::to_string(errno) + ")";
147 }
148
149 if (level != kFatal)
150 gErrorHandler(level, level >= gErrorAbortLevel, location, bp.c_str());
151 else
152 gErrorHandler(level, kTRUE, location, bp.c_str());
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// This function can be used in abstract base classes in case one does
157/// not want to make the class a "real" (in C++ sense) ABC. If this
158/// function is called it will warn the user that the function should
159/// have been overridden.
160
161void AbstractMethod(const char *method)
162{
163 Warning(method, "this method must be overridden!");
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// This function can be used in classes that should override a certain
168/// function, but in the inherited class the function makes no sense.
169
170void MayNotUse(const char *method)
171{
172 Warning(method, "may not use this method");
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Use this function to declare a function obsolete. Specify as of which version
177/// the method is obsolete and as from which version it will be removed.
178
179void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
180{
181 Warning(function, "obsolete as of %s and will be removed from %s", asOfVers, removedFromVers);
182}
183
184////////////////////////////////////////////////////////////////////////////////
185/// Use this function in case an error occurred.
186
187void Error(const char *location, const char *fmt, ...)
188{
189 std::va_list ap;
190 va_start(ap, fmt);
191 ErrorHandler(kError, location, fmt, ap);
192 va_end(ap);
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Use this function in case a system (OS or GUI) related error occurred.
197
198void SysError(const char *location, const char *fmt, ...)
199{
200 std::va_list ap;
201 va_start(ap, fmt);
202 ErrorHandler(kSysError, location, fmt, ap);
203 va_end(ap);
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Use this function in case an error occurred.
208
209void Break(const char *location, const char *fmt, ...)
210{
211 std::va_list ap;
212 va_start(ap, fmt);
213 ErrorHandler(kBreak, location, fmt, ap);
214 va_end(ap);
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Use this function for informational messages.
219
220void Info(const char *location, const char *fmt, ...)
221{
222 std::va_list ap;
223 va_start(ap, fmt);
224 ErrorHandler(kInfo, location, fmt, ap);
225 va_end(ap);
226}
227
228////////////////////////////////////////////////////////////////////////////////
229/// Use this function in warning situations.
230
231void Warning(const char *location, const char *fmt, ...)
232{
233 std::va_list ap;
234 va_start(ap, fmt);
235 ErrorHandler(kWarning, location, fmt, ap);
236 va_end(ap);
237}
238
239////////////////////////////////////////////////////////////////////////////////
240/// Use this function in case of a fatal error. It will abort the program.
241
242// Fatal() *might* not abort the program (if gAbortLevel > kFatal) - but for all
243// reasonable settings it *will* abort. So let's be reasonable wrt Coverity:
244// coverity[+kill]
245void Fatal(const char *location, const char *fmt, ...)
246{
247 std::va_list ap;
248 va_start(ap, fmt);
249 ErrorHandler(kFatal, location, fmt, ap);
250 va_end(ap);
251}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
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:187
void Break(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition TError.cxx:209
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:220
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:198
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:161
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:231
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:170
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:245
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:179
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition TError.cxx:92
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
R__EXTERN Int_t gErrorIgnoreLevel
Definition TError.h:127
constexpr Int_t kUnset
Definition TError.h:42
#define va_copy(x, y)
Definition civetweb.c:892
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: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:69
ErrorSystemMsgHandlerFunc_t SetErrorSystemMsgHandler(ErrorSystemMsgHandlerFunc_t h)
Returns the previous system error message handler.
Definition TError.cxx:59
ErrorSystemMsgHandlerFunc_t GetErrorSystemMsgHandler()
Definition TError.cxx:54
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...