blob: 0f003a0c9e0f480abc1bc0f9b26344fa5fce3903 [file] [log] [blame]
akmhoque85d88332014-02-17 21:11:21 -06001#include "nlsr_logger.hpp"
2
akmhoqueb1710aa2014-02-19 17:13:36 -06003namespace nlsr {
akmhoque85d88332014-02-17 21:11:21 -06004
5string
6NlsrLogger::getEpochTime()
7{
8 std::stringstream ss;
9 boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970,1,1));
10 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
11 boost::posix_time::time_duration diff = now - time_t_epoch;
12 ss<<diff.total_seconds()<<"."<<boost::format("%06i")%(diff.total_microseconds()%1000000);
13 return ss.str();
14}
15
16string
17NlsrLogger::getUserHomeDirectory()
18{
19 string homeDirPath(getpwuid(getuid())->pw_dir);
20 if( homeDirPath.empty() )
21 {
22 homeDirPath = getenv("HOME");
23 }
24
25 return homeDirPath;
26}
27
28void
29NlsrLogger::initNlsrLogger(std::string dirPath)
30{
31 string logDirPath(dirPath);
32 if( dirPath.empty() )
33 {
akmhoque92afde42014-02-18 14:04:07 -060034 logDirPath=getUserHomeDirectory()+"/nlsrLog";
akmhoque85d88332014-02-17 21:11:21 -060035 }
akmhoque92afde42014-02-18 14:04:07 -060036 cout<<"Log Dir Path: "<< logDirPath<<endl;
37
38 //Create a text file sink
akmhoque85d88332014-02-17 21:11:21 -060039 typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
40 shared_ptr< file_sink > sink(new file_sink(
akmhoque92afde42014-02-18 14:04:07 -060041 keywords::file_name = logDirPath+"/NLSR%Y%m%d%H%M%S_%3N.log", // file name pattern
42 keywords::rotation_size = 128 * 1024 * 1024 , // rotation size, in characters
43 keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0),
44 keywords::auto_flush = true
akmhoque85d88332014-02-17 21:11:21 -060045 ));
akmhoque92afde42014-02-18 14:04:07 -060046 //Set up where the rotated files will be stored
akmhoque85d88332014-02-17 21:11:21 -060047 sink->locked_backend()->set_file_collector(sinks::file::make_collector(
48 keywords::target = logDirPath, // where to store rotated files
akmhoque92afde42014-02-18 14:04:07 -060049 keywords::max_size = 16 * 1024 * 1024 * 1024, // maximum total size of the stored files, in bytes
akmhoque85d88332014-02-17 21:11:21 -060050 keywords::min_free_space = 128 * 1024 * 1024 // minimum free space on the drive, in bytes
akmhoque92afde42014-02-18 14:04:07 -060051 ));
akmhoque85d88332014-02-17 21:11:21 -060052
akmhoque92afde42014-02-18 14:04:07 -060053 sink->set_formatter(
akmhoque85d88332014-02-17 21:11:21 -060054 expr::format("%1%: %2%")
55 % getEpochTime()
56 % expr::smessage
akmhoque92afde42014-02-18 14:04:07 -060057 );
akmhoque85d88332014-02-17 21:11:21 -060058
akmhoque92afde42014-02-18 14:04:07 -060059 // Add it to the core
akmhoque85d88332014-02-17 21:11:21 -060060 logging::core::get()->add_sink(sink);
61}
akmhoqueb1710aa2014-02-19 17:13:36 -060062
63}//namespace nlsr