Skip to content

Commit 0238430

Browse files
BenHuddlestondaverigby
authored andcommitted
MB-30798: Update spdlog to version 1.1
As we need to make changes to spdlog we should first update our version of it to 1.1. Make the necessary changes to our code to use the new spdlog api. Use the new buffer/size style string returned by fmtlib where we perform custom formatting. Change-Id: I616628d50008758e21a54003cf676c1eb3109cc9 Reviewed-on: http://review.couchbase.org/100224 Reviewed-by: Dave Rigby <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent c904851 commit 0238430

File tree

6 files changed

+92
-56
lines changed

6 files changed

+92
-56
lines changed

engines/ep/src/bucket_logger.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ BucketLogger::BucketLogger(const BucketLogger& other)
3737
set_level(spdLogger->level());
3838
}
3939

40-
void BucketLogger::_sink_it(spdlog::details::log_msg& msg) {
40+
void BucketLogger::sink_it_(spdlog::details::log_msg& msg) {
4141
// Get the engine pointer for logging the bucket name.
4242
// Normally we would wish to stop tracking memory at this point to avoid
4343
// tracking any allocations or de-allocations done by the logging library
@@ -74,7 +74,7 @@ void BucketLogger::_sink_it(spdlog::details::log_msg& msg) {
7474
}
7575

7676
// Append the rest of the message and log
77-
fmtString.append(msg.raw.c_str());
77+
fmtString.append(msg.raw.data(), msg.raw.size());
7878
spdLogger->log(msg.level, fmtString);
7979
}
8080

engines/ep/src/bucket_logger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const std::string globalBucketLoggerName = "globalBucketLogger";
5151
* type safety and we don't want to inline a lot of message formatting as
5252
* macros. One global BucketLogger object is created without sinks to perform
5353
* the spd style formatting without logging. Instead of "sinking" this message
54-
* with the BucketLogger we override the _sink_it method to prepend the
54+
* with the BucketLogger we override the sink_it_ method to prepend the
5555
* engine name and log the message as a pre-formatted string using the
5656
* original spdlog::logger passed via the SERVER_API.
5757
*
@@ -81,7 +81,7 @@ class BucketLogger : public spdlog::logger {
8181
const std::string& name, const std::string& prefix = "");
8282

8383
protected:
84-
void _sink_it(spdlog::details::log_msg& msg) override;
84+
void sink_it_(spdlog::details::log_msg& msg) override;
8585

8686
// Connection ID prefix that is printed if set (printed before any other
8787
// prefix or message)

engines/ep/tests/module_tests/kvstore_test.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,13 @@ class MockBucketLogger : public BucketLogger {
551551
const std::string& message));
552552

553553
protected:
554-
// Override the _sink_it method to redirect to the mocked method
554+
// Override the sink_it_ method to redirect to the mocked method
555555
// Must call the mlog method to check the message details as they are
556-
// bundled in the log_msg object
557-
void _sink_it(spdlog::details::log_msg& msg) override {
558-
mlog(msg.level, msg.raw.c_str());
556+
// bundled in the log_msg object. Beware, msg.raw is not null terminated.
557+
// In these test cases however we just search for a substring within the log
558+
// message so this is okay.
559+
void sink_it_(spdlog::details::log_msg& msg) override {
560+
mlog(msg.level, msg.raw.data());
559561
}
560562
};
561563

logger/custom_rotating_file_sink.cc

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "custom_rotating_file_sink.h"
2424

2525
#include <platform/dirutils.h>
26+
#include <spdlog/details/file_helper.h>
27+
#include <spdlog/details/fmt_helper.h>
2628
#include <memory>
2729

