![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 #include "easy_kvp_logger.h" 00002 #include "soa/types/date.h" 00003 00004 namespace Datacratic{ 00005 00006 using namespace std; 00007 using namespace boost::program_options; 00008 00009 EasyKvpLogger::EasyKvpLogger(const boost::property_tree::ptree& pt, 00010 const string& coll, const string& envVar, const string& runId, 00011 const bool& logStartEnd, const strmap& defaults) : coll(coll), 00012 runId(runId), logStartEnd(logStartEnd), start(Date::now()) 00013 { 00014 setEnvVar(envVar, runId); 00015 defineLogger(pt, defaults, logStartEnd); 00016 } 00017 00018 EasyKvpLogger::EasyKvpLogger(const boost::property_tree::ptree& pt, 00019 const string& coll, const string& envVar, const bool& logStartEnd, 00020 const strmap& defaults) : coll(coll), logStartEnd(logStartEnd), 00021 start(Date::now()) 00022 { 00023 setDateAsRunIdIfNotInEnvVar(envVar); 00024 defineLogger(pt, defaults, logStartEnd); 00025 } 00026 00027 EasyKvpLogger::EasyKvpLogger(const variables_map& vm, 00028 const string& coll, const string& envVar, const bool& logStartEnd, 00029 const strmap& defaults) : coll(coll), logStartEnd(logStartEnd), 00030 start(Date::now()) 00031 { 00032 setDateAsRunIdIfNotInEnvVar(envVar); 00033 defineLogger(vm, defaults, logStartEnd); 00034 } 00035 00036 void EasyKvpLogger::setDateAsRunIdIfNotInEnvVar(const string & envVar) 00037 { 00038 char* runIdTmp; 00039 if((runIdTmp = getenv(envVar.c_str()))){ 00040 runId = string(runIdTmp); 00041 cout << "EasyKvpLogger reading " << envVar << " as " << runId << endl; 00042 }else{ 00043 Date d = Date::now(); 00044 runId = d.printClassic(); 00045 setEnvVar(envVar, runId); 00046 } 00047 } 00048 00049 00050 EasyKvpLogger::~EasyKvpLogger(){ 00051 if(logStartEnd){ 00052 Date end = Date::now(); 00053 clog({ 00054 {"key", "end"}, 00055 {"val", end.printClassic()}, 00056 {"duration", 00057 to_string(end.secondsSinceEpoch() - start.secondsSinceEpoch())} 00058 }); 00059 } 00060 } 00061 00062 void EasyKvpLogger::logStart(){ 00063 log("start", start.printClassic()); 00064 } 00065 00066 00067 void EasyKvpLogger::defineLogger(const boost::property_tree::ptree& pt, 00068 const strmap& defaults, const bool& logStartEnd) 00069 { 00070 IKvpLogger::KvpLoggerParams params; 00071 params.hostAndPort = pt.get<string>("logger.hostAndPort"); 00072 params.db = pt.get<string>("logger.db"); 00073 params.user = pt.get<string>("logger.user"); 00074 params.pwd = pt.get<string>("logger.pwd"); 00075 params.failSafe = pt.get<bool>("logger.failSafe"); 00076 string type = pt.get<string>("logger.type"); 00077 00078 /*cerr << params.hostAndPort << endl; 00079 cerr << params.db << endl; 00080 cerr << params.user << endl; 00081 cerr << params.pwd << endl; 00082 cerr << type << endl;*/ 00083 00084 logger = std::shared_ptr<IKvpLogger>( 00085 IKvpLogger::getKvpLogger(type, params)); 00086 setDefaults(defaults); 00087 if (logStartEnd) 00088 logStart(); 00089 } 00090 00091 void EasyKvpLogger::defineLogger(const variables_map& vm, 00092 const strmap& defaults, const bool& logStartEnd) 00093 { 00094 IKvpLogger::KvpLoggerParams params; 00095 params.hostAndPort = vm["logger.hostAndPort"].as<string>(); 00096 params.db = vm["logger.db"].as<string>(); 00097 params.user = vm["logger.user"].as<string>(); 00098 params.pwd = vm["logger.pwd"].as<string>(); 00099 string type = vm["logger.type"].as<string>(); 00100 params.failSafe = vm["logger.failSafe"].as<bool>(); 00101 00102 logger = std::shared_ptr<IKvpLogger>( 00103 IKvpLogger::getKvpLogger(type, params)); 00104 setDefaults(defaults); 00105 if (logStartEnd) 00106 logStart(); 00107 } 00108 00109 00110 void EasyKvpLogger 00111 ::setEnvVar(const std::string& envVar, const std::string& runId){ 00112 if(getenv(envVar.c_str()) && runId.length() > 0){ 00113 throw ML::Exception(envVar + " is already defined."); 00114 } 00115 cout << "EasyKvpLogger defining " << envVar << " as " << runId << endl; 00116 setenv(envVar.c_str(), runId.c_str(), 0); 00117 } 00118 00119 void EasyKvpLogger::log(strmap& kvpMap){ 00120 addDefaultsToMap(kvpMap); 00121 logger->log(kvpMap, coll); 00122 } 00123 00124 void EasyKvpLogger::clog(const strmap& kvpMap){ 00125 strmap values = kvpMap; 00126 log(values); 00127 } 00128 00129 void EasyKvpLogger::log(const string& key, const string& value){ 00130 strmap kvpMap; 00131 kvpMap["key"] = key; 00132 kvpMap["val"] = value; 00133 addDefaultsToMap(kvpMap); 00134 logger->log(kvpMap, coll); 00135 } 00136 00137 string EasyKvpLogger::getRunId(){ 00138 return runId; 00139 } 00140 00141 void EasyKvpLogger::addDefaultsToMap(strmap& kvpMap){ 00142 kvpMap["runId"] = runId; 00143 for(strmap_citerator it = defaults.begin(); it != defaults.end(); ++ it){ 00144 kvpMap[it->first] = it->second; 00145 } 00146 } 00147 00148 void EasyKvpLogger::setDefaults(const strmap& defaults){ 00149 this->defaults = defaults; 00150 } 00151 00152 00153 options_description 00154 EasyKvpLogger::get_options() 00155 { 00156 options_description options; 00157 options.add_options() 00158 ("logger.hostAndPort", value<string>()->required()) 00159 ("logger.db", value<string>()->required()) 00160 ("logger.user", value<string>()->required()) 00161 ("logger.pwd", value<string>()->required()) 00162 ("logger.type", value<string>()->required()) 00163 ("logger.failSafe", value<bool>()->required()); 00164 return options; 00165 } 00166 00167 }//namespace Datacratic