Skip to content

Commit

Permalink
add CombinedTags
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed Nov 24, 2023
1 parent fe74c3e commit 46ce2cd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
36 changes: 30 additions & 6 deletions examples/example_custom_tags.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
#include "quill/Quill.h"
#include "quill/Utility.h"

#include <chrono>
#include <thread>

// Define a CustomTags class
class MyCustomTags : public quill::CustomTags
class MyCustomTagsA : public quill::CustomTags
{
public:
constexpr MyCustomTags(char const* tag_a, uint32_t tag_b) : _tag_a(tag_a), _tag_b(tag_b) {}
constexpr MyCustomTagsA(char const* tag_a, uint32_t tag_b) : _tag_a(tag_a), _tag_b(tag_b) {}

void format(std::string& out) const override { out = fmtquill::format("{}:{}", _tag_a, _tag_b); }
void format(std::string& out) const override
{
out.append(fmtquill::format("{}:{}", _tag_a, _tag_b));
}

private:
char const* _tag_a;
uint32_t _tag_b;
};

static constexpr MyCustomTags custom_tags_a{"CUSTOM_TAG_12", 12};
static constexpr MyCustomTags custom_tags_b{"CUSTOM_TAG_23", 23};
// Define another CustomTags class
class MyCustomTagsB : public quill::CustomTags
{
public:
constexpr MyCustomTagsB(char const* tag_a, uint32_t tag_b) : _tag_a(tag_a), _tag_b(tag_b) {}

void format(std::string& out) const override
{
out.append(fmtquill::format("{}:{}", _tag_a, _tag_b));
}

private:
char const* _tag_a;
uint32_t _tag_b;
};

static constexpr MyCustomTagsA custom_tags_a{"CUSTOM_TAG_A", 12};
static constexpr MyCustomTagsB custom_tags_b{"CUSTOM_TAG_B", 23};

// Combine different tags
static constexpr quill::utility::CombinedCustomTags<MyCustomTagsA, MyCustomTagsB> custom_tags_ab{
custom_tags_a, custom_tags_b};

int main()
{
Expand Down Expand Up @@ -48,6 +72,6 @@ int main()
LOG_INFO_WITH_TAGS(logger, custom_tags_a, "Info with custom tags");
LOG_WARNING_WITH_TAGS(logger, custom_tags_b, "Warning with custom tags");
LOG_ERROR_WITH_TAGS(logger, custom_tags_a, "Error with custom tags");
LOG_CRITICAL_WITH_TAGS(logger, custom_tags_b, "Critical with custom tags");
LOG_CRITICAL_WITH_TAGS(logger, custom_tags_ab, "Critical with combined custom tags");
LOG_CRITICAL(logger, "Critical without custom tags");
}
25 changes: 23 additions & 2 deletions quill/include/quill/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

#pragma once

#include "quill/Fmt.h"
#include "quill/detail/misc/Attributes.h"
#include <cstddef>
#include <string>
#include <tuple>

#include "quill/Fmt.h"
#include "quill/detail/misc/Common.h"
#include "quill/detail/misc/Attributes.h"

/**
* Contains useful utilities to assist with logging
Expand Down Expand Up @@ -53,4 +56,22 @@ QUILL_NODISCARD std::string to_string(T const& obj) noexcept
{
return fmtquill::to_string(obj);
}

/**
* Helper class for combining different CustomTag objects
*/
template <typename... TCustomTags>
class CombinedCustomTags : public quill::CustomTags
{
public:
constexpr CombinedCustomTags(TCustomTags... custom_tags) : _tags(std::move(custom_tags)...) {}

virtual void format(std::string& out) const override
{
std::apply([&out](const auto&... tags) { (((tags.format(out)), out.append(", ")), ...); }, _tags);
}

private:
std::tuple<TCustomTags...> _tags;
};
} // namespace quill::utility
1 change: 1 addition & 0 deletions quill/src/PatternFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ fmt_buffer_t const& PatternFormatter::format(std::chrono::nanoseconds timestamp,
{
if (macro_metadata.custom_tags())
{
_custom_tags.clear();
macro_metadata.custom_tags()->format(_custom_tags);
_set_arg_val<Attribute::CustomTags>(_custom_tags);
}
Expand Down

0 comments on commit 46ce2cd

Please sign in to comment.