Skip to content

Commit

Permalink
add basic timestamp output with UTC strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bastidest committed Nov 5, 2023
1 parent f7db158 commit 52a0e6c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions src/MainFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand All @@ -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
10 changes: 8 additions & 2 deletions src/SdJournal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SdJournal {
sd_journal_get_data(handle.get(), sFieldName.data(), reinterpret_cast<const void **>(&ptr), &uMessageLength);
std::string_view ret{static_cast<const char *>(ptr), uMessageLength};

if(ret.size() >= sFieldName.size() + 1) {
if (ret.size() >= sFieldName.size() + 1) {
return ret.substr(sFieldName.size() + 1);
}

Expand All @@ -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<std::chrono::system_clock> getTimestampRealtime() {
uint64_t ret;
sd_journal_get_realtime_usec(handle.get(), &ret);
return std::chrono::time_point<std::chrono::system_clock>{std::chrono::microseconds{ret}};
}

SdLine getLine() { return SdLine{std::string{getFieldString("MESSAGE")}, getTimestampRealtime()}; }
};
} // namespace jess
16 changes: 12 additions & 4 deletions src/SdLine.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <chrono>
#include <string>
#include <utility>

Expand All @@ -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<std::chrono::system_clock> 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<std::chrono::system_clock> 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<std::chrono::system_clock> timestampRealtime;
};
} // namespace jess

0 comments on commit 52a0e6c

Please sign in to comment.