log4cplus  2.0.0
appender.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    appender.h
00004 // Created: 6/2001
00005 // Author:  Tad E. Smith
00006 //
00007 //
00008 // Copyright 2001-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_APPENDER_HEADER_
00025 #define LOG4CPLUS_APPENDER_HEADER_
00026 
00027 #include <log4cplus/config.hxx>
00028 
00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
00030 #pragma once
00031 #endif
00032 
00033 #include <log4cplus/layout.h>
00034 #include <log4cplus/loglevel.h>
00035 #include <log4cplus/tstring.h>
00036 #include <log4cplus/helpers/pointer.h>
00037 #include <log4cplus/spi/filter.h>
00038 #include <log4cplus/helpers/lockfile.h>
00039 
00040 #include <memory>
00041 #include <mutex>
00042 #include <atomic>
00043 #include <condition_variable>
00044 
00045 
00046 namespace log4cplus {
00047 
00048 
00049     namespace helpers
00050     {
00051 
00052         class Properties;
00053 
00054     }
00055 
00056 
00061     class LOG4CPLUS_EXPORT ErrorHandler
00062     {
00063     public:
00064         ErrorHandler ();
00065         virtual ~ErrorHandler() = 0;
00066         virtual void error(const log4cplus::tstring& err) = 0;
00067         virtual void reset() = 0;
00068     };
00069 
00070 
00071     class LOG4CPLUS_EXPORT OnlyOnceErrorHandler
00072         : public ErrorHandler
00073     {
00074     public:
00075       // Ctor
00076         OnlyOnceErrorHandler();
00077         virtual ~OnlyOnceErrorHandler ();
00078         virtual void error(const log4cplus::tstring& err);
00079         virtual void reset();
00080 
00081     private:
00082         bool firstTime;
00083     };
00084 
00085 
00132     class LOG4CPLUS_EXPORT Appender
00133         : public virtual log4cplus::helpers::SharedObject
00134     {
00135     public:
00136       // Ctor
00137         Appender();
00138         Appender(const log4cplus::helpers::Properties & properties);
00139 
00140       // Dtor
00141         virtual ~Appender();
00142 
00151         void destructorImpl();
00152 
00153       // Methods
00160         virtual void close() = 0;
00161 
00165         bool isClosed() const;
00166 
00172         void syncDoAppend(const log4cplus::spi::InternalLoggingEvent& event);
00173 
00179         void asyncDoAppend(const log4cplus::spi::InternalLoggingEvent& event);
00180 
00186         void doAppend(const log4cplus::spi::InternalLoggingEvent& event);
00187 
00192         virtual log4cplus::tstring getName();
00193 
00198         virtual void setName(const log4cplus::tstring& name);
00199 
00203         virtual void setErrorHandler(std::unique_ptr<ErrorHandler> eh);
00204 
00209         virtual ErrorHandler* getErrorHandler();
00210 
00216         virtual void setLayout(std::unique_ptr<Layout> layout);
00217 
00223         virtual Layout* getLayout();
00224 
00228         void setFilter(log4cplus::spi::FilterPtr f);
00229 
00233         log4cplus::spi::FilterPtr getFilter() const;
00234 
00238         void addFilter (log4cplus::spi::FilterPtr f);
00239 
00243         void addFilter (std::function<
00244             spi::FilterResult (const log4cplus::spi::InternalLoggingEvent &)>);
00245 
00250         LogLevel getThreshold() const { return threshold; }
00251 
00260         void setThreshold(LogLevel th) { threshold = th; }
00261 
00267         bool isAsSevereAsThreshold(LogLevel ll) const {
00268             return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold));
00269         }
00270 
00275         void waitToFinishAsyncLogging();
00276 
00277     protected:
00278       // Methods
00284         virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0;
00285 
00286         tstring & formatEvent (const log4cplus::spi::InternalLoggingEvent& event) const;
00287 
00288       // Data
00291         std::unique_ptr<Layout> layout;
00292 
00294         log4cplus::tstring name;
00295 
00297         LogLevel threshold;
00298 
00301         log4cplus::spi::FilterPtr filter;
00302 
00304         std::unique_ptr<ErrorHandler> errorHandler;
00305 
00307         std::unique_ptr<helpers::LockFile> lockFile;
00308 
00311         bool useLockFile;
00312 
00314         bool async;
00315 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
00316         std::atomic<std::size_t> in_flight;
00317         std::mutex in_flight_mutex;
00318         std::condition_variable in_flight_condition;
00319 #endif
00320 
00322         bool closed;
00323 
00324     private:
00325 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
00326         void subtract_in_flight();
00327 #endif
00328     };
00329 
00331     typedef helpers::SharedObjectPtr<Appender> SharedAppenderPtr;
00332 
00333 } // end namespace log4cplus
00334 
00335 #endif // LOG4CPLUS_APPENDER_HEADER_