From 52a0e6c4ce4385ec19877a6aa43a015e11170a41 Mon Sep 17 00:00:00 2001 From: Sebastian Hiebl Date: Sun, 5 Nov 2023 18:57:14 +0100 Subject: [PATCH] add basic timestamp output with UTC strings --- CMakeLists.txt | 4 ++-- src/MainFrame.hpp | 11 ++++------- src/SdJournal.hpp | 10 ++++++++-- src/SdLine.hpp | 16 ++++++++++++---- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc231a6..6fcce98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 20) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") -add_compile_options(-Wall -Wextra -pedantic) +add_compile_options(-Wall -Wextra -pedantic -Wno-unknown-pragmas) set(CURSES_NEED_NCURSES TRUE) find_package(Curses REQUIRED) @@ -36,7 +36,7 @@ add_executable(jess src/main.cpp src/ChunkedJournal.hpp) target_link_libraries(jess ncurses systemd) -if(BUILD_TESTING) +if (BUILD_TESTING) find_package(doctest REQUIRED) include(doctest) # for doctest_discover_tests add_executable(tests src/SdCursor.hpp test/SdCursor_test.cpp) diff --git a/src/MainFrame.hpp b/src/MainFrame.hpp index fec167a..98de05d 100644 --- a/src/MainFrame.hpp +++ b/src/MainFrame.hpp @@ -10,9 +10,7 @@ class MainFrame { jess::NcWindow m_mainWindow{m_rootWindow.height() - 1, m_rootWindow.width(), 0, 0}; public: - explicit MainFrame(jess::NcWindow &rootWindow) : m_rootWindow(rootWindow) { - - } + explicit MainFrame(jess::NcWindow &rootWindow) : m_rootWindow(rootWindow) {} void drawLines(auto lines) { auto it = std::begin(lines); @@ -22,6 +20,7 @@ class MainFrame { m_mainWindow.move(0, 0); for (size_t i = 0; i < windowHeight && it != end; ++i, ++it) { m_mainWindow.move(i, 0); + m_mainWindow.printw("%s ", it->realtimeUtc().c_str()); m_mainWindow.printw("%s", it->message().c_str()); m_mainWindow.clearToEol(); } @@ -31,9 +30,7 @@ class MainFrame { m_mainWindow.refresh(); } - [[nodiscard]] size_t height() const { - return m_mainWindow.height(); - } + [[nodiscard]] size_t height() const { return m_mainWindow.height(); } }; -} +} // namespace jess diff --git a/src/SdJournal.hpp b/src/SdJournal.hpp index dc8e95f..7a7c53d 100644 --- a/src/SdJournal.hpp +++ b/src/SdJournal.hpp @@ -49,7 +49,7 @@ class SdJournal { sd_journal_get_data(handle.get(), sFieldName.data(), reinterpret_cast(&ptr), &uMessageLength); std::string_view ret{static_cast(ptr), uMessageLength}; - if(ret.size() >= sFieldName.size() + 1) { + if (ret.size() >= sFieldName.size() + 1) { return ret.substr(sFieldName.size() + 1); } @@ -59,6 +59,12 @@ class SdJournal { #pragma clang diagnostic pop } - SdLine getLine() { return SdLine{std::string{getFieldString("MESSAGE")}, std::string{getFieldString("MESSAGE_ID")}}; } + std::chrono::time_point getTimestampRealtime() { + uint64_t ret; + sd_journal_get_realtime_usec(handle.get(), &ret); + return std::chrono::time_point{std::chrono::microseconds{ret}}; + } + + SdLine getLine() { return SdLine{std::string{getFieldString("MESSAGE")}, getTimestampRealtime()}; } }; } // namespace jess diff --git a/src/SdLine.hpp b/src/SdLine.hpp index 03a2885..dbd3b3c 100644 --- a/src/SdLine.hpp +++ b/src/SdLine.hpp @@ -1,3 +1,4 @@ +#include #include #include @@ -6,14 +7,21 @@ namespace jess { class SdLine { public: - explicit SdLine(std::string sMessage, std::string sRealtimeTimestamp) - : sMessage(std::move(sMessage)), sRealtimeTimestamp(std::move(sRealtimeTimestamp)) {} + explicit SdLine(std::string sMessage, std::chrono::time_point timestampRealtime) + : sMessage(std::move(sMessage)), timestampRealtime(timestampRealtime) {} [[nodiscard]] std::string message() const { return sMessage; } - [[nodiscard]] std::string realtime() const { return sRealtimeTimestamp; } + [[nodiscard]] std::chrono::time_point realtime() const { return timestampRealtime; } + [[nodiscard]] std::string realtimeUtc() const { + auto time_t_ = std::chrono::system_clock::to_time_t(timestampRealtime); + std::tm tm_ = *std::gmtime(&time_t_); + std::stringstream ss{}; + ss << std::put_time(&tm_, "%Y-%m-%d %H:%M:%S"); + return ss.str(); + } private: std::string sMessage; - std::string sRealtimeTimestamp; + std::chrono::time_point timestampRealtime; }; } // namespace jess