Skip to content

Commit

Permalink
Accept string view as extra param inline (#36)
Browse files Browse the repository at this point in the history
* log: adding support for passing string_view inline

* logging: use perfect forwarding

* formatting

---------

Co-authored-by: Lucas Cimon <[email protected]>
  • Loading branch information
felixguendling and Lucas-C authored Jan 21, 2025
1 parent ecd49ba commit 2d8b992
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[fmt]
[email protected]:motis-project/fmt.git
branch=master
commit=edb385ac526c24bc917ec4a41bb0edb28f0ca59e
commit=dc10f83be70ac2873d5f8d1ce317596f1fd318a2
[cista]
[email protected]:felixguendling/cista.git
branch=master
Expand Down
3 changes: 1 addition & 2 deletions include/utl/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ inline std::string now() {
/// Produce a new log line at the given `level`.
template <log_level LogLevel, typename... Args>
struct log {
log(const char* ctx, fmt::format_string<Args...> fmt_str,
const Args&&... args,
log(const char* ctx, fmt::format_string<Args...> fmt_str, Args&&... args,
std::source_location const& loc = std::source_location::current())
: loc_{loc},
ctx_{ctx},
Expand Down
9 changes: 9 additions & 0 deletions test/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ TEST(log, accept_string_view_as_extra_param) {
"Hello world!\n"));
};

TEST(log, accept_string_view_as_extra_param_inline) {
testing::internal::CaptureStderr();
std::string str{"world"};
utl::log_info("MyCtx", "Hello {}!", std::string_view{str});
EXPECT_THAT(testing::internal::GetCapturedStderr(),
MatchesRegex(".+T.+Z \\[info\\] \\[logging.+:.+\\] \\[MyCtx\\] "
"Hello world!\n"));
};

TEST(log, can_have_optional_attrs) {
testing::internal::CaptureStderr();
utl::log_info("MyCtx", "Message").attrs({{"key", "value"}});
Expand Down

0 comments on commit 2d8b992

Please sign in to comment.