File indexing completed on 2025-01-30 09:17:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "XML/Printout.h"
0016 #include <stdexcept>
0017
0018
0019 #ifdef __GNUC__
0020 #pragma GCC diagnostic ignored "-Wvarargs"
0021 #endif
0022
0023 using namespace std;
0024
0025 namespace {
0026 string print_fmt = "%-16s %5s %s";
0027 dd4hep::PrintLevel print_lvl = dd4hep::INFO;
0028 const char* print_level(dd4hep::PrintLevel lvl) {
0029 switch(lvl) {
0030 case dd4hep::NOLOG: return "NOLOG";
0031 case dd4hep::VERBOSE: return "VERB ";
0032 case dd4hep::DEBUG: return "DEBUG";
0033 case dd4hep::INFO: return "INFO ";
0034 case dd4hep::WARNING: return "WARN ";
0035 case dd4hep::ERROR: return "ERROR";
0036 case dd4hep::FATAL: return "FATAL";
0037 case dd4hep::ALWAYS: return " ";
0038 default:
0039 if ( lvl> dd4hep::ALWAYS )
0040 return print_level(dd4hep::ALWAYS);
0041 return print_level(dd4hep::NOLOG);
0042 }
0043 }
0044 size_t print_out(dd4hep::PrintLevel lvl, const char* src, const char* fmt, va_list& args) {
0045 char text[4096];
0046 ::snprintf(text,sizeof(text),print_fmt.c_str(),src,print_level(lvl),fmt);
0047 size_t len = ::vfprintf(stdout, text, args);
0048 ::fputc('\n',stdout);
0049 return len;
0050 }
0051 }
0052
0053
0054 dd4hep::PrintLevel dd4hep::setPrintLevel(PrintLevel new_level) {
0055 PrintLevel old = print_lvl;
0056 print_lvl = new_level;
0057 return old;
0058 }
0059
0060
0061 dd4hep::PrintLevel dd4hep::printLevel() {
0062 return print_lvl;
0063 }
0064
0065
0066
0067
0068
0069
0070
0071
0072 int dd4hep::printout(PrintLevel severity, const char* src, const char* fmt, ...) {
0073 if (severity >= print_lvl) {
0074 va_list args;
0075 va_start(args, fmt);
0076 print_out(severity, src, fmt, args);
0077 va_end(args);
0078 }
0079 return 1;
0080 }
0081
0082
0083
0084
0085
0086
0087
0088 int dd4hep::printout(PrintLevel severity, const string& src, const char* fmt, ...) {
0089 if (severity >= print_lvl) {
0090 va_list args;
0091 va_start(args, fmt);
0092 print_out(severity, src.c_str(), fmt, args);
0093 va_end(args);
0094 }
0095 return 1;
0096 }
0097
0098
0099
0100
0101
0102
0103 int dd4hep::except(const char* src, const char* fmt, ...) {
0104 char str[4096];
0105 va_list args;
0106 va_start(args, fmt);
0107 size_t len1 = ::snprintf(str, sizeof(str), "%s: ", src);
0108 size_t len2 = ::vsnprintf(str + len1, sizeof(str) - len1, fmt, args);
0109 if ( len2 > sizeof(str) - len1 ) {
0110 len2 = sizeof(str) - len1 - 1;
0111 str[sizeof(str)-1] = 0;
0112 }
0113 printout(FATAL,src,fmt,args);
0114 va_end(args);
0115 throw runtime_error(str);
0116 }