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 #include "CeylanLogPlug.h"
00028
00029 #include "CeylanLogSource.h"
00030 #include "CeylanMuteLogSource.h"
00031
00032 #include "CeylanLogTransport.h"
00033 #include "CeylanLogListener.h"
00034 #include "CeylanLogAggregator.h"
00035
00036 #include "CeylanLogLight.h"
00037 #include "CeylanDirectory.h"
00038 #include "CeylanUtils.h"
00039 #include "CeylanStringUtils.h"
00040 #include "CeylanOperators.h"
00041
00042 #include <iostream>
00043
00044
00045 using std::string ;
00046 using std::list ;
00047
00048
00049 using namespace Ceylan::Log ;
00050
00051
00052 #ifdef CEYLAN_USES_CONFIG_H
00053 #include "CeylanConfig.h"
00054 #endif // CEYLAN_USES_CONFIG_H
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 LogSource * LogPlug::InfoLogSource = 0 ;
00065 LogSource * LogPlug::TraceLogSource = 0 ;
00066 LogSource * LogPlug::DebugLogSource = 0 ;
00067 LogSource * LogPlug::WarningLogSource = 0 ;
00068 LogSource * LogPlug::ErrorLogSource = 0 ;
00069 LogSource * LogPlug::FatalLogSource = 0 ;
00070
00071 LogSource * LogPlug::LogrootLogSource = 0 ;
00072
00073 LogTransport * LogPlug::Transport = 0 ;
00074 LogListener * LogPlug::Listener = 0 ;
00075 LogAggregator * LogPlug::Aggregator = 0 ;
00076
00077
00078 string LogPlug::FullExecutablePath = "" ;
00079
00080
00081 string LogPlug::SourceName = "<no source specified>" ;
00082
00083
00084
00085 const string LogPlug::LogSystemNotInitialized =
00086 "Attempt of using the Log system whereas it has not been initialized yet. "
00087 "Consider using LogPlug<Implementation>::StartService, "
00088 "for example LogPlugClassical::StartService" ;
00089
00090
00091
00092 void LogPlug::SetInfoLogSource( LogSource & newInfoLogSource )
00093 {
00094
00095 InfoLogSource = & newInfoLogSource ;
00096
00097 }
00098
00099
00100
00101 LogSource & LogPlug::GetInfoLogSource()
00102 {
00103
00104 return * InfoLogSource ;
00105
00106 }
00107
00108
00109
00110 void LogPlug::SetTraceLogSource( LogSource & newTraceLogSource )
00111 {
00112
00113 TraceLogSource = & newTraceLogSource ;
00114
00115 }
00116
00117
00118
00119 LogSource & LogPlug::GetTraceLogSource()
00120 {
00121
00122 return * TraceLogSource ;
00123
00124 }
00125
00126
00127
00128 void LogPlug::SetDebugLogSource( LogSource & newDebugLogSource )
00129 {
00130
00131 DebugLogSource = & newDebugLogSource ;
00132
00133 }
00134
00135
00136
00137 LogSource & LogPlug::GetDebugLogSource()
00138 {
00139
00140 return * DebugLogSource ;
00141
00142 }
00143
00144
00145
00146 void LogPlug::SetWarningLogSource( LogSource & newWarningLogSource )
00147 {
00148
00149 WarningLogSource = & newWarningLogSource ;
00150
00151 }
00152
00153
00154
00155 LogSource & LogPlug::GetWarningLogSource()
00156 {
00157
00158 return * WarningLogSource ;
00159
00160 }
00161
00162
00163
00164 void LogPlug::SetErrorLogSource( LogSource & newErrorLogSource )
00165 {
00166
00167 ErrorLogSource = & newErrorLogSource ;
00168
00169 }
00170
00171
00172
00173 LogSource & LogPlug::GetErrorLogSource()
00174 {
00175
00176 return * ErrorLogSource ;
00177
00178 }
00179
00180
00181
00182 void LogPlug::SetFatalLogSource( LogSource & newFatalLogSource )
00183 {
00184
00185 FatalLogSource = & newFatalLogSource ;
00186
00187 }
00188
00189
00190
00191 LogSource & LogPlug::GetFatalLogSource()
00192 {
00193
00194 return * FatalLogSource ;
00195
00196 }
00197
00198
00199
00200 bool LogPlug::IsFatalLogSourceAvailable()
00201 {
00202
00203 return ( FatalLogSource != 0 ) ;
00204
00205 }
00206
00207
00208
00209 void LogPlug::SetLogRootLogSource( LogSource & newLogRootLogSource )
00210 {
00211
00212 LogrootLogSource = & newLogRootLogSource ;
00213
00214 }
00215
00216
00217
00218 LogSource & LogPlug::GetLogRootLogSource()
00219 {
00220
00221 return * LogrootLogSource ;
00222
00223 }
00224
00225
00226
00227 void LogPlug::SetTransport( LogTransport & newTransport )
00228 {
00229
00230 Transport = & newTransport ;
00231
00232 }
00233
00234
00235
00236 LogTransport & LogPlug::GetTransport()
00237 {
00238
00239 return *Transport ;
00240
00241 }
00242
00243
00244
00245 void LogPlug::SetListener( LogListener & newListener )
00246 {
00247
00248 Listener = & newListener ;
00249
00250 }
00251
00252
00253
00254 LogListener & LogPlug::GetListener()
00255 {
00256
00257 return * Listener ;
00258
00259 }
00260
00261
00262
00263 void LogPlug::SetAggregator( LogAggregator & newAggregator )
00264 {
00265
00266 Aggregator = & newAggregator ;
00267
00268 }
00269
00270
00271
00272 LogAggregator & LogPlug::GetAggregator()
00273 {
00274
00275 return * Aggregator ;
00276
00277 }
00278
00279
00280
00281 void LogPlug::CheckBlank()
00282 {
00283
00284 if ( LogrootLogSource != 0 )
00285 throw LogException( "LogPlug::StartService: there was already a Log "
00286 "source assigned to LogPlug::logroot." ) ;
00287
00288 if ( InfoLogSource != 0 )
00289 throw LogException( "LogPlug::StartService: there was already a Log "
00290 "source assigned to LogPlug::info." ) ;
00291
00292 if ( TraceLogSource != 0 )
00293 throw LogException( "LogPlug::StartService: there was already a Log "
00294 "source assigned to LogPlug::trace." ) ;
00295
00296 if ( DebugLogSource != 0 )
00297 throw LogException( "LogPlug::StartService: there was already a Log "
00298 "source assigned to LogPlug::debug." ) ;
00299
00300 if ( WarningLogSource != 0 )
00301 throw LogException( "LogPlug::StartService: there was already a Log "
00302 "source assigned to LogPlug::warning." ) ;
00303
00304 if ( ErrorLogSource != 0 )
00305 throw LogException( "LogPlug::StartService: there was already a Log "
00306 "source assigned to LogPlug::error." ) ;
00307
00308 if ( FatalLogSource != 0 )
00309 throw LogException( "LogPlug::StartService: there was already a Log "
00310 "source assigned to LogPlug::fatal." ) ;
00311
00312
00313 if ( Transport != 0 )
00314 throw LogException( "LogPlug::StartService: LogPlug::Transport "
00315 "was already assigned." ) ;
00316
00317 if ( Listener != 0 )
00318 throw LogException( "LogPlug::StartService: LogPlug::Listener "
00319 "was already assigned." ) ;
00320
00321 if ( Aggregator != 0 )
00322 throw LogException( "LogPlug::StartService: LogPlug::Aggregator "
00323 "was already assigned." ) ;
00324
00325 }
00326
00327
00328
00329 void LogPlug::SetFullExecutablePath( const string & plugInitiatorFullName )
00330 {
00331
00332 if ( FullExecutablePath.size() != 0 )
00333 throw LogException( "LogPlug::SetFullExecutablePath: "
00334 "path already set." ) ;
00335
00336 FullExecutablePath = plugInitiatorFullName ;
00337
00338 }
00339
00340
00341
00342 std::string LogPlug::GetFullExecutablePath()
00343 {
00344
00345 if ( FullExecutablePath.size() == 0 )
00346 throw LogException( "LogPlug::GetFullExecutablePath: "
00347 "no path available." ) ;
00348
00349 return FullExecutablePath ;
00350
00351 }
00352
00353
00354
00355 std::string LogPlug::GetSpeakerNameFrom( const string & plugInitiatorFullName )
00356 {
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 string speakerName ;
00378
00379
00380 #if CEYLAN_ARCH_NINTENDO_DS
00381
00382
00383
00384
00385
00386
00387
00388 speakerName = "DS" ;
00389
00390 #else // CEYLAN_ARCH_NINTENDO_DS
00391
00392 try
00393 {
00394
00395 Ceylan::System::Directory::StripFilename(
00396 plugInitiatorFullName,
00397 0, & speakerName ) ;
00398
00399 }
00400 catch( const System::DirectoryException & e )
00401 {
00402 throw LogException( "GetSpeakerNameFrom failed: " + e.toString() ) ;
00403 }
00404
00405
00406 #endif // CEYLAN_ARCH_NINTENDO_DS
00407
00408 CEYLAN_LOG( "LogHolder: speaker name for logs will be " + speakerName ) ;
00409
00410 return speakerName ;
00411
00412 }
00413
00414
00415
00416 void LogPlug::CreateBasicPlug()
00417 {
00418
00419 CEYLAN_LOG( "Creating default standard channels." ) ;
00420
00421 if ( Transport == 0 )
00422 throw LogException( "LogPlug::CreateBasicPlug: "
00423 "no transport available" ) ;
00424
00425
00426
00427 LogrootLogSource = new LogSource( "Log root", *Transport ) ;
00428
00429 FatalLogSource = new LogSource( "Fatal", *Transport ) ;
00430
00431 ErrorLogSource = new LogSource( "Error", *Transport ) ;
00432
00433 WarningLogSource = new LogSource( "Warning", *Transport ) ;
00434
00435 DebugLogSource = new LogSource( "Debug", *Transport ) ;
00436
00437 TraceLogSource = new LogSource( "Trace", *Transport ) ;
00438
00439 InfoLogSource = new LogSource( "Info", *Transport ) ;
00440
00441
00442 LogrootLogSource->send( "Starting log plug service, from Ceylan "
00443 + Ceylan::GetVersion().toString() + "." ) ;
00444
00445
00446
00447 #if ! CEYLAN_ARCH_NINTENDO_DS
00448
00449 LogrootLogSource->send( "Fatal standard log channel created." ) ;
00450 LogrootLogSource->send( "Error standard log channel created." ) ;
00451 LogrootLogSource->send( "Warning standard log channel created." ) ;
00452 LogrootLogSource->send( "Debug standard log channel created." ) ;
00453 LogrootLogSource->send( "Trace standard log channel created." ) ;
00454 LogrootLogSource->send( "Info standard log channel created." ) ;
00455
00456 #endif // CEYLAN_ARCH_NINTENDO_DS
00457
00458 }
00459
00460
00461
00462 void LogPlug::CreateNullPlug()
00463 {
00464
00465 CEYLAN_LOG( "Creating muted null channels." ) ;
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 LogrootLogSource = new MuteLogSource() ;
00479
00480 FatalLogSource = new MuteLogSource() ;
00481
00482 ErrorLogSource = new MuteLogSource() ;
00483
00484 WarningLogSource = new MuteLogSource() ;
00485
00486 DebugLogSource = new MuteLogSource() ;
00487
00488 TraceLogSource = new MuteLogSource() ;
00489
00490 InfoLogSource = new MuteLogSource() ;
00491
00492 }
00493
00494
00495
00496 void LogPlug::StartService( const string & plugCreator )
00497 {
00498
00499 SourceName = plugCreator ;
00500
00501 if ( LogrootLogSource == 0 )
00502 throw LogException( "No Log source assigned to LogPlug::logroot" ) ;
00503
00504 if ( InfoLogSource == 0 )
00505 throw LogException( "No Log source assigned to LogPlug::info" ) ;
00506
00507 if ( TraceLogSource == 0 )
00508 throw LogException( "No Log source assigned to LogPlug::trace" ) ;
00509
00510 if ( DebugLogSource == 0 )
00511 throw LogException( "No Log source assigned to LogPlug::debug" ) ;
00512
00513 if ( WarningLogSource == 0 )
00514 throw LogException( "No Log source assigned to LogPlug::warning" ) ;
00515
00516 if ( ErrorLogSource == 0 )
00517 throw LogException( "No Log source assigned to LogPlug::error" ) ;
00518
00519 if ( FatalLogSource == 0 )
00520 throw LogException( "No Log source assigned to LogPlug::fatal" ) ;
00521
00522 }
00523
00524
00525
00526 void LogPlug::StopService( bool warnIfAlreadyStopped )
00527 {
00528
00529 if ( InfoLogSource != 0 )
00530 {
00531
00532 #if ! CEYLAN_ARCH_NINTENDO_DS
00533 info( "Stopping channel." ) ;
00534 logroot( "Stopping channel Info." ) ;
00535 #endif // CEYLAN_ARCH_NINTENDO_DS
00536
00537 delete InfoLogSource ;
00538 InfoLogSource = 0 ;
00539 }
00540
00541
00542 if ( WarningLogSource != 0 )
00543 {
00544
00545 #if ! CEYLAN_ARCH_NINTENDO_DS
00546 warning( "Stopping channel." ) ;
00547 logroot( "Stopping channel Warning." ) ;
00548 #endif // CEYLAN_ARCH_NINTENDO_DS
00549
00550 delete WarningLogSource ;
00551 WarningLogSource = 0 ;
00552 }
00553
00554
00555 if ( TraceLogSource != 0 )
00556 {
00557
00558 #if ! CEYLAN_ARCH_NINTENDO_DS
00559 trace( "Stopping channel." ) ;
00560 logroot( "Stopping channel Trace." ) ;
00561 #endif // CEYLAN_ARCH_NINTENDO_DS
00562
00563 delete TraceLogSource ;
00564 TraceLogSource = 0 ;
00565 }
00566
00567
00568 if ( DebugLogSource != 0 )
00569 {
00570
00571 #if ! CEYLAN_ARCH_NINTENDO_DS
00572 debug( "Stopping channel." ) ;
00573 logroot( "Stopping channel Debug." ) ;
00574 #endif // CEYLAN_ARCH_NINTENDO_DS
00575
00576 delete DebugLogSource ;
00577 DebugLogSource = 0 ;
00578 }
00579
00580
00581 if ( ErrorLogSource != 0 )
00582 {
00583
00584 #if ! CEYLAN_ARCH_NINTENDO_DS
00585 error( "Stopping channel." ) ;
00586 logroot( "Stopping channel Error." ) ;
00587 #endif // CEYLAN_ARCH_NINTENDO_DS
00588
00589 delete ErrorLogSource ;
00590 ErrorLogSource = 0 ;
00591 }
00592
00593
00594 if ( FatalLogSource != 0 )
00595 {
00596
00597 #if ! CEYLAN_ARCH_NINTENDO_DS
00598 fatal( "Stopping channel." ) ;
00599 logroot( "Stopping channel Fatal." ) ;
00600 #endif // CEYLAN_ARCH_NINTENDO_DS
00601
00602 delete FatalLogSource ;
00603 FatalLogSource = 0 ;
00604 }
00605
00606
00607 if ( LogrootLogSource != 0 )
00608 {
00609
00610 #if ! CEYLAN_ARCH_NINTENDO_DS
00611 logroot( "Stopping log plug service." ) ;
00612 delete LogrootLogSource ;
00613 #endif // CEYLAN_ARCH_NINTENDO_DS
00614
00615 LogrootLogSource = 0 ;
00616 }
00617 else
00618 {
00619
00620 if ( warnIfAlreadyStopped )
00621 std::cerr << "Error in LogPlug::StopService: "
00622 "no log root channel available, "
00623 "maybe LogPlug::StopService was called more than once?"
00624 << std::endl ;
00625 }
00626
00627 }
00628
00629
00630
00631 const string LogPlug::GetSourceName()
00632 {
00633
00634 return SourceName ;
00635
00636 }
00637
00638
00639
00640 const string LogPlug::ToString( Ceylan::VerbosityLevels level )
00641 {
00642
00643 std::list<string> res ;
00644
00645 if ( Aggregator != 0 )
00646 res.push_back( Aggregator->toString() ) ;
00647 else
00648 res.push_back( "no aggregator connected." ) ;
00649
00650
00651 if ( InfoLogSource != 0 )
00652 res.push_back( InfoLogSource->toString() ) ;
00653 else
00654 res.push_back( "no info log source connected." ) ;
00655
00656 if ( TraceLogSource != 0 )
00657 res.push_back( TraceLogSource->toString() ) ;
00658 else
00659 res.push_back( "no trace log source connected." ) ;
00660
00661 if ( DebugLogSource != 0 )
00662 res.push_back( DebugLogSource->toString() ) ;
00663 else
00664 res.push_back( "no debug log source connected." ) ;
00665
00666 if ( WarningLogSource != 0 )
00667 res.push_back( WarningLogSource->toString() ) ;
00668 else
00669 res.push_back( "no warning log source connected." ) ;
00670
00671 if ( ErrorLogSource != 0 )
00672 res.push_back( ErrorLogSource->toString() ) ;
00673 else
00674 res.push_back( "no error log source connected." ) ;
00675
00676 if ( FatalLogSource != 0 )
00677 res.push_back( FatalLogSource->toString() ) ;
00678 else
00679 res.push_back( "no fatal log source connected." ) ;
00680
00681 if ( LogrootLogSource != 0 )
00682 res.push_back( LogrootLogSource->toString() ) ;
00683 else
00684 res.push_back( "no root log source connected." ) ;
00685
00686 if ( Transport != 0 )
00687 res.push_back( Transport->toString() ) ;
00688 else
00689 res.push_back( "no log transport connected." ) ;
00690
00691 if ( Listener != 0 )
00692 res.push_back( Listener->toString() ) ;
00693 else
00694 res.push_back( "no log listener connected." ) ;
00695
00696
00697 return "LogSystem status: " + Ceylan::formatStringList( res ) ;
00698
00699 }
00700
00701
00702
00703 void LogPlug::info( const string & message, LevelOfDetail levelOfDetail )
00704 {
00705
00706 #if CEYLAN_DEBUG
00707
00708 if ( InfoLogSource == 0 )
00709 {
00710 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00711 throw LogException( LogSystemNotInitialized ) ;
00712 }
00713
00714 #endif // CEYLAN_DEBUG
00715
00716 InfoLogSource->send( message, levelOfDetail ) ;
00717
00718 }
00719
00720
00721
00722 void LogPlug::trace( const string & message, LevelOfDetail levelOfDetail )
00723 {
00724
00725 #if CEYLAN_DEBUG
00726
00727 if ( TraceLogSource == 0 )
00728 {
00729 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00730 throw LogException( LogSystemNotInitialized ) ;
00731 }
00732
00733 #endif // CEYLAN_DEBUG
00734
00735 TraceLogSource->send( message, levelOfDetail ) ;
00736
00737 }
00738
00739
00740
00741 void LogPlug::debug( const string & message, LevelOfDetail levelOfDetail )
00742 {
00743
00744 #if CEYLAN_DEBUG
00745
00746 if ( DebugLogSource == 0 )
00747 {
00748 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00749 throw LogException( LogSystemNotInitialized ) ;
00750 }
00751
00752 #endif // CEYLAN_DEBUG
00753
00754 DebugLogSource->send( message, levelOfDetail ) ;
00755
00756 }
00757
00758
00759
00760 void LogPlug::warning( const string & message, LevelOfDetail levelOfDetail )
00761 {
00762
00763 #if CEYLAN_DEBUG
00764
00765 if ( WarningLogSource == 0 )
00766 {
00767 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00768 throw LogException( LogSystemNotInitialized ) ;
00769 }
00770
00771 #endif // CEYLAN_DEBUG
00772
00773 WarningLogSource->send( message, levelOfDetail ) ;
00774
00775 }
00776
00777
00778
00779 void LogPlug::error( const string & message, LevelOfDetail levelOfDetail )
00780 {
00781
00782 #if CEYLAN_DEBUG
00783
00784 if ( ErrorLogSource == 0 )
00785 {
00786 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00787 throw LogException( LogSystemNotInitialized ) ;
00788 }
00789
00790 #endif // CEYLAN_DEBUG
00791
00792 ErrorLogSource->send( message, levelOfDetail ) ;
00793
00794
00795
00796
00797
00798
00799 if ( message != "Stopping channel." )
00800 std::cerr << std::endl << "[error] " << message << std::endl
00801 << std::endl ;
00802
00803 }
00804
00805
00806
00807 void LogPlug::fatal( const string & message, LevelOfDetail levelOfDetail )
00808 {
00809
00810 #if CEYLAN_DEBUG
00811
00812 if ( FatalLogSource == 0 )
00813 {
00814 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00815 throw LogException( LogSystemNotInitialized ) ;
00816 }
00817
00818 #endif // CEYLAN_DEBUG
00819
00820 FatalLogSource->send( message, levelOfDetail ) ;
00821
00822
00823
00824
00825
00826
00827 if ( message != "Stopping channel." )
00828 std::cerr << std::endl << "[fatal] " << message << std::endl
00829 << std::endl ;
00830
00831 }
00832
00833
00834
00835 void LogPlug::logroot( const string & message, LevelOfDetail levelOfDetail )
00836 {
00837
00838 #if CEYLAN_DEBUG
00839
00840 if ( LogrootLogSource == 0 )
00841 {
00842 std::cerr << LogSystemNotInitialized << std::endl << std::flush ;
00843 throw LogException( LogSystemNotInitialized ) ;
00844 }
00845
00846 #endif // CEYLAN_DEBUG
00847
00848 LogrootLogSource->send( message, levelOfDetail ) ;
00849
00850 }
00851
00852
00853
00854
00855 LogPlug::LogPlug()
00856 {
00857
00858 throw LogException(
00859 "Ceylan::Log::LogPlug should not be instanciated directly." ) ;
00860
00861 }
00862
00863
00864
00865 LogPlug::~LogPlug() throw()
00866 {
00867
00868 }
00869