Logging.cpp

Go to the documentation of this file.
00001 // Copyright GSI 2012
00002 
00003 #include <fesa-core-gsi/Diagnostic/Logging.h>
00004 #include <fesa-core-gsi/Diagnostic/SyslogAppenderBuilder.h>
00005 #include <fesa-core-gsi/Diagnostic/SyslogAppender.h>
00006 #include <fesa-core-gsi/Diagnostic/SyslogAppenderFactory.h>
00007 
00008 #include <fesa-core/Exception/FesaException.h>
00009 
00010 #include <cmw-log/Logger.h>
00011 #include <cmw-log/LogManager.h>
00012 #include <cmw-log/AppenderManager.h>
00013 #include <cmw-log/StdoutAppender.h>
00014 #include <cmw-log/StdoutAppenderBuilder.h>
00015 #include <cmw-log/LogException.h>
00016 
00017 #include <syslog.h>
00018 
00019 namespace fesaGSI
00020 {
00021 
00022 Logging::Logging()
00023 {
00024 }
00025 
00026 
00027 Logging::~Logging()
00028 {
00029 }
00030     
00031 
00032 void
00033 Logging::initialize(const std::string& cfgFileName, fesa::LoggingMode::LoggingMode loggingMode) const
00034 {
00035         try
00036         {
00037                 CMW::Log::StdoutAppenderBuilder std_builder("StdoutAppender");
00038                 std_builder.setOutputStream(std::cout);
00039                 CMW::Log::AppenderManager::registerAppender(std_builder.build());
00040 
00041                 //for foreign appenders, we as well have to register a factory
00042                 SyslogAppenderBuilder syslog_builder("SyslogAppender");
00043                 CMW::Log::AppenderManager::registerAppender(syslog_builder.build());
00044                 SyslogAppenderFactory fac;
00045                 CMW::Log::AppenderManager::registerAppenderFactory("SyslogAppender",&fac);
00046 
00047                 const bool rc = CMW::Log::LogManager::init(cfgFileName);
00048                 CMW::Log::Logger& loggingLogger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core-cern.Diagnostic.Logging");
00049                 if (rc == true)
00050                 {
00051                         std::ostringstream strStream;
00052                         strStream << "Logging initialized from " << cfgFileName;
00053                         LOG_INFO_IF(loggingLogger, strStream.str());
00054                 }
00055                 else
00056                 {
00057                         std::ostringstream strStream;
00058                         strStream << "Can't initialize logging from " << cfgFileName << ". It's already been initalized";
00059                         LOG_WARNING_IF(loggingLogger, strStream.str());
00060                 }
00061                 CMW::Log::Logger& fesaLogger = CMW::Log::LoggerFactory::getLogger("FESA");
00062                 switch (loggingMode)
00063                 {
00064                         case fesa::LoggingMode::normal:
00065                                 // Nothing to do, logging will be done at the default level, i.e INFO
00066                                 break;
00067                         case fesa::LoggingMode::verbose:
00068                                 fesaLogger.setLevel(CMW::Log::Level::LL_DEBUG);
00069                                 break;
00070                         case fesa::LoggingMode::veryVerbose:
00071                                 fesaLogger.setLevel(CMW::Log::Level::LL_TRACE);
00072                                 break;
00073                         default:
00074                                 std::ostringstream strStream;
00075                                 strStream << "Unknown logging mode: " << loggingMode;
00076                                 throw fesa::FesaException(__FILE__, __LINE__, strStream.str());
00077                 }
00078 
00079         }
00080         catch(CMW::Log::LogException& ex)
00081         {
00082                 std::cerr << "Exception caught while initializing the logging" << std::endl;
00083                 std::cerr <<  ex.what() << std::endl;
00084                 throw;
00085         }
00086 
00087 }
00088 
00089 } // fesaGSI

Generated on 25 Jan 2013 for fesa-core-gsi by  doxygen 1.6.1