1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2010 Sage Weil <sage@newdream.net>
7 * Copyright (C) 2010 Dreamhost
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.
19 #include <type_traits>
21 #include "include/ceph_assert.h"
22 #include "include/common_fwd.h"
23 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
24 #include <seastar/util/log.hh>
25 #include "crimson/common/log.h"
26 #include "crimson/common/config_proxy.h"
28 #include "global/global_context.h"
29 #include "common/ceph_context.h"
30 #include "common/config.h"
31 #include "common/likely.h"
32 #include "common/Clock.h"
36 extern void dout_emergency(const char * const str
);
37 extern void dout_emergency(const std::string
&str
);
39 // intentionally conflict with endl
40 class _bad_endl_use_dendl_t
{ public: _bad_endl_use_dendl_t(int) {} };
41 static const _bad_endl_use_dendl_t endl
= 0;
42 inline std::ostream
& operator<<(std::ostream
& out
, _bad_endl_use_dendl_t
) {
43 ceph_abort_msg("you are using the wrong endl.. use std::endl or dendl");
47 class DoutPrefixProvider
{
49 virtual std::ostream
& gen_prefix(std::ostream
& out
) const = 0;
50 virtual CephContext
*get_cct() const = 0;
51 virtual unsigned get_subsys() const = 0;
52 virtual ~DoutPrefixProvider() {}
55 inline std::ostream
&operator<<(
56 std::ostream
&lhs
, const DoutPrefixProvider
&dpp
) {
57 return dpp
.gen_prefix(lhs
);
59 #if FMT_VERSION >= 90000
60 template <> struct fmt::formatter
<DoutPrefixProvider
> : fmt::ostream_formatter
{};
63 // a prefix provider with empty prefix
64 class NoDoutPrefix
: public DoutPrefixProvider
{
65 CephContext
*const cct
;
66 const unsigned subsys
;
68 NoDoutPrefix(CephContext
*cct
, unsigned subsys
) : cct(cct
), subsys(subsys
) {}
70 std::ostream
& gen_prefix(std::ostream
& out
) const override
{ return out
; }
71 CephContext
*get_cct() const override
{ return cct
; }
72 unsigned get_subsys() const override
{ return subsys
; }
75 // a prefix provider with static (const char*) prefix
76 class DoutPrefix
: public NoDoutPrefix
{
77 const char *const prefix
;
79 DoutPrefix(CephContext
*cct
, unsigned subsys
, const char *prefix
)
80 : NoDoutPrefix(cct
, subsys
), prefix(prefix
) {}
82 std::ostream
& gen_prefix(std::ostream
& out
) const override
{
87 // a prefix provider that composes itself on top of another
88 class DoutPrefixPipe
: public DoutPrefixProvider
{
89 const DoutPrefixProvider
& dpp
;
91 DoutPrefixPipe(const DoutPrefixProvider
& dpp
) : dpp(dpp
) {}
93 std::ostream
& gen_prefix(std::ostream
& out
) const override final
{
98 CephContext
*get_cct() const override
{ return dpp
.get_cct(); }
99 unsigned get_subsys() const override
{ return dpp
.get_subsys(); }
101 virtual void add_prefix(std::ostream
& out
) const = 0;
105 namespace ceph::dout
{
108 struct dynamic_marker_t
{
110 // constexpr ctor isn't needed as it's an aggregate type
111 constexpr operator T() const { return value
; }
115 constexpr dynamic_marker_t
<T
> need_dynamic(T
&& t
) {
116 return dynamic_marker_t
<T
>{ std::forward
<T
>(t
) };
120 struct is_dynamic
: public std::false_type
{};
123 struct is_dynamic
<dynamic_marker_t
<T
>> : public std::true_type
{};
128 #define dout_prefix *_dout
130 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
131 #define dout_impl(cct, sub, v) \
133 if (crimson::common::local_conf()->subsys.should_gather(sub, v)) { \
134 seastar::logger& _logger = crimson::get_logger(sub); \
135 const auto _lv = v; \
136 std::ostringstream _out; \
137 std::ostream* _dout = &_out;
140 _logger.log(crimson::to_log_level(_lv), \
141 "{}", _out.str().c_str()); \
145 #define dout_impl(cct, sub, v) \
147 const bool should_gather = [&](const auto cctX) { \
148 if constexpr (ceph::dout::is_dynamic<decltype(sub)>::value || \
149 ceph::dout::is_dynamic<decltype(v)>::value) { \
150 return cctX->_conf->subsys.should_gather(sub, v); \
152 /* The parentheses are **essential** because commas in angle \
153 * brackets are NOT ignored on macro expansion! A language's \
154 * limitation, sorry. */ \
155 return (cctX->_conf->subsys.template should_gather<sub, v>()); \
159 if (should_gather) { \
160 ceph::logging::MutableEntry _dout_e(v, sub); \
161 static_assert(std::is_convertible<decltype(&*cct), \
162 CephContext* >::value, \
163 "provided cct must be compatible with CephContext*"); \
164 auto _dout_cct = cct; \
165 std::ostream* _dout = &_dout_e.get_ostream();
167 #define dendl_impl std::flush; \
168 _dout_cct->_log->submit_entry(std::move(_dout_e)); \
171 #endif // WITH_SEASTAR
173 #define lsubdout(cct, sub, v) dout_impl(cct, ceph_subsys_##sub, v) dout_prefix
174 #define ldout(cct, v) dout_impl(cct, dout_subsys, v) dout_prefix
175 #define lderr(cct) dout_impl(cct, ceph_subsys_, -1) dout_prefix
177 #define ldpp_subdout(dpp, sub, v) \
178 if (decltype(auto) pdpp = (dpp); pdpp) /* workaround -Wnonnull-compare for 'this' */ \
179 dout_impl(pdpp->get_cct(), ceph_subsys_##sub, v) \
180 pdpp->gen_prefix(*_dout)
182 #define ldpp_dout(dpp, v) \
183 if (decltype(auto) pdpp = (dpp); pdpp) /* workaround -Wnonnull-compare for 'this' */ \
184 dout_impl(pdpp->get_cct(), ceph::dout::need_dynamic(pdpp->get_subsys()), v) \
185 pdpp->gen_prefix(*_dout)
187 #define lgeneric_subdout(cct, sub, v) dout_impl(cct, ceph_subsys_##sub, v) *_dout
188 #define lgeneric_dout(cct, v) dout_impl(cct, ceph_subsys_, v) *_dout
189 #define lgeneric_derr(cct) dout_impl(cct, ceph_subsys_, -1) *_dout
191 #define ldlog_p1(cct, sub, lvl) \
192 (cct->_conf->subsys.should_gather((sub), (lvl)))
194 #define dendl dendl_impl