Skip to content

Commit 11a122f

Browse files
committed
add and export util::FormatTime
1 parent a55a641 commit 11a122f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

include/plog/Formatters/TxtFormatter.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ namespace plog
1515

1616
static util::nstring format(const Record& record)
1717
{
18-
tm t;
19-
util::localtime_s(&t, &record.getTime().time);
20-
2118
util::nostringstream ss;
22-
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(" ");
23-
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(" ");
19+
ss << util::formatTime(&record.getTime());
2420
ss << std::setfill(PLOG_NSTR(' ')) << std::setw(5) << std::left << severityToString(record.getSeverity()) << PLOG_NSTR(" ");
2521
ss << PLOG_NSTR("[") << record.getTid() << PLOG_NSTR("] ");
2622
ss << PLOG_NSTR("[") << record.getFunc() << PLOG_NSTR("@") << record.getLine() << PLOG_NSTR("] ");

include/plog/Util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cassert>
33
#include <cstring>
44
#include <cstdio>
5+
#include <iomanip>
56
#include <sstream>
67
#include <fcntl.h>
78
#include <sys/stat.h>
@@ -90,6 +91,17 @@ namespace plog
9091
}
9192
#endif
9293

94+
inline util::nstring formatTime(const Time* time)
95+
{
96+
struct tm t;
97+
util::localtime_s(&t, &(time->time));
98+
99+
nostringstream ss;
100+
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(" ");
101+
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(" ");
102+
return ss.str();
103+
}
104+
93105
inline unsigned int gettid()
94106
{
95107
#ifdef _WIN32

0 commit comments

Comments
 (0)