]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/dout.h
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / common / dout.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2010 Sage Weil <sage@newdream.net>
7 * Copyright (C) 2010 Dreamhost
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 #ifndef CEPH_DOUT_H
17 #define CEPH_DOUT_H
18
19 #include <type_traits>
20
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"
27 #else
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"
33 #include "log/Log.h"
34 #endif
35
36 extern void dout_emergency(const char * const str);
37 extern void dout_emergency(const std::string &str);
38
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");
44 return out;
45 }
46
47 class DoutPrefixProvider {
48 public:
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() {}
53 };
54
55 inline std::ostream &operator<<(
56 std::ostream &lhs, const DoutPrefixProvider &dpp) {
57 return dpp.gen_prefix(lhs);
58 }
59 #if FMT_VERSION >= 90000
60 template <> struct fmt::formatter<DoutPrefixProvider> : fmt::ostream_formatter {};
61 #endif
62
63 // a prefix provider with empty prefix
64 class NoDoutPrefix : public DoutPrefixProvider {
65 CephContext *const cct;
66 const unsigned subsys;
67 public:
68 NoDoutPrefix(CephContext *cct, unsigned subsys) : cct(cct), subsys(subsys) {}
69
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; }
73 };
74
75 // a prefix provider with static (const char*) prefix
76 class DoutPrefix : public NoDoutPrefix {
77 const char *const prefix;
78 public:
79 DoutPrefix(CephContext *cct, unsigned subsys, const char *prefix)
80 : NoDoutPrefix(cct, subsys), prefix(prefix) {}
81
82 std::ostream& gen_prefix(std::ostream& out) const override {
83 return out << prefix;
84 }
85 };
86
87 // a prefix provider that composes itself on top of another
88 class DoutPrefixPipe : public DoutPrefixProvider {
89 const DoutPrefixProvider& dpp;
90 public:
91 DoutPrefixPipe(const DoutPrefixProvider& dpp) : dpp(dpp) {}
92
93 std::ostream& gen_prefix(std::ostream& out) const override final {
94 dpp.gen_prefix(out);
95 add_prefix(out);
96 return out;
97 }
98 CephContext *get_cct() const override { return dpp.get_cct(); }
99 unsigned get_subsys() const override { return dpp.get_subsys(); }
100
101 virtual void add_prefix(std::ostream& out) const = 0;
102 };
103
104 // helpers
105 namespace ceph::dout {
106
107 template<typename T>
108 struct dynamic_marker_t {
109 T value;
110 // constexpr ctor isn't needed as it's an aggregate type
111 constexpr operator T() const { return value; }
112 };
113
114 template<typename T>
115 constexpr dynamic_marker_t<T> need_dynamic(T&& t) {
116 return dynamic_marker_t<T>{ std::forward<T>(t) };
117 }
118
119 template<typename T>
120 struct is_dynamic : public std::false_type {};
121
122 template<typename T>
123 struct is_dynamic<dynamic_marker_t<T>> : public std::true_type {};
124
125 } // ceph::dout
126
127 // generic macros
128 #define dout_prefix *_dout
129
130 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
131 #define dout_impl(cct, sub, v) \
132 do { \
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;
138 #define dendl_impl \
139 ""; \
140 _logger.log(crimson::to_log_level(_lv), \
141 "{}", _out.str().c_str()); \
142 } \
143 } while (0)
144 #else
145 #define dout_impl(cct, sub, v) \
146 do { \
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); \
151 } else { \
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>()); \
156 } \
157 }(cct); \
158 \
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();
166
167 #define dendl_impl std::flush; \
168 _dout_cct->_log->submit_entry(std::move(_dout_e)); \
169 } \
170 } while (0)
171 #endif // WITH_SEASTAR
172
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
176
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)
181
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)
186
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
190
191 #define ldlog_p1(cct, sub, lvl) \
192 (cct->_conf->subsys.should_gather((sub), (lvl)))
193
194 #define dendl dendl_impl
195
196 #endif