blob: 82b6e753bdd2ddda7d04458f97a7117313851ac5 [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;
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