2830
static unsigned long find_first_logfile_id(const std::string& basename) {
@@ -64,7 +66,7 @@ custom_rotating_file_sink<Mutex>::custom_rotating_file_sink(
6466
_current_size(0),
6567
_file_helper(std::make_unique<spdlog::details::file_helper>()),
6668
_next_file_id(find_first_logfile_id(base_filename)) {
67-
formatter = std::make_shared<spdlog::pattern_formatter>(
69+
formatter = std::make_unique<spdlog::pattern_formatter>(
6870
log_pattern, spdlog::pattern_time_type::local);
6971
_file_helper->open(calc_filename());
7072
_current_size = _file_helper->size(); // expensive. called only once
@@ -75,17 +77,17 @@ custom_rotating_file_sink<Mutex>::custom_rotating_file_sink(
7577
* this class adds hooks marking the start and end of a logfile.
7678
*/
7779
template <class Mutex>
78-
void custom_rotating_file_sink<Mutex>::_sink_it(
80+
void custom_rotating_file_sink<Mutex>::sink_it_(
7981
const spdlog::details::log_msg& msg) {
80-
_current_size += msg.formatted.size();
82+
_current_size += msg.raw.size();
8183
if (_current_size > _max_size) {
8284
std::unique_ptr<spdlog::details::file_helper> next =
8385
std::make_unique<spdlog::details::file_helper>();
8486
try {
8587
next->open(calc_filename(), true);
8688
addHook(closingLogfile);
8789
std::swap(_file_helper, next);
88-
_current_size = msg.formatted.size();
90+
_current_size = msg.raw.size();
8991
addHook(openingLogfile);
9092
} catch (...) {
9193
// Keep on logging to the this file, but try swap at the next
@@ -94,11 +96,14 @@ void custom_rotating_file_sink<Mutex>::_sink_it(
9496
_next_file_id--;
9597
}
9698
}
97-
_file_helper->write(msg);
99+
fmt::memory_buffer formatted;
100+
formatter->format(msg, formatted);
101+
102+
_file_helper->write(formatted);
98103
}
99104

100105
template <class Mutex>
101-
void custom_rotating_file_sink<Mutex>::_flush() {
106+
void custom_rotating_file_sink<Mutex>::flush_() {
102107
_file_helper->flush();
103108
}
104109

@@ -108,34 +113,43 @@ void custom_rotating_file_sink<Mutex>::addHook(const std::string& hook) {
108113
spdlog::details::log_msg msg;
109114
msg.time = spdlog::details::os::now();
110115
msg.level = spdlog::level::info;
111-
msg.raw << hook;
116+
117+
// Append the hook to the msg
118+
spdlog::details::fmt_helper::append_str(hook, msg.raw);
112119

113120
if (hook == openingLogfile) {
114-
msg.raw << fmt::StringRef(_file_helper->filename().data(),
115-
_file_helper->filename().size());
121+
spdlog::details::fmt_helper::append_str(
122+
std::string(_file_helper->filename().data(),
123+
_file_helper->filename().size()),
124+
msg.raw);
116125
}
117-
formatter->format(msg);
118-
_current_size += msg.formatted.size();
126+
fmt::memory_buffer formatted;
127+
formatter->format(msg, formatted);
128+
_current_size += formatted.size();
119129

120-
_file_helper->write(msg);
130+
_file_helper->write(formatted);
121131
}
122132

133+
/**
134+
* Create a new filename. If this breaks when updating spdlog then compare
135+
* functionality to the calc_filename() method in the spdlog rotating_file_sink
136+
* @tparam Mutex
137+
* @return An spdlog filename
138+
*/
123139
template <class Mutex>
124140
spdlog::filename_t custom_rotating_file_sink<Mutex>::calc_filename() {
125-
std::conditional<std::is_same<spdlog::filename_t::value_type, char>::value,
126-
fmt::MemoryWriter,
127-
fmt::WMemoryWriter>::type w;
128-
129141
char fname[1024];
130142
unsigned long try_id = _next_file_id;
131143
do {
132144
sprintf(fname, "%s.%06lu.txt", _base_filename.c_str(), try_id++);
133145
} while (access(fname, F_OK) == 0);
134146

135147
_next_file_id = try_id;
136-
137-
w.write(SPDLOG_FILENAME_T("{}"), fname);
138-
return w.str();
148+
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
149+
return fmt::to_wstring(fname);
150+
#else
151+
return fmt::to_string(fname);
152+
#endif
139153
}
140154

141155
template <class Mutex>

logger/custom_rotating_file_sink.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
#include "config.h"
2626

2727
#include <spdlog/sinks/base_sink.h>
28-
#include <spdlog/spdlog.h>
28+
29+
namespace spdlog {
30+
namespace details {
31+
class file_helper;
32+
} // namespace details
33+
} // namespace spdlog
2934

3035
/**
3136
* Customised version of spdlog's rotating_file_sink with the following
@@ -37,7 +42,7 @@
3742
* 2 Instead of renaming all of the files every time we're rotating to
3843
* the next file we start a new log file with a higher number
3944
*
40-
* TODO: If updating spdlog from v0.14.0, check if this class also needs
45+
* TODO: If updating spdlog from v1.1.0, check if this class also needs
4146
* updating.
4247
*/
4348
template <class Mutex>
@@ -50,8 +55,8 @@ class custom_rotating_file_sink : public spdlog::sinks::base_sink<Mutex> {
5055
~custom_rotating_file_sink() override;
5156

5257
protected:
53-
void _sink_it(const spdlog::details::log_msg& msg) override;
54-
void _flush() override;
58+
void sink_it_(const spdlog::details::log_msg& msg) override;
59+
void flush_() override;
5560

5661
private:
5762
void addHook(const std::string& hook);
@@ -62,7 +67,7 @@ class custom_rotating_file_sink : public spdlog::sinks::base_sink<Mutex> {
6267
const std::size_t _max_size;
6368
std::size_t _current_size;
6469
std::unique_ptr<spdlog::details::file_helper> _file_helper;
65-
spdlog::formatter_ptr formatter;
70+
std::unique_ptr<spdlog::pattern_formatter> formatter;
6671
unsigned long _next_file_id;
6772

6873
const std::string openingLogfile{"---------- Opening logfile: "};

logger/spdlogger.cc

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,15 @@
2323
#include "logger_config.h"
2424

2525
#include <memcached/engine.h>
26+
#include <spdlog/async.h>
27+
#include <spdlog/async_logger.h>
28+
#include <spdlog/sinks/ansicolor_sink.h>
2629
#include <spdlog/sinks/dist_sink.h>
2730
#include <spdlog/sinks/null_sink.h>
31+
#include <spdlog/sinks/stdout_color_sinks.h>
2832
#include <spdlog/sinks/stdout_sinks.h>
29-
#include <spdlog/spdlog.h>
3033
#include <chrono>
3134
#include <cstdio>
32-
33-
#ifndef WIN32
34-
#include <spdlog/sinks/ansicolor_sink.h>
35-
36-
#endif
37-
3835
static const std::string logger_name{"spdlog_file_logger"};
3936

4037
/**
@@ -44,7 +41,7 @@ static const std::string logger_name{"spdlog_file_logger"};
4441
* TODO: Remove the duplication in the future, by (maybe) moving
4542
* the const to a header file.
4643
*/
47-
static const std::string log_pattern{"%Y-%m-%dT%T.%fZ %l %v"};
44+
static const std::string log_pattern{"%^%Y-%m-%dT%T.%fZ %l %v%$"};
4845

4946
/**
5047
* Instances of spdlog (async) file logger.
@@ -132,12 +129,11 @@ boost::optional<std::string> cb::logger::initialize(
132129
}
133130

134131
if (logger_settings.console) {
135-
#ifdef WIN32
136-
auto stderrsink = std::make_shared<spdlog::sinks::stderr_sink_mt>();
137-
#else
138132
auto stderrsink =
139-
std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
140-
#endif
133+
std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
134+
135+
// Set the formatting pattern of this sink
136+
stderrsink->set_pattern(log_pattern);
141137
if (logger_settings.unit_test) {
142138
stderrsink->set_level(spdlog::level::trace);
143139
} else {
@@ -147,25 +143,35 @@ boost::optional<std::string> cb::logger::initialize(
147143
}
148144

149145
spdlog::drop(logger_name);
146+
150147
if (logger_settings.unit_test) {
151-
file_logger = spdlog::create(logger_name, sink);
148+
file_logger = std::make_shared<spdlog::logger>(logger_name, sink);
152149
} else {
153-
file_logger = spdlog::create_async(
150+
// Create the default thread pool for async logging
151+
spdlog::init_thread_pool(buffersz, 1);
152+
153+
// Get the thread pool so that we can actually construct the
154+
// object with already created sinks...
155+
auto tp = spdlog::thread_pool();
156+
file_logger = std::make_shared<spdlog::async_logger>(
154157
logger_name,
155158
sink,
156-
buffersz,
157-
spdlog::async_overflow_policy::block_retry,
158-
nullptr,
159-
std::chrono::milliseconds(200));
159+
tp,
160+
spdlog::async_overflow_policy::block);
160161
}
162+
163+
file_logger->set_pattern(log_pattern);
164+
file_logger->set_level(logger_settings.log_level);
165+
166+
// Set the flushing interval policy
167+
spdlog::flush_every(std::chrono::seconds(1));
168+
169+
spdlog::register_logger(file_logger);
161170
} catch (const spdlog::spdlog_ex& ex) {
162171
std::string msg =
163172
std::string{"Log initialization failed: "} + ex.what();
164173
return boost::optional<std::string>{msg};
165174
}
166-
167-
file_logger->set_pattern(log_pattern);
168-
file_logger->set_level(logger_settings.log_level);
169175
return {};
170176
}
171177

@@ -174,24 +180,33 @@ spdlog::logger* cb::logger::get() {
174180
}
175181

176182
void cb::logger::reset() {
183+
spdlog::drop(logger_name);
177184
file_logger.reset();
178185
}
179186

180187
void cb::logger::createBlackholeLogger() {
181188
// delete if already exists
182189
spdlog::drop(logger_name);
183190

184-
file_logger = spdlog::create(
191+
file_logger = std::make_shared<spdlog::logger>(
185192
logger_name, std::make_shared<spdlog::sinks::null_sink_mt>());
186193

187194
file_logger->set_level(spdlog::level::off);
188195
file_logger->set_pattern(log_pattern);
196+
197+
spdlog::register_logger(file_logger);
189198
}
190199

191200
void cb::logger::createConsoleLogger() {
192201
// delete if already exists
193202
spdlog::drop(logger_name);
194-
file_logger = spdlog::stderr_color_mt(logger_name);
203+
204+
auto stderrsink =
205+
std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
206+
207+
file_logger = std::make_shared<spdlog::logger>(logger_name, stderrsink);
195208
file_logger->set_level(spdlog::level::info);
196209
file_logger->set_pattern(log_pattern);
210+
211+
spdlog::register_logger(file_logger);
197212
}

0 commit comments

Comments
 (0)