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