| #include "logger.hpp" |
| |
| namespace nlsr { |
| |
| string |
| Logger::getEpochTime() |
| { |
| std::stringstream ss; |
| boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970, 1, 1)); |
| boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); |
| boost::posix_time::time_duration diff = now - time_t_epoch; |
| ss << diff.total_seconds() << "." << boost::format("%06i") % |
| (diff.total_microseconds() |
| % 1000000); |
| return ss.str(); |
| } |
| |
| string |
| Logger::getUserHomeDirectory() |
| { |
| string homeDirPath(getpwuid(getuid())->pw_dir); |
| if (homeDirPath.empty()) |
| { |
| homeDirPath = getenv("HOME"); |
| } |
| return homeDirPath; |
| } |
| |
| void |
| Logger::initialize(std::string dirPath) |
| { |
| string logDirPath(dirPath); |
| if (dirPath.empty()) |
| { |
| logDirPath = getUserHomeDirectory() + "/nlsrLog"; |
| } |
| cout << "Log Dir Path: " << logDirPath << endl; |
| typedef sinks::synchronous_sink<sinks::text_file_backend> file_sink; |
| shared_ptr<file_sink> sink(new file_sink( |
| keywords::file_name = logDirPath |
| + "/NLSR%Y%m%d%H%M%S_%3N.log", |
| keywords::rotation_size = 16 * 1024 * 1024, |
| keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0), |
| keywords::auto_flush = true |
| )); |
| sink->locked_backend()->set_file_collector(sinks::file::make_collector( |
| keywords::target = logDirPath, |
| //keywords::max_size = 512 * 1024 * 1024, |
| keywords::min_free_space = 64 * 1024 * 1024 |
| )); |
| sink->set_formatter( |
| expr::format("%1% %2%") |
| % getEpochTime() |
| % expr::smessage |
| ); |
| logging::core::get()->add_sink(sink); |
| } |
| |
| }//namespace nlsr |