SimpleConfigurator.cpp
Go to the documentation of this file.
00001 /*
00002  * SimpleConfigurator.cpp
00003  *
00004  * Copyright 2001, Glen Scott. All rights reserved.
00005  *
00006  * See the COPYING file for the terms of usage and distribution.
00007  */
00008 #include "PortabilityImpl.hh"
00009 
00010 #ifdef LOG4CPP_HAVE_UNISTD_H
00011 #include <unistd.h>
00012 #endif
00013 #ifdef LOG4CPP_HAVE_IO_H
00014 #    include <io.h>
00015 #endif
00016 
00017 #include <stdio.h>
00018 #include <iostream>
00019 #include <string>
00020 #include <fstream>
00021 #include <stdio.h>
00022 
00023 #include <log4cpp/Category.hh>
00024 #include <log4cpp/Appender.hh>
00025 #include <log4cpp/OstreamAppender.hh>
00026 #include <log4cpp/FileAppender.hh>
00027 #include <log4cpp/RollingFileAppender.hh>
00028 #include <log4cpp/Layout.hh>
00029 #include <log4cpp/BasicLayout.hh>
00030 #include <log4cpp/SimpleLayout.hh>
00031 #include <log4cpp/Priority.hh>
00032 #include <log4cpp/NDC.hh>
00033 #include <log4cpp/PatternLayout.hh>
00034 #include <log4cpp/SimpleConfigurator.hh>
00035 #ifdef LOG4CPP_HAVE_SYSLOG
00036 #include <log4cpp/SyslogAppender.hh>
00037 #endif
00038 #include <log4cpp/RemoteSyslogAppender.hh>
00039 #ifdef WIN32
00040 #include <log4cpp/NTEventLogAppender.hh>
00041 #endif
00042 
00043 namespace log4cpp {
00044 
00045     void SimpleConfigurator::configure(const std::string& initFileName) throw (ConfigureFailure) {
00046         std::ifstream initFile(initFileName.c_str());
00047         
00048         if (!initFile) {
00049             throw ConfigureFailure(std::string("Config File ") + initFileName + " does not exist or is unreadable");
00050         }
00051         
00052         configure(initFile);
00053     }
00054           
00055     void SimpleConfigurator::configure(std::istream& initFile) throw (ConfigureFailure) {
00056         std::string nextCommand;
00057         std::string categoryName;
00058         
00059         while (initFile >> nextCommand) {
00060             /* skip comment lines */
00061             if (nextCommand[0] == '#') {
00062                 std::string dummy;
00063                 std::getline(initFile, dummy);
00064                 continue;
00065             }
00066             /* stop on missing categoryName */
00067             if (!(initFile >> categoryName))
00068                 break;
00069 
00070             log4cpp::Category& category =
00071                 (categoryName.compare("root") == 0) ?
00072                 log4cpp::Category::getRoot() :
00073                 log4cpp::Category::getInstance(categoryName);
00074             
00075             if (nextCommand.compare("appender") == 0) {
00076                 std::string layout;
00077                 std::string appenderName;
00078                 
00079                 if (initFile >> layout >> appenderName) {
00080                     log4cpp::Appender* appender;
00081                     if (appenderName.compare("file") == 0) {
00082                         std::string logFileName;
00083                         if (!(initFile >> logFileName)) {
00084                             throw ConfigureFailure("Missing filename for log file logging configuration file for category: " + categoryName);
00085                         }
00086                         appender = new log4cpp::FileAppender(categoryName, logFileName);
00087                     }
00088                     else if (appenderName.compare("rolling") == 0) {
00089                         std::string logFileName;
00090                         size_t maxFileSize;
00091                         unsigned int maxBackupIndex=1;
00092                         if (!(initFile >> logFileName)) {
00093                             throw ConfigureFailure("Missing filename for log file logging configuration file for category: " + categoryName);
00094                         }
00095                                 if (!(initFile >> maxFileSize)) {
00096                             throw ConfigureFailure("Missing maximum size for log file logging configuration file for category: " + categoryName);
00097                         }
00098                         if (!(initFile >> maxBackupIndex)) {
00099                             throw ConfigureFailure("Missing maximum backup index for log file logging configuration file for category: " + categoryName);
00100                         }
00101                         appender = new log4cpp::RollingFileAppender(categoryName, logFileName, maxFileSize, maxBackupIndex);
00102                     }
00103                     else if (appenderName.compare("console") == 0) {
00104                         appender =
00105                             new log4cpp::OstreamAppender(categoryName, &std::cout);
00106                     }
00107                     else if (appenderName.compare("stdout") == 0) {
00108                         appender =
00109                             new log4cpp::FileAppender(categoryName, ::dup(fileno(stdout)));
00110                     }
00111                     else if (appenderName.compare("stderr") == 0) {
00112                         appender =
00113                             new log4cpp::FileAppender(categoryName, ::dup(fileno(stderr)));
00114                     }
00115 #ifdef LOG4CPP_HAVE_SYSLOG
00116                     else if (appenderName.compare("syslog") == 0) {
00117                         std::string syslogName;
00118                         int facility;
00119                         if (!(initFile >> syslogName)) {
00120                             throw ConfigureFailure("Missing syslogname for SysLogAppender for category: " + categoryName);
00121                         }
00122                         if (!(initFile >> facility)) {
00123                             facility = LOG_USER;
00124                         } else {
00125                             // * 8
00126                             facility *= 8;
00127                         }
00128                         appender =
00129                             new log4cpp::SyslogAppender(categoryName, syslogName, facility);
00130                     } 
00131 #endif
00132 #if defined(WIN32)
00133                     else if (appenderName.compare("nteventlog") == 0) {
00134                         std::string source;
00135                         if (!(initFile >> source)) {
00136                             throw ConfigureFailure("Missing source for NTEventLogAppender for category: " + categoryName);
00137                         }
00138                         appender =
00139                             new log4cpp::NTEventLogAppender(categoryName, source);
00140                     } 
00141 #endif
00142                     else if (appenderName.compare("remotesyslog") == 0) {
00143                         std::string syslogName;
00144                         std::string relayer;
00145                         int facility;
00146                         int portNumber;
00147                         if (!(initFile >> syslogName)) {
00148                             throw ConfigureFailure("Missing syslogname for SysLogAppender for category: " + categoryName);
00149                         }
00150                         if (!(initFile >> relayer)) {
00151                             throw ConfigureFailure("Missing syslog host for SysLogAppender for category: " + categoryName);
00152                         }
00153                         if (!(initFile >> facility)) {
00154                             facility = LOG_USER;
00155                         }
00156                         if (!(initFile >> portNumber)) {
00157                             portNumber = 514;
00158                         }
00159                         appender =
00160                             new log4cpp::RemoteSyslogAppender(categoryName, syslogName, relayer, facility, portNumber);
00161                     } 
00162                     else {
00163                         throw ConfigureFailure("Invalid appender name (" +
00164                                                appenderName +
00165                                                ") in logging configuration file for category: " +
00166                                                categoryName);
00167                     }
00168                     if (layout.compare("basic") == 0)
00169                         appender->setLayout(new log4cpp::BasicLayout());
00170                     else if (layout.compare("simple") == 0)
00171                         appender->setLayout(new log4cpp::SimpleLayout());
00172                     else if (layout.compare("pattern") == 0) {
00173                         log4cpp::PatternLayout *layout =
00174                             new log4cpp::PatternLayout();
00175                         initFile >> std::ws; // skip whitespace
00176                         char pattern[1000];
00177                         initFile.getline(pattern, 1000);
00178                         layout->setConversionPattern(std::string(pattern));
00179                         appender->setLayout(layout);
00180                     }
00181                     else {
00182                         throw ConfigureFailure("Invalid layout (" + layout +
00183                                                ") in logging configuration file for category: " +
00184                                                categoryName);
00185                     }
00186                     category.addAppender(appender);
00187                 }
00188             }
00189             else if (nextCommand.compare("priority") == 0) {
00190                 std::string priority;
00191                 if (!(initFile >> priority)) {
00192                     throw ConfigureFailure("Missing priority in logging configuration file for category: " + categoryName);
00193                 }
00194                 
00195                 try {
00196                     category.setPriority(log4cpp::Priority::getPriorityValue(priority));
00197                 } catch(std::invalid_argument) {
00198                     throw ConfigureFailure("Invalid priority ("+priority+") in logging configuration file for category: "+categoryName);
00199                 }
00200             }
00201             else if (nextCommand.compare("category") == 0) {
00202                 /*
00203                   This command means we should "refer" to the category
00204                   (in order to have it created). We've already done this
00205                   in common setup code for all commands.
00206                 */
00207             }
00208             else {
00209                 throw ConfigureFailure("Invalid format in logging configuration file. Command: " + nextCommand);
00210             }
00211         }
00212     }
00213 }
00214 
00215 
00216 


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung.
autogenerated on Wed Sep 16 2015 10:27:14