RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/logger/multi_output.h
00001 /* multi_output.h                                                  -*- C++ -*-
00002    Jeremy Barnes, 21 September 2012
00003    Copyright (c) 2012 Datacratic Inc.  All rights reserved.
00004 
00005    Multiple output sources based upon the event.
00006 */
00007 
00008 #ifndef __logger__multi_output_h__
00009 #define __logger__multi_output_h__
00010 
00011 #include "logger.h"
00012 #include "rotating_output.h"
00013 #include <unordered_map>
00014 #include <mutex>
00015 #include <thread>
00016 
00017 namespace Datacratic {
00018 
00019 
00020 /*****************************************************************************/
00021 /* MULTI OUTPUT                                                              */
00022 /*****************************************************************************/
00023 
00026 struct MultiOutput : public LogOutput {
00028     typedef std::function<std::shared_ptr<LogOutput> (std::string)> CreateLogger;
00029 
00030     MultiOutput();
00031 
00032     virtual ~MultiOutput();
00033 
00034     virtual void logMessage(const std::string & channel,
00035                             const std::string & message);
00036 
00037     virtual void close();
00038 
00051     void logTo(const std::string & channel,
00052                const std::string & pattern,
00053                const CreateLogger & createLogger);
00054 
00055     virtual Json::Value stats() const;
00056 
00057     virtual void clearStats();
00058 
00059 
00060 private:
00061 
00062     struct ChannelEntry {
00063         
00064         std::string tmplate;
00065 
00066         enum TokenType {
00067             TOK_LITERAL,
00068             TOK_CHANNEL,
00069             TOK_FIELD
00070         };
00071 
00072         struct Token {
00073             Token()
00074                 : type(TOK_LITERAL), field(-1)
00075             {
00076             }
00077 
00078             TokenType type;
00079             std::string literal;
00080             int field;
00081         };
00082 
00083         std::vector<Token> tokens;
00084 
00085         CreateLogger createLogger;
00086 
00087         void parse(const std::string & tmplate);
00088 
00089         std::string apply(const std::string & channel,
00090                           const std::string & message) const;
00091     };
00092 
00093     mutable std::mutex lock;
00094     
00096     std::unordered_map<std::string, ChannelEntry> channels;
00097 
00099     std::unordered_map<std::string, std::shared_ptr<LogOutput> > outputs;
00100 };
00101 
00102 
00103 } // namespace Datacratic
00104 
00105 
00106 #endif /* __logger__multi_output_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator