Skip to content

Commit 9239b4b

Browse files
authored
Merge pull request ceph#58229 from cbodley/wip-dout-fmt
dout: add macros for libfmt-style logging Reviewed-by: Matt Benjamin <[email protected]> Reviewed-by: Patrick Donnelly <[email protected]> Reviewed-by: Kefu Chai <[email protected]>
2 parents 64880d7 + a58d07a commit 9239b4b

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/common/dout_fmt.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab ft=cpp
3+
4+
/*
5+
* Ceph - scalable distributed file system
6+
*
7+
* Copyright contributors to the Ceph project
8+
*
9+
* This is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License version 2.1, as published by the Free Software
12+
* Foundation. See file COPYING.
13+
*
14+
*/
15+
16+
#pragma once
17+
18+
#include <iosfwd>
19+
#include <iterator>
20+
#include <fmt/ostream.h>
21+
#include "dout.h"
22+
23+
/// \file dout_fmt.h
24+
///
25+
/// \brief dout macros to format log statements with libfmt
26+
///
27+
/// A set of dout macros taking a format string and its corresponding argument
28+
/// list. Log output is written directly to the underlying std::ostream by
29+
/// fmt::print() rather than exposing the stream for ostream operator
30+
/// chaining.
31+
32+
// work around "warning: value computed is not used" with default dout_prefix
33+
inline void dout_fmt_use_prefix(std::ostream&) {}
34+
35+
#define lsubdout_fmt(cct, sub, v, ...) \
36+
dout_impl(cct, ceph_subsys_##sub, v) \
37+
dout_fmt_use_prefix(dout_prefix); \
38+
fmt::print(*_dout, __VA_ARGS__); \
39+
*_dout << dendl
40+
41+
#define ldout_fmt(cct, v, ...) \
42+
dout_impl(cct, dout_subsys, v) \
43+
dout_fmt_use_prefix(dout_prefix); \
44+
fmt::print(*_dout, __VA_ARGS__); \
45+
*_dout << dendl
46+
47+
#define dout_fmt(v, ...) \
48+
ldout_fmt((dout_context), v, __VA_ARGS__)
49+
50+
#define ldpp_dout_fmt(dpp, v, ...) \
51+
if (decltype(auto) pdpp = (dpp); pdpp) { /* workaround -Wnonnull-compare for 'this' */ \
52+
dout_impl(pdpp->get_cct(), ceph::dout::need_dynamic(pdpp->get_subsys()), v) \
53+
pdpp->gen_prefix(*_dout); \
54+
fmt::print(*_dout, __VA_ARGS__); \
55+
*_dout << dendl; \
56+
}

src/test/common/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ target_link_libraries(unittest_dns_resolve global)
312312
add_ceph_unittest(unittest_dns_resolve)
313313
endif()
314314

315+
add_executable(unittest_dout_fmt test_dout_fmt.cc $<TARGET_OBJECTS:unit-main>)
316+
target_link_libraries(unittest_dout_fmt global)
317+
add_ceph_unittest(unittest_dout_fmt)
318+
315319
# We're getting an ICE when trying to compile this test using mingw-gcc and
316320
# recent Boost versions. Note that mingw-llvm works fine.
317321
if (NOT WIN32 OR (NOT(CMAKE_CXX_COMPILER_ID STREQUAL GNU)))

src/test/common/test_dout_fmt.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab ft=cpp
3+
4+
/*
5+
* Ceph - scalable distributed file system
6+
*
7+
* Copyright contributors to the Ceph project
8+
*
9+
* This is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License version 2.1, as published by the Free Software
12+
* Foundation. See file COPYING.
13+
*
14+
*/
15+
16+
#include "common/dout_fmt.h"
17+
#include <gtest/gtest.h>
18+
19+
TEST(DoutFmt, SubDout)
20+
{
21+
// expect level 0 to always be gathered
22+
lsubdout_fmt(g_ceph_context, test, 0, "{}: {}", "value", 42);
23+
// expect level 99 to be compiled out
24+
lsubdout_fmt(g_ceph_context, test, 99, "{}: {}", "value", 42);
25+
}
26+
27+
#define dout_subsys ceph_subsys_test
28+
29+
TEST(DoutFmt, Dout)
30+
{
31+
ldout_fmt(g_ceph_context, 0, "{}: {}", "value", 42);
32+
ldout_fmt(g_ceph_context, 99, "{}: {}", "value", 42);
33+
}
34+
35+
#define dout_context g_ceph_context
36+
37+
TEST(DoutFmt, DoutContext)
38+
{
39+
dout_fmt(0, "{}: {}", "value", 42);
40+
dout_fmt(99, "{}: {}", "value", 42);
41+
}
42+
43+
#undef dout_prefix
44+
#define dout_prefix *_dout << "prefix: "
45+
46+
TEST(DoutFmt, DoutPrefix)
47+
{
48+
ldout_fmt(g_ceph_context, 0, "{}: {}", "value", 42);
49+
ldout_fmt(g_ceph_context, 99, "{}: {}", "value", 42);
50+
}
51+
52+
TEST(DoutFmt, DppDout)
53+
{
54+
const DoutPrefix dpp{g_ceph_context, dout_subsys, "prefix: "};
55+
ldpp_dout_fmt(&dpp, 0, "{}: {}", "value", 42);
56+
ldpp_dout_fmt(&dpp, 99, "{}: {}", "value", 42);
57+
}

0 commit comments

Comments
 (0)