27 #pragma GCC diagnostic ignored "-Wvarargs"
31 std::mutex s_output_synchronization;
33 size_t _the_printer_1(
void*, dd4hep::PrintLevel lvl,
const char* src,
const char* text);
34 size_t _the_printer_2(
void* par, dd4hep::PrintLevel lvl,
const char* src,
const char* fmt, va_list& args);
36 std::string print_fmt =
"%-16s %5s %s";
37 dd4hep::PrintLevel print_lvl = dd4hep::INFO;
39 dd4hep::output_function1_t print_func_1 = 0;
40 dd4hep::output_function2_t print_func_2 = _the_printer_2;
42 const char* print_level(dd4hep::PrintLevel lvl) {
44 case dd4hep::NOLOG:
return "NOLOG";
45 case dd4hep::VERBOSE:
return "VERB ";
46 case dd4hep::DEBUG:
return "DEBUG";
47 case dd4hep::INFO:
return "INFO ";
48 case dd4hep::WARNING:
return "WARN ";
49 case dd4hep::ERROR:
return "ERROR";
50 case dd4hep::FATAL:
return "FATAL";
51 case dd4hep::ALWAYS:
return " ";
53 if ( lvl> dd4hep::ALWAYS )
54 return print_level(dd4hep::ALWAYS);
55 return print_level(dd4hep::NOLOG);
59 size_t _the_printer_1(
void*, dd4hep::PrintLevel lvl,
const char* src,
const char* text) {
60 std::lock_guard<std::mutex> lock(s_output_synchronization);
63 std::cout << std::flush;
64 std::cerr << std::flush;
65 size_t len = ::fprintf(stdout, print_fmt.c_str(), src, print_level(lvl), text);
70 size_t _the_printer_2(
void* par, dd4hep::PrintLevel lvl,
const char* src,
const char* fmt, va_list& args) {
71 if ( !print_func_1 ) {
73 std::lock_guard<std::mutex> lock(s_output_synchronization);
76 std::cout << std::flush;
77 std::cerr << std::flush;
78 ::snprintf(text,
sizeof(text),print_fmt.c_str(),src,print_level(lvl),fmt);
79 size_t len = ::vfprintf(stdout, text, args);
84 ::vsnprintf(str,
sizeof(str), fmt, args);
85 return print_func_1(par, lvl, src, str);
88 std::string __format(
const char* fmt, va_list& args) {
90 ::vsnprintf(str,
sizeof(str), fmt, args);
91 return std::string(str);
95 dd4hep::PrintLevel dd4hep::decodePrintLevel(
const std::string& val) {
96 switch(::toupper(val[0])) {
99 return dd4hep::VERBOSE;
102 return dd4hep::DEBUG;
108 return dd4hep::WARNING;
111 return dd4hep::ERROR;
114 return dd4hep::FATAL;
117 return dd4hep::FATAL;
119 std::cout <<
"Unknown print level supplied:'" << val <<
"'. Argument ignored." << std::endl;
120 throw std::runtime_error(
"Invalid printLevel:"+val);
130 std::string dd4hep::arguments(
int argc,
char** argv) {
131 std::stringstream str;
132 for(
int i=0; i<argc;) {
134 if ( ++i < argc ) str <<
" ";
147 int dd4hep::printout(PrintLevel severity,
const char* src, std::stringstream& str) {
149 if (severity >= print_lvl) {
150 ret = printout(severity, src, str.str().c_str());
164 int dd4hep::printout(PrintLevel severity,
const std::string& src, std::stringstream& str) {
166 if (severity >= print_lvl) {
167 ret = printout(severity, src, str.str().c_str());
179 int dd4hep::printout(PrintLevel severity,
const char* src,
const char* fmt, ...) {
180 if (severity >= print_lvl) {
183 printout(severity, src, fmt, args);
195 int dd4hep::printout(PrintLevel severity,
const std::string& src,
const char* fmt, ...) {
196 if (severity >= print_lvl) {
199 printout(severity, src.c_str(), fmt, args);
211 int dd4hep::printout(PrintLevel severity,
const char* src,
const std::string& fmt, ...) {
212 if (severity >= print_lvl) {
214 va_start(args, &fmt);
215 printout(severity, src, fmt.c_str(), args);
227 int dd4hep::printout(PrintLevel severity,
const std::string& src,
const std::string& fmt, ...) {
228 if (severity >= print_lvl) {
230 va_start(args, &fmt);
231 printout(severity, src.c_str(), fmt.c_str(), args);
243 int dd4hep::printout(PrintLevel severity,
const char* src,
const char* fmt, va_list& args) {
244 if (severity >= print_lvl) {
245 print_func_2(print_arg, PrintLevel(severity&(~FORCE_LEVEL)), src, fmt, args);
256 int dd4hep::printout(PrintLevel severity,
const std::string& src,
const char* fmt, va_list& args) {
257 return printout(severity, src.c_str(), fmt, args);
266 int dd4hep::printout(PrintLevel severity,
const char* src,
const std::string& fmt, va_list& args) {
267 return printout(severity, src, fmt.c_str(), args);
276 int dd4hep::printout(PrintLevel severity,
const std::string& src,
const std::string& fmt, va_list& args) {
277 return printout(severity, src.c_str(), fmt.c_str(), args);
285 int dd4hep::except(
const std::string& src,
const std::string& fmt, ...) {
287 va_start(args, &fmt);
288 return except(src.c_str(),fmt.c_str(), args);
296 int dd4hep::except(
const char* src,
const char* fmt, ...) {
299 return except(src, fmt, args);
308 int dd4hep::except(
const std::string& src,
const std::string& fmt, va_list& args) {
309 std::string msg = __format(fmt.c_str(), args);
311 printout(ERROR, src.c_str(),
"%s", msg.c_str());
313 throw std::runtime_error((src+
": "+msg).c_str());
322 int dd4hep::except(
const char* src,
const char* fmt, va_list& args) {
323 std::string msg = __format(fmt, args);
325 printout(ERROR, src,
"%s", msg.c_str());
327 throw std::runtime_error((std::string(src)+
": "+msg).c_str());
335 std::string dd4hep::format(
const std::string& src,
const std::string& fmt, ...) {
337 va_start(args, &fmt);
338 std::string str = format(src, fmt, args);
348 std::string dd4hep::format(
const char* src,
const char* fmt, ...) {
351 std::string str = format(src, fmt, args);
362 std::string dd4hep::format(
const std::string& src,
const std::string& fmt, va_list& args) {
363 return format(src.c_str(), fmt.c_str(), args);
372 std::string dd4hep::format(
const char* src,
const char* fmt, va_list& args) {
375 if ( src && *src ) ::snprintf(str,
sizeof(str),
"%s: ", src);
376 size_t len2 = ::vsnprintf(str + len1,
sizeof(str) - len1, fmt, args);
377 if ( len2 >
sizeof(str) - len1 ) {
378 len2 =
sizeof(str) - len1 - 1;
379 str[
sizeof(str)-1] = 0;
381 return std::string(str);
385 dd4hep::PrintLevel dd4hep::setPrintLevel(PrintLevel new_level) {
386 PrintLevel old = print_lvl;
387 print_lvl = new_level;
392 dd4hep::PrintLevel dd4hep::printLevel() {
397 dd4hep::PrintLevel dd4hep::printLevel(
const char* value) {
398 if ( !value ) except(
"Printout",
"Invalid printlevel requested [EINVAL: Null-pointer argument]");
400 if ( strcmp(value,
"NOLOG") == 0 )
return dd4hep::NOLOG;
401 if ( strcmp(value,
"VERBOSE") == 0 )
return dd4hep::VERBOSE;
402 if ( strcmp(value,
"DEBUG") == 0 )
return dd4hep::DEBUG;
403 if ( strcmp(value,
"INFO") == 0 )
return dd4hep::INFO;
404 if ( strcmp(value,
"WARNING") == 0 )
return dd4hep::WARNING;
405 if ( strcmp(value,
"ERROR") == 0 )
return dd4hep::ERROR;
406 if ( strcmp(value,
"FATAL") == 0 )
return dd4hep::FATAL;
407 if ( strcmp(value,
"ALWAYS") == 0 )
return dd4hep::ALWAYS;
409 if ( strcmp(value,
"0") == 0 )
return dd4hep::NOLOG;
410 if ( strcmp(value,
"1") == 0 )
return dd4hep::VERBOSE;
411 if ( strcmp(value,
"2") == 0 )
return dd4hep::DEBUG;
412 if ( strcmp(value,
"3") == 0 )
return dd4hep::INFO;
413 if ( strcmp(value,
"4") == 0 )
return dd4hep::WARNING;
414 if ( strcmp(value,
"5") == 0 )
return dd4hep::ERROR;
415 if ( strcmp(value,
"6") == 0 )
return dd4hep::FATAL;
416 if ( strcmp(value,
"7") == 0 )
return dd4hep::ALWAYS;
417 except(
"Printout",
"Unknown printlevel requested:%s",value);
418 return dd4hep::ALWAYS;
422 dd4hep::PrintLevel dd4hep::printLevel(
const std::string& value) {
423 return printLevel(value.c_str());
427 bool dd4hep::isActivePrintLevel(
int severity) {
428 return severity >= print_lvl;
432 std::string dd4hep::setPrintFormat(
const std::string& new_format) {
433 std::string old = print_fmt;
434 print_fmt = new_format;
439 void dd4hep::setPrinter(
void* arg, output_function1_t fcn) {
441 print_func_1 = fcn ? fcn : _the_printer_1;
445 void dd4hep::setPrinter2(
void* arg, output_function2_t fcn) {
447 print_func_2 = fcn ? fcn : _the_printer_2;