blob: 0f003a0c9e0f480abc1bc0f9b26344fa5fce3903 [file] [log] [blame]
#include "nlsr_logger.hpp"
namespace nlsr {
string
NlsrLogger::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
NlsrLogger::getUserHomeDirectory()
{
string homeDirPath(getpwuid(getuid())->pw_dir);
if( homeDirPath.empty() )
{
homeDirPath = getenv("HOME");
}
return homeDirPath;
}
void
NlsrLogger::initNlsrLogger(std::string dirPath)
{
string logDirPath(dirPath);
if( dirPath.empty() )
{
logDirPath=getUserHomeDirectory()+"/nlsrLog";
}
cout<<"Log Dir Path: "<< logDirPath<<endl;
//Create a text file sink
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", // file name pattern
keywords::rotation_size = 128 * 1024 * 1024 , // rotation size, in characters
keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0),
keywords::auto_flush = true
));
//Set up where the rotated files will be stored
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = logDirPath, // where to store rotated files
keywords::max_size = 16 * 1024 * 1024 * 1024, // maximum total size of the stored files, in bytes
keywords::min_free_space = 128 * 1024 * 1024 // minimum free space on the drive, in bytes
));
sink->set_formatter(
expr::format("%1%: %2%")
% getEpochTime()
% expr::smessage
);
// Add it to the core
logging::core::get()->add_sink(sink);
}
}//namespace nlsr