DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Printout.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include "DD4hep/Printout.h"
16 
17 // C/C++ include files
18 #include <cstring>
19 #include <cstdarg>
20 #include <sstream>
21 #include <stdexcept>
22 // Disable some diagnostics for ROOT dictionaries
23 #ifdef __GNUC__
24 #pragma GCC diagnostic ignored "-Wvarargs"
25 #endif
26 
27 using namespace std;
28 
29 namespace {
30  size_t _the_printer_1(void*, DD4hep::PrintLevel lvl, const char* src, const char* text);
31  size_t _the_printer_2(void* par, DD4hep::PrintLevel lvl, const char* src, const char* fmt, va_list& args);
32 
33  std::string print_fmt = "%-16s %5s %s";
34  DD4hep::PrintLevel print_lvl = DD4hep::INFO;
35  void* print_arg = 0;
36  DD4hep::output_function1_t print_func_1 = 0;
37  DD4hep::output_function2_t print_func_2 = _the_printer_2;
38 
39  const char* print_level(DD4hep::PrintLevel lvl) {
40  switch(lvl) {
41  case DD4hep::NOLOG: return "NOLOG";
42  case DD4hep::VERBOSE: return "VERB ";
43  case DD4hep::DEBUG: return "DEBUG";
44  case DD4hep::INFO: return "INFO ";
45  case DD4hep::WARNING: return "WARN ";
46  case DD4hep::ERROR: return "ERROR";
47  case DD4hep::FATAL: return "FATAL";
48  case DD4hep::ALWAYS: return " ";
49  default:
50  if ( lvl> DD4hep::ALWAYS )
51  return print_level(DD4hep::ALWAYS);
52  return print_level(DD4hep::NOLOG);
53  }
54  }
55 
56  size_t _the_printer_1(void*, DD4hep::PrintLevel lvl, const char* src, const char* text) {
57  ::fflush(stdout);
58  ::fflush(stderr);
59  cout << flush;
60  cerr << flush;
61  size_t len = ::fprintf(stdout, print_fmt.c_str(), src, print_level(lvl), text);
62  ::fputc('\n',stdout);
63  return len;
64  }
65 
66  size_t _the_printer_2(void* par, DD4hep::PrintLevel lvl, const char* src, const char* fmt, va_list& args) {
67  if ( !print_func_1 ) {
68  char text[4096];
69  ::fflush(stdout);
70  ::fflush(stderr);
71  cout << flush;
72  cerr << flush;
73  ::snprintf(text,sizeof(text),print_fmt.c_str(),src,print_level(lvl),fmt);
74  size_t len = ::vfprintf(stdout, text, args);
75  ::fputc('\n',stdout);
76  return len;
77  }
78  char str[4096];
79  ::vsnprintf(str, sizeof(str), fmt, args);
80  return print_func_1(par, lvl, src, str);
81  }
82 
83  string __format(const char* fmt, va_list& args) {
84  char str[4096];
85  ::vsnprintf(str, sizeof(str), fmt, args);
86  return string(str);
87  }
88 }
89 
91 
96 string DD4hep::arguments(int argc, char** argv) {
97  stringstream str;
98  for(int i=0; i<argc;) {
99  str << argv[i];
100  if ( ++i < argc ) str << " ";
101  }
102  return str.str();
103 }
104 
111 int DD4hep::printout(PrintLevel severity, const char* src, const char* fmt, ...) {
112  if (severity >= print_lvl) {
113  va_list args;
114  va_start(args, fmt);
115  printout(severity, src, fmt, args);
116  va_end(args);
117  }
118  return 1;
119 }
120 
127 int DD4hep::printout(PrintLevel severity, const string& src, const char* fmt, ...) {
128  if (severity >= print_lvl) {
129  va_list args;
130  va_start(args, fmt);
131  printout(severity, src.c_str(), fmt, args);
132  va_end(args);
133  }
134  return 1;
135 }
136 
143 int DD4hep::printout(PrintLevel severity, const char* src, const string& fmt, ...) {
144  if (severity >= print_lvl) {
145  va_list args;
146  va_start(args, &fmt);
147  printout(severity, src, fmt.c_str(), args);
148  va_end(args);
149  }
150  return 1;
151 }
152 
159 int DD4hep::printout(PrintLevel severity, const string& src, const string& fmt, ...) {
160  if (severity >= print_lvl) {
161  va_list args;
162  va_start(args, &fmt);
163  printout(severity, src.c_str(), fmt.c_str(), args);
164  va_end(args);
165  }
166  return 1;
167 }
168 
175 int DD4hep::printout(PrintLevel severity, const char* src, const char* fmt, va_list& args) {
176  if (severity >= print_lvl) {
177  print_func_2(print_arg, severity,src,fmt,args);
178  }
179  return 1;
180 }
181 
188 int DD4hep::printout(PrintLevel severity, const string& src, const char* fmt, va_list& args) {
189  return printout(severity, src.c_str(), fmt, args);
190 }
191 
198 int DD4hep::printout(PrintLevel severity, const char* src, const string& fmt, va_list& args) {
199  return printout(severity, src, fmt.c_str(), args);
200 }
201 
208 int DD4hep::printout(PrintLevel severity, const string& src, const string& fmt, va_list& args) {
209  return printout(severity, src.c_str(), fmt.c_str(), args);
210 }
211 
217 int DD4hep::except(const string& src, const string& fmt, ...) {
218  va_list args;
219  va_start(args, &fmt);
220  return except(src.c_str(),fmt.c_str(), args);
221 }
222 
228 int DD4hep::except(const char* src, const char* fmt, ...) {
229  va_list args;
230  va_start(args, fmt);
231  return except(src, fmt, args);
232 }
233 
240 int DD4hep::except(const string& src, const string& fmt, va_list& args) {
241  string msg = __format(fmt.c_str(), args);
242  va_end(args);
243  printout(ERROR, src.c_str(), "%s", msg.c_str());
244  // No return. Must call va_end here!
245  throw runtime_error((src+": "+msg).c_str());
246 }
247 
254 int DD4hep::except(const char* src, const char* fmt, va_list& args) {
255  string msg = __format(fmt, args);
256  va_end(args);
257  printout(ERROR, src, "%s", msg.c_str());
258  // No return. Must call va_end here!
259  throw runtime_error((string(src)+": "+msg).c_str());
260 }
261 
267 string DD4hep::format(const string& src, const string& fmt, ...) {
268  va_list args;
269  va_start(args, &fmt);
270  string str = format(src, fmt, args);
271  va_end(args);
272  return str;
273 }
274 
280 string DD4hep::format(const char* src, const char* fmt, ...) {
281  va_list args;
282  va_start(args, fmt);
283  string str = format(src, fmt, args);
284  va_end(args);
285  return str;
286 }
287 
294 string DD4hep::format(const string& src, const string& fmt, va_list& args) {
295  return format(src.c_str(), fmt.c_str(), args);
296 }
297 
304 string DD4hep::format(const char* src, const char* fmt, va_list& args) {
305  char str[4096];
306  size_t len1 = ::snprintf(str, sizeof(str), "%s: ", src);
307  size_t len2 = ::vsnprintf(str + len1, sizeof(str) - len1, fmt, args);
308  if ( len2 > sizeof(str) - len1 ) {
309  len2 = sizeof(str) - len1 - 1;
310  str[sizeof(str)-1] = 0;
311  }
312  return string(str);
313 }
314 
317  PrintLevel old = print_lvl;
318  print_lvl = new_level;
319  return old;
320 }
321 
324  return print_lvl;
325 }
326 
329  if ( !value ) except("Printout","Invalid printlevel requested");
330  // Explicit values
331  if ( strcmp(value,"NOLOG") == 0 ) return DD4hep::NOLOG;
332  if ( strcmp(value,"VERBOSE") == 0 ) return DD4hep::VERBOSE;
333  if ( strcmp(value,"DEBUG") == 0 ) return DD4hep::DEBUG;
334  if ( strcmp(value,"INFO") == 0 ) return DD4hep::INFO;
335  if ( strcmp(value,"WARNING") == 0 ) return DD4hep::WARNING;
336  if ( strcmp(value,"ERROR") == 0 ) return DD4hep::ERROR;
337  if ( strcmp(value,"FATAL") == 0 ) return DD4hep::FATAL;
338  if ( strcmp(value,"ALWAYS") == 0 ) return DD4hep::ALWAYS;
339  // Numeric values
340  if ( strcmp(value,"0") == 0 ) return DD4hep::NOLOG;
341  if ( strcmp(value,"1") == 0 ) return DD4hep::VERBOSE;
342  if ( strcmp(value,"2") == 0 ) return DD4hep::DEBUG;
343  if ( strcmp(value,"3") == 0 ) return DD4hep::INFO;
344  if ( strcmp(value,"4") == 0 ) return DD4hep::WARNING;
345  if ( strcmp(value,"5") == 0 ) return DD4hep::ERROR;
346  if ( strcmp(value,"6") == 0 ) return DD4hep::FATAL;
347  if ( strcmp(value,"7") == 0 ) return DD4hep::ALWAYS;
348  except("Printout","Unknown printlevel requested:%s",value);
349  return DD4hep::ALWAYS;
350 }
351 
353 DD4hep::PrintLevel DD4hep::printLevel(const std::string& value) {
354  return printLevel(value.c_str());
355 }
356 
358 string DD4hep::setPrintFormat(const string& new_format) {
359  string old = print_fmt;
360  print_fmt = new_format;
361  return old;
362 }
363 
366  print_arg = arg;
367  print_func_1 = fcn ? fcn : _the_printer_1;
368 }
369 
372  print_arg = arg;
373  print_func_2 = fcn ? fcn : _the_printer_2;
374 }
void setPrinter2(void *print_arg, output_function2_t fcn)
Customize printer function.
Definition: Printout.cpp:371
PrintLevel printLevel()
Access the current printer level.
Definition: Printout.cpp:323
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
PrintLevel
Definition: Printout.h:47
std::string format(const std::string &src, const std::string &fmt,...)
Build formatted string.
Definition: Printout.cpp:267
size_t(* output_function1_t)(void *, PrintLevel severity, const char *, const char *)
Definition: Printout.h:59
void setPrinter(void *print_arg, output_function1_t fcn)
Customize printer function.
Definition: Printout.cpp:365
size_t(* output_function2_t)(void *, PrintLevel severity, const char *, const char *, va_list &args)
Definition: Printout.h:60
std::string setPrintFormat(const std::string &new_format)
Set new printout format for the 3 fields: source-level-message. All 3 are strings.
Definition: Printout.cpp:358
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
PrintLevel setPrintLevel(PrintLevel new_level)
Set new print level. Returns the old print level.
Definition: Printout.cpp:316
std::string arguments(int argc, char **argv)
Helper function to serialize argument list to a single string.
Definition: Printout.cpp:96