Skip to content

Commit

Permalink
Merge pull request #50 from Ttibsi/logging-date-time
Browse files Browse the repository at this point in the history
Add datetime to logger
  • Loading branch information
Ttibsi authored Oct 27, 2024
2 parents 6d1234d + 47bfedb commit c1abf54
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/logger.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#ifndef LOGGER_H
#define LOGGER_H

#include <chrono>
#include <concepts>
#include <cstdlib>
#include <format>
#include <fstream>
#include <string>
#include <string_view>

using namespace std::chrono;

inline const std::string log_file = "iris.log";

enum class Level { INFO, WARNING, ERROR };
Expand All @@ -24,12 +28,16 @@ enum class Level { INFO, WARNING, ERROR };
}
}

[[nodiscard]] inline const std::string formatted_time(const time_point<system_clock>& time) {
const auto now = round<seconds>(time);
return std::format("[{:%y-%m-%d %H:%M:%S}]", now);
}

template <typename T>
concept Streamable = requires(std::ostream& out, T in) {
{ out << in } -> std::convertible_to<std::ostream&>;
};

// TODO: Add date/time stamp to log
template <Streamable T = std::string_view>
inline void log(Level lvl, T msg) {
if (std::getenv("RAWTERM_DEBUG") != nullptr) {
Expand All @@ -38,7 +46,7 @@ inline void log(Level lvl, T msg) {

std::ofstream out;
out.open(log_file, std::ios::app);
out << level_str(lvl) << " " << msg << "\n";
out << formatted_time(system_clock::now()) << level_str(lvl) << " " << msg << "\n";
out.close();
}

Expand Down
22 changes: 18 additions & 4 deletions tests/logger_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdlib>

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

#include "file_io.h"
Expand All @@ -12,6 +13,19 @@ TEST_CASE("level_str", "[LOGGER]") {
REQUIRE(level_str(Level::WARNING) == "[WARNING]");
}

TEST_CASE("formatted_time", "[LOGGER]") {
const std::string given_time = formatted_time(system_clock::now());

REQUIRE(given_time.size() == 19);

REQUIRE(given_time.at(0) == '[');
REQUIRE(given_time.at(18) == ']');
REQUIRE_THAT(std::stof(given_time.substr(1, 2)), Catch::Matchers::WithinAbs(0.0, 31.0));
REQUIRE_THAT(std::stof(given_time.substr(3, 2)), Catch::Matchers::WithinAbs(0.0, 12.0));
REQUIRE_THAT(std::stof(given_time.substr(10, 2)), Catch::Matchers::WithinAbs(0.0, 23.0));
REQUIRE_THAT(std::stof(given_time.substr(10, 2)), Catch::Matchers::WithinAbs(0.0, 59.0));
}

TEST_CASE("log", "[LOGGER]") {
unsetenv("RAWTERM_DEBUG");

Expand All @@ -24,8 +38,8 @@ TEST_CASE("log", "[LOGGER]") {
std::string contents_str =
std::string(file_contents.value().begin(), file_contents.value().end());

std::string expected = "[INFO] hello world";
REQUIRE_THAT(contents_str, Catch::Matchers::ContainsSubstring(expected));
REQUIRE_THAT(contents_str, Catch::Matchers::ContainsSubstring("INFO"));
REQUIRE_THAT(contents_str, Catch::Matchers::ContainsSubstring("hello world"));
}

SECTION("Given level") {
Expand All @@ -37,8 +51,8 @@ TEST_CASE("log", "[LOGGER]") {
std::string contents_str =
std::string(file_contents.value().begin(), file_contents.value().end());

std::string expected = "[WARNING] hello world";
REQUIRE(contents_str.find(expected) != std::string::npos);
REQUIRE_THAT(contents_str, Catch::Matchers::ContainsSubstring("WARNING"));
REQUIRE_THAT(contents_str, Catch::Matchers::ContainsSubstring("hello world"));
}

setenv("RAWTERM_DEBUG", "True", 1);
Expand Down

0 comments on commit c1abf54

Please sign in to comment.