akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 1 | #include "nlsr_logger.hpp" |
| 2 | |
| 3 | |
| 4 | string |
| 5 | NlsrLogger::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 | |
| 15 | string |
| 16 | NlsrLogger::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 | |
| 27 | void |
| 28 | NlsrLogger::initNlsrLogger(std::string dirPath) |
| 29 | { |
| 30 | string logDirPath(dirPath); |
| 31 | if( dirPath.empty() ) |
| 32 | { |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 33 | logDirPath=getUserHomeDirectory()+"/nlsrLog"; |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 34 | } |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 35 | cout<<"Log Dir Path: "<< logDirPath<<endl; |
| 36 | |
| 37 | //Create a text file sink |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 38 | typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink; |
| 39 | shared_ptr< file_sink > sink(new file_sink( |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 40 | 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 |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 44 | )); |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 45 | //Set up where the rotated files will be stored |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 46 | sink->locked_backend()->set_file_collector(sinks::file::make_collector( |
| 47 | keywords::target = logDirPath, // where to store rotated files |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 48 | keywords::max_size = 16 * 1024 * 1024 * 1024, // maximum total size of the stored files, in bytes |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 49 | keywords::min_free_space = 128 * 1024 * 1024 // minimum free space on the drive, in bytes |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 50 | )); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 51 | |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 52 | sink->set_formatter( |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 53 | expr::format("%1%: %2%") |
| 54 | % getEpochTime() |
| 55 | % expr::smessage |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 56 | ); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 57 | |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame^] | 58 | // Add it to the core |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 59 | logging::core::get()->add_sink(sink); |
| 60 | } |