Skip to content

Commit

Permalink
add and export util::FormatTime
Browse files Browse the repository at this point in the history
  • Loading branch information
MOON-CLJ committed Jan 22, 2018
1 parent a55a641 commit 94f0e45
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 1 addition & 5 deletions include/plog/Formatters/TxtFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ namespace plog

static util::nstring format(const Record& record)
{
tm t;
util::localtime_s(&t, &record.getTime().time);

util::nostringstream ss;
ss << t.tm_year + 1900 << "-" << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mon + 1 << PLOG_NSTR("-") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mday << PLOG_NSTR(" ");
ss << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_hour << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_min << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_sec << PLOG_NSTR(".") << std::setfill(PLOG_NSTR('0')) << std::setw(3) << record.getTime().millitm << PLOG_NSTR(" ");
ss << util::formatTime(&record.getTime());
ss << std::setfill(PLOG_NSTR(' ')) << std::setw(5) << std::left << severityToString(record.getSeverity()) << PLOG_NSTR(" ");
ss << PLOG_NSTR("[") << record.getTid() << PLOG_NSTR("] ");
ss << PLOG_NSTR("[") << record.getFunc() << PLOG_NSTR("@") << record.getLine() << PLOG_NSTR("] ");
Expand Down
12 changes: 12 additions & 0 deletions include/plog/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cassert>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <fcntl.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -90,6 +91,17 @@ namespace plog
}
#endif

inline util::nstring formatTime(const Time* time)
{
struct tm t;
localtime_s(&t, &(time->time));

nostringstream ss;
ss << t.tm_year + 1900 << "-" << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mon + 1 << PLOG_NSTR("-") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mday << PLOG_NSTR(" ");
ss << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_hour << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_min << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_sec << PLOG_NSTR(".") << std::setfill(PLOG_NSTR('0')) << std::setw(3) << time->millitm << PLOG_NSTR(" ");
return ss.str();
}

inline unsigned int gettid()
{
#ifdef _WIN32
Expand Down

0 comments on commit 94f0e45

Please sign in to comment.