Skip to content

Commit

Permalink
fix: localtime is unsafe, consider using localtime_s instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeppahBaws committed Jul 30, 2020
1 parent a235f92 commit 2054c97
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions logtools/src/logtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,23 @@ class Logger
if (!m_Settings.showDate && !m_Settings.showTime)
return;

std::time_t t = std::time(nullptr);
std::tm* now = std::localtime(&t);
time_t t = time(nullptr);
std::tm now{};

#if defined(LOGTOOLS_WINDOWS)
localtime_s(&now, &t);
#elif defined(LOGTOOLS_LINUX)
localtime_r(&t, &now);
#endif

std::printf("[");

if (m_Settings.showDate)
{
std::printf("%.4d-%.2d-%.2d",
now->tm_year + 1900,
now->tm_mon + 1,
now->tm_hour);
now.tm_year + 1900,
now.tm_mon + 1,
now.tm_hour);
}

if (m_Settings.showDate && m_Settings.showTime)
Expand All @@ -269,9 +275,9 @@ class Logger
if (m_Settings.showTime)
{
std::printf("%.2d:%.2d:%.2d",
now->tm_hour,
now->tm_min,
now->tm_sec);
now.tm_hour,
now.tm_min,
now.tm_sec);
}

std::printf("] ");
Expand Down

0 comments on commit 2054c97

Please sign in to comment.