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 /**
13 Error handling routines.
14 
15 This file defines a number of global error handling routines:
16 Warning(), Error(), SysError() and Fatal(). They all take a
17 location string (where the error happened) and a printf style format
18 string plus vararg's. In the end these functions call an
19 errorhandler function. Initially the MinimalErrorHandler, which is supposed
20 to 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 
38 const char *kAssertMsg = "%s violated at line %d of `%s'";
39 const char *kCheckMsg = "%s not true at line %d of `%s'";
40 
42 
43 
45 {
47  return h;
48 }
49 
50 
51 namespace ROOT {
52 namespace 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 void 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 {
94  ErrorHandlerFunc_t oldhandler = gErrorHandler;
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 
111 void 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(' ');
144  bp += GetErrorSystemMsgHandlerRef()();
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 
161 void 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 
170 void 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 
179 void 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 
187 void 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 
198 void 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 
209 void 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 
220 void 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 
231 void 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]
245 void 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 }
kSysError
const Int_t kSysError
Definition: TError.h:50
kFatal
const Int_t kFatal
Definition: TError.h:51
n
const Int_t n
Definition: legend1.C:16
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
GetErrorHandler
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition: TError.cxx:102
ErrorHandlerFunc_t
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition: TError.h:72
ROOT::Internal::ErrorSystemMsgHandlerFunc_t
std::function< const char *()> ErrorSystemMsgHandlerFunc_t
Retrieves the error string associated with the last system error.
Definition: TError.h:61
Break
void Break(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:209
kWarning
const Int_t kWarning
Definition: TError.h:47
gErrorHandler
static ErrorHandlerFunc_t gErrorHandler
Definition: TError.cxx:41
kError
const Int_t kError
Definition: TError.h:48
SysError
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
Int_t
int Int_t
Definition: RtypesCore.h:45
gErrorMutex
TVirtualMutex * gErrorMutex
Error handling routines.
Definition: TError.cxx:32
TVirtualMutex
Definition: TVirtualMutex.h:32
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
MayNotUse
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
gErrorAbortLevel
Int_t gErrorAbortLevel
Definition: TError.cxx:35
Fatal
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
Obsolete
void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
Use this function to declare a function obsolete.
Definition: TError.cxx:179
h
#define h(i)
Definition: RSha256.hxx:124
Warning
void Warning(const char *location, const char *fmt,...)
Use this function in warning situations.
Definition: TError.cxx:231
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
Error
void Error(const char *location, const char *fmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
Info
void Info(const char *location, const char *fmt,...)
Use this function for informational messages.
Definition: TError.cxx:220
kAssertMsg
const char * kAssertMsg
Definition: TError.cxx:38
gPrintViaErrorHandler
Bool_t gPrintViaErrorHandler
Definition: TError.cxx:36
kBreak
const Int_t kBreak
Definition: TError.h:49
ROOT::Internal::SetErrorSystemMsgHandler
ErrorSystemMsgHandlerFunc_t SetErrorSystemMsgHandler(ErrorSystemMsgHandlerFunc_t h)
Returns the previous system error message handler.
Definition: TError.cxx:59
kInfo
const Int_t kInfo
Definition: TError.h:46
ROOT::Internal::GetErrorSystemMsgHandler
ErrorSystemMsgHandlerFunc_t GetErrorSystemMsgHandler()
Definition: TError.cxx:54
SetErrorHandler
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition: TError.cxx:92
ErrorHandler
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
kCheckMsg
const char * kCheckMsg
Definition: TError.cxx:39
gErrorIgnoreLevel
Int_t gErrorIgnoreLevel
Definition: TError.cxx:34
ROOT
VSD Structures.
Definition: StringConv.hxx:21
GetErrorSystemMsgHandlerRef
static ROOT::Internal::ErrorSystemMsgHandlerFunc_t & GetErrorSystemMsgHandlerRef()
Definition: TError.cxx:44
va_copy
#define va_copy(x, y)
Definition: civetweb.c:892
kUnset
const Int_t kUnset
Definition: TError.h:44
ROOT::Internal::MinimalErrorHandler
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
AbstractMethod
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
TError.h