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