Skip to content

Commit

Permalink
add CombinedTags
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed Nov 25, 2023
1 parent e9e57b1 commit 6b26a73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/example_custom_tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static constexpr MyCustomTagsB custom_tags_b{"CUSTOM_TAG_B", 23};
static constexpr quill::utility::CombinedCustomTags<MyCustomTagsA, MyCustomTagsB> custom_tags_ab{
custom_tags_a, custom_tags_b};

static constexpr quill::utility::CombinedCustomTags<MyCustomTagsA, MyCustomTagsB> custom_tags_ab_custom_format_delimiter{
custom_tags_a, custom_tags_b, " -- "};

int main()
{
// Set a custom formatter for stdout that prints the tags
Expand Down Expand Up @@ -73,5 +76,6 @@ int main()
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_ab, "Critical with combined custom tags");
LOG_CRITICAL_WITH_TAGS(logger, custom_tags_ab_custom_format_delimiter, "Critical with combined custom tags custom delimiter");
LOG_CRITICAL(logger, "Critical without custom tags");
}
18 changes: 15 additions & 3 deletions quill/include/quill/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

#include <cstddef>
#include <string>
#include <string_view>
#include <tuple>

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

/**
* Contains useful utilities to assist with logging
Expand Down Expand Up @@ -64,14 +65,25 @@ template <typename... TCustomTags>
class CombinedCustomTags : public CustomTags
{
public:
constexpr CombinedCustomTags(TCustomTags... custom_tags) : _tags(std::move(custom_tags)...) {}
constexpr CombinedCustomTags(TCustomTags... custom_tags, std::string_view delim = ", ")
: _tags(std::move(custom_tags)...), _delim(delim)
{
}

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

if (!out.empty())
{
// erase last delim
out.erase(out.length() - _delim.length(), _delim.length());
}
}

private:
std::tuple<TCustomTags...> _tags;
std::string_view _delim;
};
} // namespace quill::utility

0 comments on commit 6b26a73

Please sign in to comment.