Skip to content

Commit

Permalink
Improved Logger class.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenliang committed May 29, 2011
1 parent 16766bd commit 4a899ef
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions util/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,40 @@

#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>

namespace
{
const std::string kDefaultFile = "log.txt";

void PrintTime(std::ostream& os)
{
time_t rawtime;
time(&rawtime);
tm* timeinfo = localtime(&rawtime);
os << std::setfill('0');
os << "["
<< std::setw(4) << timeinfo->tm_year + 1900 << "-"
<< std::setw(2) << timeinfo->tm_mon + 1<< "-"
<< std::setw(2) << timeinfo->tm_mday << " "
<< std::setw(2) << timeinfo->tm_hour << ":"
<< std::setw(2) << timeinfo->tm_min << ":"
<< std::setw(2) << timeinfo->tm_sec << "] ";
os << std::setfill(' ');
}

void PrintLoglevel(std::ostream& os, int loglevel)
{
switch (loglevel)
{
case util::Logger::LL_INFO: os << "[INFO] "; break;
case util::Logger::LL_WARNING: os << "[WARNING] "; break;
case util::Logger::LL_ERROR: os << "[ERROR] "; break;
case util::Logger::LL_NONE: os << "[NONE] "; break;
default: os << "[UNKNOWN] "; break;
}
}
}

namespace util
Expand All @@ -28,6 +58,9 @@ namespace util
if (!outstream_.is_open())
outstream_.open(logfile_.c_str());

PrintTime(outstream_);
PrintLoglevel(outstream_, loglevel);

if ((logMode_ & LM_FILE) && outstream_.is_open() && outstream_.good())
outstream_ << text << std::endl;

Expand Down

0 comments on commit 4a899ef

Please sign in to comment.