CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // ---------------------------------------------------------------------- 00002 // 00003 // ZMexLogger.cc - define basic logging behaviors 00004 // 00005 // History: 00006 // 970919 WEB Created based on code review 4 comments on 00007 // ZMexLogger behavior desired 00008 // 971007 WEB Removed limiting logger; all loggers now 00009 // optionally limit by exception severity 00010 // 971211 WEB Updated per code walkthrough 00011 // 971211 WEB Created from ZMexLogger.icc per code walkthrough 00012 // 971215 WEB Removed unused 2nd parm to ZMexLogger constructor 00013 // 971219 WEB Use std::flush instead of endl in ...::emit() 00014 // 980617 WEB Added namespace support 00015 // 990104 WEB Merged with .icc; restructured os data member 00016 // ownership 00017 // 990802 JVR Added support for augmented exception logging 00018 // 010410 MF Added ZMexValidationStyle 00019 // 00020 // ---------------------------------------------------------------------- 00021 00022 00023 #include "CLHEP/Exceptions/ZMexLogger.h" 00024 #include "CLHEP/Exceptions/ZMexception.h" 00025 00026 00027 // ---------------------------------------------------------------------- 00028 00029 00030 namespace zmex { 00031 00032 00033 // ---------------------------------------------------------------------- 00034 00035 00036 // ----------------- 00037 // ZMexLogBehavior:: 00038 // ----------------- 00039 00040 ZMexLogBehavior::ZMexLogBehavior() { ; } 00041 00042 ZMexLogBehavior::~ZMexLogBehavior() { ; } 00043 00044 ZMexLogBehavior * 00045 ZMexLogBehavior::clone() const { return new ZMexLogBehavior( *this ); } 00046 00047 ZMexLogResult ZMexLogBehavior::emit( const ZMexception & ) { 00048 return ZMexNOTLOGGED; 00049 } 00050 00051 ZMexLogResult ZMexLogBehavior::emit( 00052 const std::string & 00053 ) { 00054 //DEBUG cerr << "ZMexLogBehavior::emit()" << endl; 00055 00056 // Do nothing with string& (but do it well!): 00057 return ZMexNOTLOGGED; 00058 } 00059 00060 bool ZMexLogBehavior::isTimeDesired() const { return true; } 00061 bool ZMexLogBehavior::isFilePathDesired() const { return true; } 00062 00063 // -------------- 00064 // ZMexLogNever:: 00065 // -------------- 00066 00067 ZMexLogNever::ZMexLogNever() 00068 : ZMexLogBehavior() 00069 { ; } 00070 00071 ZMexLogNever::~ZMexLogNever() { ; } 00072 00073 ZMexLogNever * 00074 ZMexLogNever::clone() const { return new ZMexLogNever( *this ); } 00075 00076 ZMexLogResult ZMexLogNever::emit( const ZMexception & ) { 00077 return ZMexNOTLOGGED; // 00078 } 00079 00080 ZMexLogResult ZMexLogNever::emit( 00081 const std::string & 00082 ) { 00083 //DEBUG cerr << "ZMexLogNever::emit()" << endl; 00084 00085 // Do nothing with string& (but do it well!): 00086 return ZMexNOTLOGGED; 00087 } 00088 00089 00090 // --------------- 00091 // ZMexLogAlways:: 00092 // --------------- 00093 00094 ZMexLogAlways::ZMexLogAlways( ) 00095 : ZMexLogBehavior() 00096 , myOs( std::cerr ) 00097 { ; } 00098 00099 ZMexLogAlways::ZMexLogAlways( std::ostream & os ) 00100 : ZMexLogBehavior() 00101 , myOs( os ) 00102 { ; } 00103 00104 ZMexLogAlways::~ZMexLogAlways() { ; } 00105 00106 ZMexLogAlways * 00107 ZMexLogAlways::clone() const { return new ZMexLogAlways( *this ); } 00108 00109 ZMexLogResult ZMexLogAlways::emit( const ZMexception & x ) { 00110 std::string s = x.logMessage(); // 00111 if ( s != "" ) 00112 return emit( s ); 00113 00114 x.logObject(); 00115 return ZMexLOGGED; 00116 } 00117 00118 ZMexLogResult ZMexLogAlways::emit( 00119 const std::string & s 00120 ) { 00121 //DEBUG cerr << "ZMexLogAlways::emit( \"" << s << "\" )" << endl; 00122 00123 // Emit the message, flushing the output right away: 00124 myOs << s << std::flush; 00125 return ZMexLOGGED; 00126 } 00127 00128 // --------------- 00129 // ZMexLogTwice:: 00130 // --------------- 00131 00132 ZMexLogTwice::ZMexLogTwice( std::ostream & os1 ) 00133 : ZMexLogBehavior() 00134 , myOs1( os1 ) 00135 , myOs2( std::cerr ) 00136 { ; } 00137 00138 ZMexLogTwice::ZMexLogTwice( std::ostream & os1, std::ostream & os2 ) 00139 : ZMexLogBehavior() 00140 , myOs1( os1 ) 00141 , myOs2( os2 ) 00142 { ; } 00143 00144 ZMexLogTwice::~ZMexLogTwice() { ; } 00145 00146 ZMexLogTwice * 00147 ZMexLogTwice::clone() const { return new ZMexLogTwice( *this ); } 00148 00149 ZMexLogResult ZMexLogTwice::emit( const ZMexception & x ) { 00150 std::string s = x.logMessage(); 00151 if (s != "") 00152 return emit( s ); 00153 00154 std::cerr << "WARNING: ZMexLogTwice() does not log in the usual manner for"; 00155 std::cerr << " SuperEx's.\n\t Its ostreams may not have received logs.\n"; 00156 x.logObject(); 00157 return ZMexLOGGED; 00158 } 00159 00160 ZMexLogResult ZMexLogTwice::emit( 00161 const std::string & s 00162 ) { 00163 //DEBUG cerr << "ZMexLogTwice::emit( \"" << s << "\" )" << endl; 00164 00165 // Emit the message, flushing the output right away: 00166 myOs1 << s << std::flush; 00167 myOs2 << s << std::flush; 00168 return ZMexLOGGED; 00169 } 00170 00171 00172 // ------------------ 00173 // ZMexLogViaParent:: 00174 // ------------------ 00175 00176 ZMexLogViaParent::ZMexLogViaParent() 00177 : ZMexLogBehavior() 00178 { ; } 00179 00180 ZMexLogViaParent::~ZMexLogViaParent() { ; } 00181 00182 ZMexLogViaParent * 00183 ZMexLogViaParent::clone() const { return new ZMexLogViaParent( *this ); } 00184 00185 ZMexLogResult ZMexLogViaParent::emit( const ZMexception & ) { 00186 return ZMexLOGVIAPARENT; // 00187 } 00188 00189 ZMexLogResult ZMexLogViaParent::emit( const std::string & ) { 00190 //DEBUG cerr << "ZMexLogViaParent::emit( \"" << s << "\" )" << endl; 00191 00192 // Bump logging decisions to someone else's logger: 00193 return ZMexLOGVIAPARENT; 00194 } 00195 00196 00197 // ------------------ 00198 // ZMexValidationStyle:: 00199 // ------------------ 00200 00201 ZMexValidationStyle::ZMexValidationStyle( ) 00202 : ZMexLogBehavior() 00203 , myOs( std::cerr ) 00204 { ; } 00205 00206 ZMexValidationStyle::ZMexValidationStyle( std::ostream & os ) 00207 : ZMexLogBehavior() 00208 , myOs( os ) 00209 { ; } 00210 00211 ZMexValidationStyle::~ZMexValidationStyle() { ; } 00212 00213 ZMexValidationStyle * 00214 ZMexValidationStyle::clone() const { return new ZMexValidationStyle( *this ); } 00215 00216 ZMexLogResult ZMexValidationStyle::emit( const ZMexception & x ) { 00217 std::string s = x.logMessage(); 00218 if ( s != "" ) 00219 return emit( s ); 00220 00221 x.logObject(); 00222 return ZMexLOGGED; 00223 } 00224 00225 ZMexLogResult ZMexValidationStyle::emit( 00226 const std::string & s 00227 ) { 00228 //DEBUG cerr << "ZMexValidationStyle::emit( \"" << s << "\" )" << endl; 00229 00230 // Emit the message, flushing the output right away: 00231 myOs << s << std::flush; 00232 return ZMexLOGGED; 00233 } 00234 00235 bool ZMexValidationStyle::isTimeDesired() const { return false; } 00236 bool ZMexValidationStyle::isFilePathDesired() const { return false; } 00237 00238 // ------------ 00239 // ZMexLogger:: 00240 // ------------ 00241 00242 ZMexLogger::ZMexLogger( 00243 const ZMexLogBehavior & desiredBehavior 00244 ) 00245 : ZMhandleTo<ZMexLogBehavior>( desiredBehavior ) 00246 { ; } 00247 // Construct logger with specified behavior. 00248 00249 ZMexLogger::~ZMexLogger() { ; } 00250 // Destroy logger with its behavior. 00251 00252 ZMexLogResult ZMexLogger::emit( const ZMexception & exc ) { 00253 return rep_->emit( exc ); 00254 } 00255 // Force the given exception's message into the log. 00256 00257 ZMexLogResult ZMexLogger::emit( const std::string & message ) { 00258 return rep_->emit( message ); 00259 } 00260 // Force the given message into the log. 00261 00262 ZMexLogBehavior * ZMexLogger::control() { return rep_; } 00263 // Grant access to the representation 00264 // to permit calling specialized behavior functions. 00265 00266 00267 // ---------------------------------------------------------------------- 00268 00269 00270 } // namespace zmex