log4cplus  2.0.0
configurator.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    configurator.h
00004 // Created: 3/2003
00005 // Author:  Tad E. Smith
00006 //
00007 //
00008 // Copyright 2003-2015 Tad E. Smith
00009 //
00010 // Licensed under the Apache License, Version 2.0 (the "License");
00011 // you may not use this file except in compliance with the License.
00012 // You may obtain a copy of the License at
00013 //
00014 //     http://www.apache.org/licenses/LICENSE-2.0
00015 //
00016 // Unless required by applicable law or agreed to in writing, software
00017 // distributed under the License is distributed on an "AS IS" BASIS,
00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019 // See the License for the specific language governing permissions and
00020 // limitations under the License.
00021 
00024 #ifndef LOG4CPLUS_CONFIGURATOR_HEADER_
00025 #define LOG4CPLUS_CONFIGURATOR_HEADER_
00026 
00027 #include <log4cplus/config.hxx>
00028 
00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
00030 #pragma once
00031 #endif
00032 
00033 #include <log4cplus/appender.h>
00034 #include <log4cplus/logger.h>
00035 #include <log4cplus/helpers/pointer.h>
00036 #include <log4cplus/helpers/property.h>
00037 
00038 #include <map>
00039 
00040 
00041 namespace log4cplus
00042 {
00043     class Hierarchy;
00044 
00045 
00067     class LOG4CPLUS_EXPORT PropertyConfigurator
00068     {
00069     public:
00070         enum PCFlags
00071         {
00072             fRecursiveExpansion   = (1 << 0)
00073             , fShadowEnvironment  = (1 << 1)
00074             , fAllowEmptyVars     = (1 << 2)
00075 
00076             // These encoding related options occupy 2 bits of the flags
00077             // and are mutually exclusive. These flags are synchronized with
00078             // PFlags in Properties.
00079 
00080             , fEncodingShift      = 3
00081             , fEncodingMask       = 0x3
00082             , fUnspecEncoding     = (0 << fEncodingShift)
00083 #if defined (LOG4CPLUS_HAVE_CODECVT_UTF8_FACET) && defined (UNICODE)
00084             , fUTF8               = (1 << fEncodingShift)
00085 #endif
00086 #if (defined (LOG4CPLUS_HAVE_CODECVT_UTF16_FACET) || defined (_WIN32)) \
00087     && defined (UNICODE)
00088             , fUTF16              = (2 << fEncodingShift)
00089 #endif
00090 #if defined (LOG4CPLUS_HAVE_CODECVT_UTF32_FACET) && defined (UNICODE)
00091             , fUTF32              = (3 << fEncodingShift)
00092 #endif
00093         };
00094 
00095         // ctor and dtor
00096         PropertyConfigurator(const log4cplus::tstring& propertyFile,
00097             Hierarchy& h = Logger::getDefaultHierarchy(), unsigned flags = 0);
00098         PropertyConfigurator(const log4cplus::helpers::Properties& props,
00099             Hierarchy& h = Logger::getDefaultHierarchy(), unsigned flags = 0);
00100         PropertyConfigurator(log4cplus::tistream& propertyStream,
00101             Hierarchy& h = Logger::getDefaultHierarchy(), unsigned flags = 0);
00102         virtual ~PropertyConfigurator();
00103 
00113         static void doConfigure(const log4cplus::tstring& configFilename,
00114             Hierarchy& h = Logger::getDefaultHierarchy(), unsigned flags = 0);
00115 
00262         virtual void configure();
00263 
00270         log4cplus::helpers::Properties const & getProperties () const;
00271 
00279         log4cplus::tstring const & getPropertyFilename () const;
00280 
00281     protected:
00282       // Methods
00283         void init();  // called by the ctor
00284         void reconfigure();
00285         void replaceEnvironVariables();
00286         void configureLoggers();
00287         void configureLogger(log4cplus::Logger logger, const log4cplus::tstring& config);
00288         void configureAppenders();
00289         void configureAdditivity();
00290 
00291         virtual Logger getLogger(const log4cplus::tstring& name);
00292         virtual void addAppender(Logger &logger, log4cplus::SharedAppenderPtr& appender);
00293 
00294       // Types
00295         typedef std::map<log4cplus::tstring, log4cplus::SharedAppenderPtr> AppenderMap;
00296 
00297       // Data
00298         Hierarchy& h;
00299         log4cplus::tstring propertyFilename;
00300         log4cplus::helpers::Properties properties;
00301         AppenderMap appenders;
00302         unsigned flags;
00303 
00304     private:
00305       // Disable copy
00306         PropertyConfigurator(const PropertyConfigurator&);
00307         PropertyConfigurator& operator=(PropertyConfigurator&);
00308     };
00309 
00310 
00311 
00320     class LOG4CPLUS_EXPORT BasicConfigurator : public PropertyConfigurator {
00321     public:
00322       // ctor and dtor
00323         BasicConfigurator(Hierarchy& h = Logger::getDefaultHierarchy(),
00324             bool logToStdErr = false);
00325         virtual ~BasicConfigurator();
00326 
00336         static void doConfigure(Hierarchy& h = Logger::getDefaultHierarchy(),
00337             bool logToStdErr = false);
00338 
00340         static log4cplus::tstring const DISABLE_OVERRIDE_KEY;
00341 
00342     private:
00343       // Disable copy
00344         BasicConfigurator(const BasicConfigurator&);
00345         BasicConfigurator& operator=(BasicConfigurator&);
00346     };
00347 
00348 
00349 #if !defined(LOG4CPLUS_SINGLE_THREADED)
00350     // Forward Declarations
00351     class ConfigurationWatchDogThread;
00352 
00353 
00354     class LOG4CPLUS_EXPORT ConfigureAndWatchThread {
00355     public:
00356       // ctor and dtor
00357         ConfigureAndWatchThread(const log4cplus::tstring& propertyFile,
00358                                 unsigned int millis = 60 * 1000);
00359         virtual ~ConfigureAndWatchThread();
00360 
00361     private:
00362       // Disallow copying of instances of this class
00363        ConfigureAndWatchThread(const ConfigureAndWatchThread&);
00364        ConfigureAndWatchThread& operator=(const ConfigureAndWatchThread&);
00365 
00366       // Data
00367        ConfigurationWatchDogThread * watchDogThread;
00368     };
00369 #endif
00370 
00371 } // end namespace log4cplus
00372 
00373 #endif // LOG4CPLUS_CONFIGURATOR_HEADER_