Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: juzzlin/SimpleLogger
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.0.0
Choose a base ref
...
head repository: juzzlin/SimpleLogger
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 27, 2025

  1. Add support for ISO timestamps with milliseconds

    juzzlin committed Jan 27, 2025

    Verified

    This commit was signed with the committer’s verified signature.
    germa89 German
    Copy the full SHA
    30ee1cb View commit details

Commits on Jan 28, 2025

  1. Add missing include header

    juzzlin committed Jan 28, 2025
    Copy the full SHA
    2b57fa6 View commit details
Showing with 25 additions and 0 deletions.
  1. +24 −0 src/simple_logger.cpp
  2. +1 −0 src/simple_logger.hpp
24 changes: 24 additions & 0 deletions src/simple_logger.cpp
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@

#include "simple_logger.hpp"

#include <array>
#include <chrono>
#include <ctime>
#include <fstream>
@@ -220,6 +221,26 @@ void SimpleLogger::Impl::prefixWithLevelAndTag(SimpleLogger::Level level)
m_message << m_symbols[level] << (!m_tag.empty() ? " " + m_tag + ":" : "") << " ";
}

static std::string isoDateTimeMilliseconds()
{
using std::chrono::duration_cast;
using std::chrono::system_clock;

const auto now = system_clock::now();
const auto nowMs = duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; // Milliseconds part
const auto timeTNow = system_clock::to_time_t(now); // Convert to time_t for strftime

// Format the datetime part
std::array<char, 64> datetimeBuffer;
std::strftime(datetimeBuffer.data(), datetimeBuffer.size(), "%Y-%m-%dT%H:%M:%S", std::localtime(&timeTNow));

// Append milliseconds
std::ostringstream oss;
oss << datetimeBuffer.data() << '.' << std::setfill('0') << std::setw(3) << nowMs.count();

return oss.str();
}

void SimpleLogger::Impl::prefixWithTimestamp()
{
std::string timestamp;
@@ -236,6 +257,9 @@ void SimpleLogger::Impl::prefixWithTimestamp()
case SimpleLogger::TimestampMode::ISODateTime: {
timestamp = currentDateTime(system_clock::now(), "%Y-%m-%dT%H:%M:%S");
} break;
case SimpleLogger::TimestampMode::ISODateTimeMilliseconds: {
timestamp = isoDateTimeMilliseconds();
} break;
case SimpleLogger::TimestampMode::EpochSeconds:
timestamp = std::to_string(duration_cast<std::chrono::seconds>(system_clock::now().time_since_epoch()).count());
break;
1 change: 1 addition & 0 deletions src/simple_logger.hpp
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ class SimpleLogger
EpochMilliseconds,
EpochMicroseconds,
ISODateTime,
ISODateTimeMilliseconds,
Custom
};