RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
examples/data_logger_ex.cc
00001 /* data_logger_ex.cc
00002    RĂ©mi Attab and Jeremy Barnes, March 2011
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    A simple data logger which dumps the logging events from the various
00006    components of the RTBKit stack into files.
00007 */
00008 
00009 
00010 #include "rtbkit/plugins/data_logger/data_logger.h"
00011 #include "soa/logger/file_output.h"
00012 #include "soa/logger/multi_output.h"
00013 #include "soa/service/service_utils.h"
00014 
00015 #include <boost/program_options/cmdline.hpp>
00016 #include <boost/program_options/options_description.hpp>
00017 #include <boost/program_options/positional_options.hpp>
00018 #include <boost/program_options/parsers.hpp>
00019 #include <boost/program_options/variables_map.hpp>
00020 #include <boost/regex.hpp>
00021 #include <vector>
00022 #include <string>
00023 #include <thread>
00024 #include <chrono>
00025 
00026 
00027 using namespace std;
00028 using namespace Datacratic;
00029 using namespace RTBKIT;
00030 
00031 
00032 /******************************************************************************/
00033 /* LOGGER SETUP                                                               */
00034 /******************************************************************************/
00035 
00044 void
00045 setupOutputs(
00046         DataLogger& logger,
00047         const string& logDir,
00048         const string& rotationInterval)
00049 {
00050 
00051     // Log the various error messages generated by our stack to the a log file.
00052     auto errorOutput = make_shared<RotatingFileOutput>();
00053     errorOutput->open(logDir + "/%F/errors-%F-%T.log", rotationInterval);
00054     logger.addOutput(errorOutput, boost::regex("ROUTERERROR|PAERROR"), boost::regex());
00055 
00056 
00057     // Output auction events (wins, impressions and clicks) into strategy
00058     // specific folders. MultiOutput allows the aggregation of multiple outputs
00059     // under a single outputs.
00060     auto strategyOutput = make_shared<MultiOutput>();
00061 
00062     auto createMatchedWinFile = [=] (const string & pattern)
00063         {
00064             auto result = make_shared<RotatingFileOutput>();
00065             result->open(pattern, rotationInterval);
00066             return result;
00067         };
00068 
00069     // The magic constants represent indexes in the received message.
00070     strategyOutput->logTo(
00071             "MATCHEDWIN",
00072             logDir + "/%F/$(17)/$(5)/$(0)-%T.log.gz",
00073             createMatchedWinFile);
00074     strategyOutput->logTo(
00075             "",
00076             logDir + "/%F/$(10)/$(11)/$(0)-%T.log.gz",
00077             createMatchedWinFile);
00078 
00079     logger.addOutput(
00080             strategyOutput,
00081             boost::regex("MATCHEDWIN|MATCHEDIMPRESSION|MATCHEDCLICK"));
00082 }
00083 
00084 
00085 /******************************************************************************/
00086 /* MAIN                                                                       */
00087 /******************************************************************************/
00088 
00089 int main (int argc, char** argv)
00090 {
00091     ServiceProxyArguments serviceArgs;
00092     string logDir = "data_logger";
00093     string rotationInterval = "1h";
00094 
00095     using namespace boost::program_options;
00096 
00097     options_description loggerOptions("Logger Options");
00098     loggerOptions.add_options()
00099         ("log-dir,d", value<string>(&logDir),
00100                 "Directory where the folders should be stored.")
00101         ("rotation-interval,r", value<string>(&rotationInterval),
00102                 "Interval between each log rotation.");
00103 
00104     options_description allOptions;
00105     allOptions.add(loggerOptions).add(serviceArgs.makeProgramOptions());
00106     allOptions.add_options() ("help,h", "Prints this message");
00107 
00108     variables_map vm;
00109     store(command_line_parser(argc, argv).options(allOptions).run(), vm);
00110     notify(vm);
00111 
00112     if (vm.count("help")) {
00113         cerr << allOptions << endl;
00114         exit(0);
00115     }
00116 
00117     auto serviceProxies = serviceArgs.makeServiceProxies();
00118 
00119     // Initialize the logger and it's outputs.
00120     DataLogger logger(serviceProxies);
00121     logger.init();
00122     setupOutputs(logger, logDir, rotationInterval);
00123 
00124     // Subscribe to the message stream coming from the adServer, the router and
00125     // the post auction loop.
00126     logger.connectAllServiceProviders("adServer", "logger");
00127     logger.connectAllServiceProviders("rtbRequestRouter", "logger");
00128     logger.connectAllServiceProviders("rtbPostAuctionService", "logger");
00129 
00130     // Start processing incoming events.
00131     logger.start();
00132 
00133     // Job done. Time to take a good long nap.
00134     while (true) this_thread::sleep_for(chrono::seconds(10));
00135 
00136     return 0;
00137 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator