00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 #include "FWCore/MessageService/interface/ELoutput.h"
00077 #include "FWCore/MessageService/interface/ELadministrator.h"
00078 #include "FWCore/MessageService/interface/ELcontextSupplier.h"
00079
00080 #include "FWCore/MessageLogger/interface/ErrorObj.h"
00081
00082 #include "FWCore/Utilities/interface/do_nothing_deleter.h"
00083
00084
00085
00086
00087
00088
00089 #include <iostream>
00090 #include <fstream>
00091 #include <cstring>
00092
00093 namespace edm {
00094 namespace service {
00095
00096
00097
00098
00099
00100
00101 static ELstring formatTime( const time_t t ) {
00102
00103 static char const dummy[] = "dd-Mon-yyyy hh:mm:ss TZN ";
00104 char ts[sizeof(dummy)];
00105
00106 struct tm timebuf;
00107
00108 strftime( ts, sizeof(dummy), "%d-%b-%Y %H:%M:%S %Z", localtime_r(&t, &timebuf) );
00109
00110
00111 #ifdef STRIP_TRAILING_BLANKS_IN_TIMEZONE
00112
00113
00114 unsigned int b = strlen(ts);
00115 while (ts[--b] == ' ') {ts[b] = 0;}
00116 #endif
00117
00118 ELstring result(ts);
00119 return result;
00120 }
00121
00122
00123
00124
00125
00126
00127 ELoutput::ELoutput()
00128 : ELdestination ( )
00129 , os ( &std::cerr, do_nothing_deleter() )
00130 , charsOnLine ( 0 )
00131 , xid ( )
00132 , wantTimestamp ( true )
00133 , wantModule ( true )
00134 , wantSubroutine ( true )
00135 , wantText ( true )
00136 , wantSomeContext ( true )
00137 , wantSerial ( false )
00138 , wantFullContext ( false )
00139 , wantTimeSeparate ( false )
00140 , wantEpilogueSeparate( false )
00141 , preambleMode ( true )
00142 {
00143
00144 #ifdef ELoutputCONSTRUCTOR_TRACE
00145 std::cerr << "Constructor for ELoutput()\n";
00146 #endif
00147
00148 emitToken( "\n=================================================", true );
00149 emitToken( "\nMessage Log File written by MessageLogger service \n" );
00150 emitToken( "\n=================================================\n", true );
00151
00152 }
00153
00154
00155 ELoutput::ELoutput( std::ostream & os_ , bool emitAtStart )
00156 : ELdestination ( )
00157 , os ( &os_, do_nothing_deleter() )
00158 , charsOnLine ( 0 )
00159 , xid ( )
00160 , wantTimestamp ( true )
00161 , wantModule ( true )
00162 , wantSubroutine ( true )
00163 , wantText ( true )
00164 , wantSomeContext ( true )
00165 , wantSerial ( false )
00166 , wantFullContext ( false )
00167 , wantTimeSeparate ( false )
00168 , wantEpilogueSeparate( false )
00169 , preambleMode ( true )
00170 {
00171
00172 #ifdef ELoutputCONSTRUCTOR_TRACE
00173 std::cerr << "Constructor for ELoutput( os )\n";
00174 #endif
00175
00176
00177 if (emitAtStart) {
00178 preambleMode = true;
00179 emitToken( "\n=================================================", true );
00180 emitToken( "\nMessage Log File written by MessageLogger service \n" );
00181 emitToken( "\n=================================================\n", true );
00182 }
00183
00184 }
00185
00186
00187 ELoutput::ELoutput( const ELstring & fileName, bool emitAtStart )
00188 : ELdestination ( )
00189 , os ( new std::ofstream( fileName.c_str() , std::ios::app), close_and_delete())
00190 , charsOnLine ( 0 )
00191 , xid ( )
00192 , wantTimestamp ( true )
00193 , wantModule ( true )
00194 , wantSubroutine ( true )
00195 , wantText ( true )
00196 , wantSomeContext ( true )
00197 , wantSerial ( false )
00198 , wantFullContext ( false )
00199 , wantTimeSeparate ( false )
00200 , wantEpilogueSeparate( false )
00201 , preambleMode ( true )
00202 {
00203
00204 #ifdef ELoutputCONSTRUCTOR_TRACE
00205 std::cerr << "Constructor for ELoutput( " << fileName << " )\n";
00206 #endif
00207
00208 preambleMode = true;
00209 if ( os && *os ) {
00210 #ifdef ELoutputCONSTRUCTOR_TRACE
00211 std::cerr << " Testing if os is owned\n";
00212 #endif
00213 #ifdef ELoutputCONSTRUCTOR_TRACE
00214 std::cerr << " About to do first emit\n";
00215 #endif
00216
00217 if (emitAtStart) {
00218 emitToken( "\n=======================================================",
00219 true );
00220 emitToken( "\nError Log File " );
00221 emitToken( fileName );
00222 emitToken( " \n" );
00223 }
00224 }
00225 else {
00226 #ifdef ELoutputCONSTRUCTOR_TRACE
00227 std::cerr << " Deleting os\n";
00228 #endif
00229 os.reset(&std::cerr, do_nothing_deleter());
00230 #ifdef ELoutputCONSTRUCTOR_TRACE
00231 std::cerr << " about to emit to cerr\n";
00232 #endif
00233 if (emitAtStart) {
00234 emitToken( "\n=======================================================",
00235 true );
00236 emitToken( "\n%MSG** Logging to cerr is being substituted" );
00237 emitToken( " for specified log file \"" );
00238 emitToken( fileName );
00239 emitToken( "\" which could not be opened for write or append.\n" );
00240 }
00241 }
00242 if (emitAtStart) {
00243 ELstring const& ftime = formatTime(time(0));
00244 emitToken( ftime, true );
00245 emitToken( "\n=======================================================\n",
00246 true );
00247 }
00248
00249
00250 #ifdef ELoutputCONSTRUCTOR_TRACE
00251 std::cerr << "Constructor for ELoutput completed.\n";
00252 #endif
00253
00254 }
00255
00256
00257 ELoutput::ELoutput( const ELoutput & orig )
00258 : ELdestination ( )
00259 , os ( orig.os )
00260 , charsOnLine ( orig.charsOnLine )
00261 , xid ( orig.xid )
00262 , wantTimestamp ( orig.wantTimestamp )
00263 , wantModule ( orig.wantModule )
00264 , wantSubroutine ( orig.wantSubroutine )
00265 , wantText ( orig.wantText )
00266 , wantSomeContext ( orig.wantSomeContext )
00267 , wantSerial ( orig.wantSerial )
00268 , wantFullContext ( orig.wantFullContext )
00269 , wantTimeSeparate ( orig.wantTimeSeparate )
00270 , wantEpilogueSeparate( orig.wantEpilogueSeparate )
00271 , preambleMode ( orig.preambleMode )
00272 {
00273
00274 #ifdef ELoutputCONSTRUCTOR_TRACE
00275 std::cerr << "Copy constructor for ELoutput\n";
00276 #endif
00277
00278
00279 threshold = orig.threshold;
00280 traceThreshold = orig.traceThreshold;
00281 limits = orig.limits;
00282 preamble = orig.preamble;
00283 newline = orig.newline;
00284 indent = orig.indent;
00285 lineLength = orig.lineLength;
00286
00287 ignoreMostModules = orig.ignoreMostModules;
00288 respondToThese = orig.respondToThese;
00289 respondToMostModules = orig.respondToMostModules;
00290 ignoreThese = orig.ignoreThese;
00291
00292 }
00293
00294
00295 ELoutput::~ELoutput() {
00296
00297 #ifdef ELoutputCONSTRUCTOR_TRACE
00298 std::cerr << "Destructor for ELoutput\n";
00299 #endif
00300
00301 }
00302
00303
00304
00305
00306
00307
00308 ELoutput *
00309 ELoutput::clone() const {
00310
00311 return new ELoutput( *this );
00312
00313 }
00314
00315
00316
00317
00318 bool ELoutput::log( const edm::ErrorObj & msg ) {
00319
00320 #ifdef ELoutputTRACE_LOG
00321 std::cerr << " =:=:=: Log to an ELoutput \n";
00322 #endif
00323
00324 xid = msg.xid();
00325
00326 #ifdef THRESHTRACE
00327 std::cerr << " =:=:=: Log to an ELoutput \n"
00328 << " severity = " << xid.severity << "\n"
00329 << " threshold = " << threshold << "\n"
00330 << " id = " << xid.id << "\n";
00331 #endif
00332
00333
00334
00335
00336 if ( xid.severity < threshold ) return false;
00337 if ( thisShouldBeIgnored(xid.module)
00338 && (xid.severity < ELsevere) )
00339 return false;
00340 if ( ! limits.add( xid )
00341 && (xid.severity < ELsevere) )
00342 return false;
00343
00344 #ifdef ELoutputTRACE_LOG
00345 std::cerr << " =:=:=: Limits table work done \n";
00346 #endif
00347
00348
00349
00350 preambleMode = true;
00351
00352 if ( !msg.is_verbatim() ) {
00353 charsOnLine = 0;
00354 emitToken( preamble );
00355 emitToken( xid.severity.getSymbol() );
00356 emitToken( " " );
00357 emitToken( xid.id );
00358 emitToken( msg.idOverflow() );
00359 emitToken( ": " );
00360 }
00361
00362 #ifdef ELoutputTRACE_LOG
00363 std::cerr << " =:=:=: Prologue done \n";
00364 #endif
00365
00366
00367 if ( !msg.is_verbatim() )
00368 {
00369 if ( wantSerial ) {
00370 std::ostringstream s;
00371 s << msg.serial();
00372 emitToken( "[serial #" + s.str() + ELstring("] ") );
00373 }
00374 }
00375
00376 #ifdef OUTPUT_FORMATTED_ERROR_MESSAGES
00377
00378
00379 if ( wantText ) {
00380 ELlist_string::const_iterator it;
00381 for ( it = msg.items().begin(); it != msg.items().end(); ++it ) {
00382 #ifdef ELoutputTRACE_LOG
00383 std::cerr << " =:=:=: Item: " << *it << '\n';
00384 #endif
00385 emitToken( *it );
00386 }
00387 }
00388 #endif
00389
00390
00391
00392 bool needAspace = true;
00393 if ( !msg.is_verbatim() )
00394 {
00395 if ( wantEpilogueSeparate ) {
00396 if ( xid.module.length() + xid.subroutine.length() > 0 ) {
00397 emitToken("\n");
00398 needAspace = false;
00399 }
00400 else if ( wantTimestamp && !wantTimeSeparate ) {
00401 emitToken("\n");
00402 needAspace = false;
00403 }
00404 }
00405 if ( wantModule && (xid.module.length() > 0) ) {
00406 if (needAspace) { emitToken(ELstring(" ")); needAspace = false; }
00407 emitToken( xid.module + ELstring(" ") );
00408 }
00409 if ( wantSubroutine && (xid.subroutine.length() > 0) ) {
00410 if (needAspace) { emitToken(ELstring(" ")); needAspace = false; }
00411 emitToken( xid.subroutine + "()" + ELstring(" ") );
00412 }
00413 }
00414
00415 #ifdef ELoutputTRACE_LOG
00416 std::cerr << " =:=:=: Module and Subroutine done \n";
00417 #endif
00418
00419
00420
00421 if ( !msg.is_verbatim() )
00422 {
00423 if ( wantTimestamp ) {
00424 if ( wantTimeSeparate ) {
00425 emitToken( ELstring("\n") );
00426 needAspace = false;
00427 }
00428 if (needAspace) { emitToken(ELstring(" ")); needAspace = false; }
00429 ELstring const& ftime = formatTime(msg.timestamp());
00430 emitToken( ftime + ELstring(" ") );
00431 }
00432 }
00433
00434 #ifdef ELoutputTRACE_LOG
00435 std::cerr << " =:=:=: TimeStamp done \n";
00436 #endif
00437
00438
00439
00440 if ( !msg.is_verbatim() )
00441 {
00442 if ( wantSomeContext ) {
00443 if (needAspace) { emitToken(ELstring(" ")); needAspace = false; }
00444 #ifdef ELoutputTRACE_LOG
00445 std::cerr << " =:=:=:>> context supplier is at 0x"
00446 << std::hex
00447 << &ELadministrator::instance()->getContextSupplier() << '\n';
00448 std::cerr << " =:=:=:>> context is --- "
00449 << ELadministrator::instance()->getContextSupplier().context()
00450 << '\n';
00451 #endif
00452 if ( wantFullContext ) {
00453 emitToken( ELadministrator::instance()->getContextSupplier().fullContext());
00454 #ifdef ELoutputTRACE_LOG
00455 std::cerr << " =:=:=: fullContext done: \n";
00456 #endif
00457 } else {
00458 emitToken( ELadministrator::instance()->getContextSupplier().context());
00459 #ifdef ELoutputTRACE_LOG
00460 std::cerr << " =:=:=: Context done: \n";
00461 #endif
00462 }
00463 }
00464 }
00465
00466
00467
00468
00469 bool insertNewlineAfterHeader = ( msg.xid().severity != ELsuccess );
00470
00471
00472 if ( !msg.is_verbatim() )
00473 {
00474 if ( msg.xid().severity >= traceThreshold ) {
00475 emitToken( ELstring("\n")
00476 + ELadministrator::instance()->getContextSupplier().traceRoutine()
00477 , insertNewlineAfterHeader );
00478 }
00479 else {
00480 emitToken("", insertNewlineAfterHeader);
00481 }
00482 }
00483 #ifdef ELoutputTRACE_LOG
00484 std::cerr << " =:=:=: Trace routine done: \n";
00485 #endif
00486
00487 #ifndef OUTPUT_FORMATTED_ERROR_MESSAGES
00488
00489
00490 preambleMode = false;
00491 if ( wantText ) {
00492 ELlist_string::const_iterator it;
00493 int item_count = 0;
00494 for ( it = msg.items().begin(); it != msg.items().end(); ++it ) {
00495 #ifdef ELoutputTRACE_LOG
00496 std::cerr << " =:=:=: Item: " << *it << '\n';
00497 #endif
00498 ++item_count;
00499 if ( !msg.is_verbatim() ) {
00500 if ( !insertNewlineAfterHeader && (item_count == 3) ) {
00501
00502 emitToken( *it, true );
00503 } else {
00504 emitToken( *it );
00505 }
00506 } else {
00507 emitToken( *it );
00508 }
00509 }
00510 }
00511 #endif
00512
00513
00514
00515
00516 if ( !msg.is_verbatim() )
00517 {
00518 emitToken("\n%MSG");
00519 }
00520
00521
00522
00523
00524
00525 (*os) << newline;
00526 flush();
00527
00528
00529 #ifdef ELoutputTRACE_LOG
00530 std::cerr << " =:=:=: log(msg) done: \n";
00531 #endif
00532
00533 return true;
00534
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544 void ELoutput::emitToken( const ELstring & s, bool nl ) {
00545
00546 #ifdef ELoutput_EMIT_TRACE
00547 std::cerr << "[][][] in emit: charsOnLine is " << charsOnLine << '\n';
00548 std::cerr << "[][][] in emit: s.length() " << s.length() << '\n';
00549 std::cerr << "[][][] in emit: lineLength is " << lineLength << '\n';
00550 #endif
00551
00552 if (s.length() == 0) {
00553 if ( nl ) {
00554 (*os) << newline << std::flush;
00555 charsOnLine = 0;
00556 }
00557 return;
00558 }
00559
00560 char first = s[0];
00561 char second,
00562 last,
00563 last2;
00564 second = (s.length() < 2) ? '\0' : s[1];
00565 last = (s.length() < 2) ? '\0' : s[s.length()-1];
00566 last2 = (s.length() < 3) ? '\0' : s[s.length()-2];
00567
00568
00569
00570 if (preambleMode) {
00571
00572 if ( first == '\n'
00573 || (charsOnLine + static_cast<int>(s.length())) > lineLength ) {
00574 #ifdef ELoutput_EMIT_TRACE
00575 std::cerr << "[][][] in emit: about to << to *os \n";
00576 #endif
00577 #ifdef HEADERS_BROKEN_INTO_LINES_AND_INDENTED
00578
00579 (*os) << newline << indent;
00580 charsOnLine = indent.length();
00581 #else
00582 charsOnLine = 0;
00583 #endif
00584 if (second != ' ') {
00585 (*os) << ' ';
00586 charsOnLine++;
00587 }
00588 if ( first == '\n' ) {
00589 (*os) << s.substr(1);
00590 }
00591 else {
00592 (*os) << s;
00593 }
00594 }
00595 #ifdef ELoutput_EMIT_TRACE
00596 std::cerr << "[][][] in emit: about to << s to *os: " << s << " \n";
00597 #endif
00598 else {
00599 (*os) << s;
00600 }
00601
00602 if (last == '\n' || last2 == '\n') {
00603 (*os) << indent;
00604 if (last != ' ')
00605 (*os) << ' ';
00606 charsOnLine = indent.length() + 1;
00607 }
00608
00609 if ( nl ) { (*os) << newline << std::flush; charsOnLine = 0; }
00610 else { charsOnLine += s.length(); }
00611 }
00612
00613 if (!preambleMode) {
00614 (*os) << s;
00615 }
00616
00617 #ifdef ELoutput_EMIT_TRACE
00618 std::cerr << "[][][] in emit: completed \n";
00619 #endif
00620
00621 }
00622
00623
00624
00625
00626
00627
00628 void ELoutput::includeTime() { wantTimestamp = true; }
00629 void ELoutput::suppressTime() { wantTimestamp = false; }
00630
00631 void ELoutput::includeModule() { wantModule = true; }
00632 void ELoutput::suppressModule() { wantModule = false; }
00633
00634 void ELoutput::includeSubroutine() { wantSubroutine = true; }
00635 void ELoutput::suppressSubroutine() { wantSubroutine = false; }
00636
00637 void ELoutput::includeText() { wantText = true; }
00638 void ELoutput::suppressText() { wantText = false; }
00639
00640 void ELoutput::includeContext() { wantSomeContext = true; }
00641 void ELoutput::suppressContext() { wantSomeContext = false; }
00642
00643 void ELoutput::suppressSerial() { wantSerial = false; }
00644 void ELoutput::includeSerial() { wantSerial = true; }
00645
00646 void ELoutput::useFullContext() { wantFullContext = true; }
00647 void ELoutput::useContext() { wantFullContext = false; }
00648
00649 void ELoutput::separateTime() { wantTimeSeparate = true; }
00650 void ELoutput::attachTime() { wantTimeSeparate = false; }
00651
00652 void ELoutput::separateEpilogue() { wantEpilogueSeparate = true; }
00653 void ELoutput::attachEpilogue() { wantEpilogueSeparate = false; }
00654
00655
00656
00657
00658
00659
00660 void ELoutput::summarization(
00661 const ELstring & fullTitle
00662 , const ELstring & sumLines
00663 ) {
00664 const int titleMaxLength( 40 );
00665
00666
00667
00668 ELstring title( fullTitle, 0, titleMaxLength );
00669 int q = (lineLength - title.length() - 2) / 2;
00670 ELstring line(q, '=');
00671 emitToken( "", true );
00672 emitToken( line );
00673 emitToken( " " );
00674 emitToken( title );
00675 emitToken( " " );
00676 emitToken( line, true );
00677
00678
00679
00680 *os << sumLines;
00681
00682
00683
00684 emitToken( "", true );
00685 emitToken( ELstring(lineLength, '='), true );
00686
00687 }
00688
00689
00690
00691
00692
00693
00694 void ELoutput::changeFile (std::ostream & os_) {
00695 os.reset(&os_, do_nothing_deleter());
00696 emitToken( "\n=======================================================", true );
00697 emitToken( "\nError Log changed to this stream\n" );
00698 ELstring const& ftime = formatTime(time(0));
00699 emitToken( ftime, true );
00700 emitToken( "\n=======================================================\n", true );
00701 }
00702
00703 void ELoutput::changeFile (const ELstring & filename) {
00704 os.reset(new std::ofstream( filename.c_str(), std::ios::app), close_and_delete());
00705 emitToken( "\n=======================================================", true );
00706 emitToken( "\nError Log changed to this file\n" );
00707 ELstring const& ftime = formatTime(time(0));
00708 emitToken( ftime, true );
00709 emitToken( "\n=======================================================\n", true );
00710 }
00711
00712 void ELoutput::flush() {
00713 os->flush();
00714 }
00715
00716
00717
00718
00719
00720 }
00721 }