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 | { |
| 33 | logDirPath=getUserHomeDirectory(); |
| 34 | } |
| 35 | |
| 36 | typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink; |
| 37 | shared_ptr< file_sink > sink(new file_sink( |
| 38 | keywords::file_name = "NLSR%Y%m%d%H%M%S_%3N.log", // file name pattern |
| 39 | keywords::rotation_size = 128 * 1024 * 1024 ,// rotation size, in characters |
| 40 | keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0) |
| 41 | )); |
| 42 | |
| 43 | sink->locked_backend()->set_file_collector(sinks::file::make_collector( |
| 44 | keywords::target = logDirPath, // where to store rotated files |
| 45 | keywords::max_size = 64 * 1024 * 1024 * 1024, // maximum total size of the stored files, in bytes |
| 46 | keywords::min_free_space = 128 * 1024 * 1024 // minimum free space on the drive, in bytes |
| 47 | )); |
| 48 | |
| 49 | sink->set_formatter |
| 50 | ( |
| 51 | expr::format("%1%: %2%") |
| 52 | % getEpochTime() |
| 53 | % expr::smessage |
| 54 | ); |
| 55 | |
| 56 | // Add it to the core |
| 57 | logging::core::get()->add_sink(sink); |
| 58 | } |