Skip to content

Commit

Permalink
Add {function}, {time} and {thread} placeholders
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Artsishevsky <polter.rnd@gmail.com>
  • Loading branch information
polter-rnd committed Oct 11, 2024
1 parent 627dc80 commit 11c7b00
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
32 changes: 30 additions & 2 deletions include/log/pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,17 @@ class Pattern {
*/
struct Placeholder {
/** @brief Log pattern field types. */
enum class Type : std::uint8_t { None, Category, Level, File, Line, Message };
enum class Type : std::uint8_t {
None,
Category,
Level,
File,
Line,
Message,
Function,
Time,
Thread
};

/** @brief Field alignment options. */
enum class Align : std::uint8_t { None, Left, Right, Center };
Expand Down Expand Up @@ -196,6 +206,8 @@ class Pattern {
[[fallthrough]];
case Placeholder::Type::File:
[[fallthrough]];
case Placeholder::Type::Function:
[[fallthrough]];
case Placeholder::Type::Message:
return true;
default:
Expand All @@ -217,15 +229,21 @@ class Pattern {
static constexpr std::array<Char, 6> Level{'l', 'e', 'v', 'e', 'l', '\0'};
static constexpr std::array<Char, 5> File{'f', 'i', 'l', 'e', '\0'};
static constexpr std::array<Char, 5> Line{'l', 'i', 'n', 'e', '\0'};
static constexpr std::array<Char, 9> Function{'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', '\0'};
static constexpr std::array<Char, 5> Time{'t', 'i', 'm', 'e', '\0'};
static constexpr std::array<Char, 7> Thread{'t', 'h', 'r', 'e', 'a', 'd', '\0'};
static constexpr std::array<Char, 8> Message{'m', 'e', 's', 's', 'a', 'g', 'e', '\0'};

public:
/** @brief List of placeholder names. */
static constexpr std::array<Placeholder, 5> List{
static constexpr std::array<Placeholder, 8> List{
{{Placeholder::Type::Category, Placeholders::Category.data()},
{Placeholder::Type::Level, Placeholders::Level.data()},
{Placeholder::Type::File, Placeholders::File.data()},
{Placeholder::Type::Line, Placeholders::Line.data()},
{Placeholder::Type::Function, Placeholders::Function.data()},
{Placeholder::Type::Time, Placeholders::Time.data()},
{Placeholder::Type::Thread, Placeholders::Thread.data()},
{Placeholder::Type::Message, Placeholders::Message.data()}}};
};

Expand Down Expand Up @@ -314,10 +332,20 @@ class Pattern {
format_string(
result, std::get<StringSpecs>(item.value), record.location.filename);
break;
case Placeholder::Type::Function:
format_string(
result, std::get<StringSpecs>(item.value), record.location.function);
break;
case Placeholder::Type::Line:
result.format_runtime(
std::get<StringViewType>(item.value), record.location.line);
break;
case Placeholder::Type::Time:
result.format_runtime(std::get<StringViewType>(item.value), record.time);
break;
case Placeholder::Type::Thread:
result.format_runtime(std::get<StringViewType>(item.value), record.thread_id);
break;
case Placeholder::Type::Message:
std::visit(
Util::Types::Overloaded{
Expand Down
3 changes: 3 additions & 0 deletions include/log/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "util/unicode.h"

#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -182,6 +183,8 @@ struct Record {
Level level = {}; ///< Log level.
Location location = {}; ///< Source code location.
RecordStringView<Char> category = {}; ///< Log category.
std::chrono::system_clock::time_point time = {}; ///< Event time.
std::size_t thread_id = {}; ///< Thread ID.
std::variant<std::reference_wrapper<String>, RecordStringView<Char>> message
= RecordStringView<Char>{}; ///< Log message.
};
Expand Down
6 changes: 5 additions & 1 deletion include/log/sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include "pattern.h"
#include "record.h"

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <memory>
#include <queue>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -333,7 +335,9 @@ class SinkDriver final {
RecordType record
= {level,
{location.file_name(), location.function_name(), location.line()},
std::move(category)};
std::move(category),
std::chrono::system_clock::now(),
std::hash<std::thread::id>{}(std::this_thread::get_id())};

// Flag to check that message has been evaluated
bool evaluated = false;
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>

class null_out_buf : public std::basic_streambuf<wchar_t> {
Expand Down Expand Up @@ -233,7 +232,8 @@ auto main(int /*argc*/, char* /*argv*/[]) -> int
auto log_root = std::make_shared<Log::Logger<std::wstring_view>>(L"main");
auto sink1 = log_root->add_sink<Log::OStreamSink>(
std::wcout,
L"!!!!! {category} [{level}] {file}|{line}: {message}",
L"!!!!! {category} [{level}] <{time:%Y-%m-%d %X}> {thread} "
L"{file}|{line}|{function}: {message}",
std::make_pair(Log::Level::Trace, gen_random<wchar_t>(5)),
std::make_pair(Log::Level::Debug, gen_random<wchar_t>(5)),
std::make_pair(Log::Level::Warning, gen_random<wchar_t>(5)),
Expand Down

0 comments on commit 11c7b00

Please sign in to comment.