akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 1 | #include "nlsr_logger.hpp" |
| 2 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 3 | namespace nlsr |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 4 | { |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 5 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 6 | string |
| 7 | NlsrLogger::getEpochTime() |
| 8 | { |
| 9 | std::stringstream ss; |
| 10 | boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970,1,1)); |
| 11 | boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); |
| 12 | boost::posix_time::time_duration diff = now - time_t_epoch; |
| 13 | ss<<diff.total_seconds()<<"."<<boost::format("%06i")%(diff.total_microseconds() |
| 14 | %1000000); |
| 15 | return ss.str(); |
| 16 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 17 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 18 | string |
| 19 | NlsrLogger::getUserHomeDirectory() |
| 20 | { |
| 21 | string homeDirPath(getpwuid(getuid())->pw_dir); |
| 22 | if( homeDirPath.empty() ) |
| 23 | { |
| 24 | homeDirPath = getenv("HOME"); |
| 25 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 26 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 27 | return homeDirPath; |
| 28 | } |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame] | 29 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 30 | void |
| 31 | NlsrLogger::initNlsrLogger(std::string dirPath) |
| 32 | { |
| 33 | string logDirPath(dirPath); |
| 34 | if( dirPath.empty() ) |
| 35 | { |
| 36 | logDirPath=getUserHomeDirectory()+"/nlsrLog"; |
| 37 | } |
| 38 | cout<<"Log Dir Path: "<< logDirPath<<endl; |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 39 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 40 | typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink; |
| 41 | shared_ptr< file_sink > sink(new file_sink( |
| 42 | keywords::file_name = logDirPath |
| 43 | +"/NLSR%Y%m%d%H%M%S_%3N.log", |
| 44 | keywords::rotation_size = 128 * 1024 * 1024, |
| 45 | keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0), |
| 46 | keywords::auto_flush = true |
| 47 | )); |
| 48 | sink->locked_backend()->set_file_collector(sinks::file::make_collector( |
| 49 | keywords::target = logDirPath, |
| 50 | keywords::max_size = 16 * 1024 * 1024 * 1024, |
| 51 | keywords::min_free_space = 128 * 1024 * 1024 |
| 52 | )); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 53 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 54 | sink->set_formatter( |
| 55 | expr::format("%1%: %2%") |
| 56 | % getEpochTime() |
| 57 | % expr::smessage |
| 58 | ); |
| 59 | |
| 60 | logging::core::get()->add_sink(sink); |
| 61 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 62 | |
| 63 | }//namespace nlsr |