CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // ---------------------------------------------------------------------- 00002 // 00003 // ZMexception.cc -- implementation of the ZMexception class 00004 // 00005 // Methods found here: 00006 // handleThis(x) 00007 // logMessage() 00008 // 00009 // Revision History: 00010 // 970912 MF Initial version after separating .icc from .cc 00011 // 970916 WEB Updated per code review 00012 // 970917 WEB Updated per code review 2 00013 // 970918 WEB Updated per code review 3 00014 // 971113 WEB Updated to conform to standard coding techniques 00015 // 971211 WEB Updated per code walkthrough 00016 // 971215 WEB Gave names to the default handler & logger 00017 // 971217 WEB Append filter failure messages in logMessage() 00018 // 971219 WEB Append newline to formatted messages 00019 // 980213 WEB Include ZMutility/ZMtime.h instead of <ctime> 00020 // 980223 WEB Include ZMutility/ctime instead of ZMtime 00021 // 980304 WEB Reformat logged messages to avoid excessively 00022 // long lines; otherwise cleaned up logMessage() & 00023 // related stuff 00024 // 980421 WEB Moved name() and facility() from .icc to .cc 00025 // 980615 WEB Added namespace support 00026 // 000217 WEB Improve C++ standards compliance 00027 // 010410 MF Added code to supress time and file path 00028 // 010626 MF Added code for ctor from ostringstream 00029 // 031105 LG Get rid of all ZMutility references 00030 // 00031 // ---------------------------------------------------------------------- 00032 00033 00034 #include "CLHEP/Exceptions/defs.h" 00035 #include "CLHEP/Exceptions/ZMexception.h" 00036 #include "CLHEP/Exceptions/ZMexAction.h" 00037 #include "CLHEP/Exceptions/ZMexHandler.h" 00038 #include "CLHEP/Exceptions/ZMexLogger.h" 00039 #include "CLHEP/Exceptions/ZMexLogResult.h" 00040 00041 #include <sstream> 00042 #include <ctime> 00043 00044 00045 namespace zmex { 00046 00047 // ************************************** 00048 // 00049 // ZMexUserActivity, ZMexUserNumericalTag 00050 // 00051 // ************************************** 00052 00053 std::string ZMexUserActivity = ""; 00054 int ZMexUserNumericalTag = 0; 00055 00056 00057 // ******************* 00058 // 00059 // ZMhandler, ZMlogger 00060 // 00061 // ******************* 00062 00063 ZMexHandler & ZMhandler() { 00064 static ZMexHandler ZMhandler = ZMexHandler( ZMexThrowErrors() ); 00065 return ZMhandler; 00066 } // ZMhandler() 00067 00068 ZMexLogger & ZMlogger() { 00069 static ZMexLogger ZMlogger = ZMexLogger( ZMexLogAlways() ); 00070 return ZMlogger; 00071 } 00072 00073 00074 // *********************** 00075 // 00076 // ZMexception::_classInfo 00077 // 00078 // *********************** 00079 00080 ZMexClassInfo ZMexception::_classInfo( 00081 "ZMexception" 00082 , "Exceptions" 00083 , ZMexFATAL 00084 , ZMhandler() 00085 , ZMlogger() 00086 ); 00087 00088 00089 // *********************** 00090 // ZMexception::facility() 00091 // *********************** 00092 00093 std::string ZMexception::facility() const { 00094 00095 return classInfo().facility(); 00096 00097 } // ZMexception::facility() 00098 00099 00100 // ******************* 00101 // ZMexception::name() 00102 // ******************* 00103 00104 std::string ZMexception::name() const { 00105 00106 return classInfo().name(); 00107 00108 } // ZMexception::name() 00109 00110 00111 //************* 00112 // logMessage() 00113 //************* 00114 00115 // This will be overridden in cases where, for a particular exception, 00116 // one wishes to include auxiliary information with the logged message. 00117 // The overriding function should compose its string, then call this 00118 // (its ancestor) function with that string as argument. 00119 00120 std::string ZMexception::logMessage( const std::string optText ) const { 00121 00122 std::ostringstream mesg; 00123 00124 // define how each follow-on line should begin: 00125 #define NEXT "\n " 00126 00127 // Supply the exception's identification as the first line: 00128 mesg << facility() 00129 << "-" << ZMexSeverityLetter[ severity() ] 00130 << "-" << name() << " [#" << count() << "]"; 00131 00132 // Second line gives the exception instance's message 00133 mesg << NEXT << message(); 00134 00135 // Warn if this exception hits the max for its severity: 00136 if ( 1 == ZMexSeverityLimit[ severity() ] ) 00137 mesg << NEXT "-- Note: severity threshhold has been reached; " 00138 "logging will be suppressed " 00139 "for any future exceptions of this severity"; 00140 00141 // Warn if this exception hits the max for its class: 00142 if ( classInfo().count() == classInfo().filterMax() ) 00143 mesg << NEXT "-- Note: class threshhold has been reached; " 00144 "logging will be suppressed " 00145 "for any future exceptions of this class"; 00146 00147 // Insert optional text (probably from override calling this as its ancestor): 00148 if ( optText.length() ) 00149 mesg << NEXT << optText; 00150 00151 // Insert time stamp: 00152 ZMexLogger lgr = getLogger(); 00153 if ( lgr.control()->isTimeDesired() ) { 00154 time_t now( time(0) ); 00155 char * timeText = ctime( & now ); 00156 timeText[24] = '\0'; // overwrite terminal '\n' 00157 mesg << NEXT << timeText; 00158 } 00159 00160 // Identify whence we got here: 00161 mesg << NEXT "-- ZMthrow was issued at line " << line(); 00162 std::string fullName = fileName(); 00163 std::string fname; 00164 if ( lgr.control()->isFilePathDesired() ) { 00165 fname = fullName; 00166 } else { 00167 unsigned int lastSlash = fullName.find_last_of("/\\"); 00168 if ( lastSlash == fullName.length() ) { 00169 fname = fullName; 00170 } else { 00171 fname = fullName.substr(lastSlash+1); 00172 } 00173 } 00174 mesg << NEXT "of file \"" << fname << '\"'; 00175 00176 // Identify disposition: 00177 mesg << NEXT "... Exception " << ( wasThrown() ? "thrown!" 00178 : "ignored" 00179 ); 00180 00181 // Include optional user information, part 1: 00182 if ( ZMexUserActivity.length() ) 00183 mesg << NEXT "-- ZMexUserActivity was: " << ZMexUserActivity; 00184 00185 // Include optional user information, part 2: 00186 if ( ZMexUserNumericalTag ) 00187 mesg << NEXT "-- User Numerical Tag was: " << ZMexUserNumericalTag; 00188 00189 return mesg.str() + '\n'; 00190 00191 } // ZMexception::logMessage() 00192 00193 //*********************************************** 00194 // Constructor of ZMexception from ostringstream& 00195 //*********************************************** 00196 00197 ZMexception::ZMexception( 00198 const std::ostringstream & msg 00199 , const ZMexSeverity howBad 00200 , int icount 00201 ) : 00202 message_(msg.str()) 00203 , line_( 0 ) 00204 , sourceFileName_( "not ZMthrow'n as of yet" ) 00205 , mySeverity_( howBad == ZMexSEVERITYenumLAST ? _classInfo.severity() : howBad ) 00206 , myCount_( icount ) 00207 , wasThrown_( false ) 00208 { } 00209 00210 } // namespace zmex