RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/logger/rotating_output.h
00001 /* rotating_output.h                                               -*- C++ -*-
00002    Jeremy Barnes, 19 September 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Output that writes to an output source and rotates it.
00006 */
00007 
00008 #ifndef __logger__rotating_output_h__
00009 #define __logger__rotating_output_h__
00010 
00011 #include "logger.h"
00012 #include "soa/types/date.h"
00013 #include <boost/thread/recursive_mutex.hpp>
00014 #include "soa/types/periodic_utils.h"
00015 #include "soa/gc/rcu_protected.h"
00016 
00017 namespace Datacratic {
00018 
00019 
00020 /*****************************************************************************/
00021 /* ROTATING OUTPUT                                                           */
00022 /*****************************************************************************/
00023 
00026 struct RotatingOutput : public LogOutput {
00027 
00028     RotatingOutput();
00029 
00030     virtual ~RotatingOutput();
00031     
00032     void open(const std::string & periodPattern);
00033 
00034     virtual void performRotation();
00035 
00036     virtual void close();
00037 
00038 protected:
00039     typedef boost::recursive_mutex Lock;
00040     mutable Lock lock;
00041     typedef boost::unique_lock<Lock> Guard;
00042 
00043     TimeGranularity granularity;
00044     int number;
00045 
00046     Date currentPeriodStart;  
00047     double interval;   
00048 
00053     virtual void openSubordinate(Date newDate) = 0;
00054     
00063     virtual void rotateSubordinate(Date newDate) = 0;
00064 
00066     virtual void closeSubordinate() = 0;
00067     
00068 private:
00070     boost::scoped_ptr<boost::thread> rotateThread;
00071 
00073     volatile int shutdown_;
00074 
00076     volatile int up_;
00077 
00079     void runRotateThread();
00080 };
00081 
00082 
00083 /*****************************************************************************/
00084 /* ROTATING OUTPUT ADAPTOR                                                   */
00085 /*****************************************************************************/
00086 
00089 struct RotatingOutputAdaptor : public RotatingOutput {
00090     typedef std::function<LogOutput * (std::string)>
00091         LoggerFactory;
00092 
00093     RotatingOutputAdaptor(LoggerFactory factory);
00094 
00095     virtual ~RotatingOutputAdaptor();
00096 
00097     
00099     void open(const std::string & filenamePattern,
00100               const std::string & periodPattern);
00101     
00102     virtual void logMessage(const std::string & channel,
00103                             const std::string & message);
00104     
00105     virtual Json::Value stats() const;
00106 
00107     virtual void clearStats();
00108 
00109     virtual void close();
00110 
00111     boost::function<void (std::string)> onPreFileOpen;
00112     boost::function<void (std::string)> onPostFileOpen;
00113     boost::function<void (std::string)> onPreFileClose;
00114     boost::function<void (std::string)> onPostFileClose;
00115     boost::function<void (std::string, std::size_t)> onFileWrite;
00116 
00118     boost::function<void (std::string, std::string)> onBeforeLogRotation;
00119     boost::function<void (std::string, std::string)> onAfterLogRotation;
00120 
00121 private:
00123     LoggerFactory loggerFactory;
00124 
00125     std::string filenamePattern;
00126 
00127     GcLock gcLock;
00128 
00130     RcuProtected<LogOutput> logger;
00131 
00132     std::string currentFilename;
00133 
00134     virtual void openSubordinate(Date newDate);
00135 
00136     virtual void rotateSubordinate(Date newDate);
00137 
00138     virtual void closeSubordinate();
00139 };
00140 
00141 
00142 } // namespace Datacratic
00143 
00144 #endif /* __logger__rotating_output_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator