akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #include "nlsr_logger.hpp" |
| 2 | |
| 3 | namespace nlsr |
| 4 | { |
| 5 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [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 | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 17 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 18 | string |
| 19 | NlsrLogger::getUserHomeDirectory() |
| 20 | { |
| 21 | string homeDirPath(getpwuid(getuid())->pw_dir); |
| 22 | if( homeDirPath.empty() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 23 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 24 | homeDirPath = getenv("HOME"); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 25 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 26 | return homeDirPath; |
| 27 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 28 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 29 | void |
| 30 | NlsrLogger::initNlsrLogger(std::string dirPath) |
| 31 | { |
| 32 | string logDirPath(dirPath); |
| 33 | if( dirPath.empty() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 34 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 35 | logDirPath=getUserHomeDirectory()+"/nlsrLog"; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 36 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 37 | cout<<"Log Dir Path: "<< logDirPath<<endl; |
| 38 | typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink; |
| 39 | shared_ptr< file_sink > sink(new file_sink( |
| 40 | keywords::file_name = logDirPath |
| 41 | +"/NLSR%Y%m%d%H%M%S_%3N.log", |
| 42 | keywords::rotation_size = 16 * 1024 * 1024, |
| 43 | keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0), |
| 44 | keywords::auto_flush = true |
| 45 | )); |
| 46 | sink->locked_backend()->set_file_collector(sinks::file::make_collector( |
| 47 | keywords::target = logDirPath, |
| 48 | //keywords::max_size = 512 * 1024 * 1024, |
| 49 | keywords::min_free_space = 64 * 1024 * 1024 |
| 50 | )); |
| 51 | sink->set_formatter( |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 52 | expr::format("%1% %2%") |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 53 | % getEpochTime() |
| 54 | % expr::smessage |
| 55 | ); |
| 56 | logging::core::get()->add_sink(sink); |
| 57 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 58 | |
| 59 | }//namespace nlsr |