Skip to content

Commit 0b34121

Browse files
committed
feat(logging): Introduce spdlog and remove simple_logger and screen_logger
1 parent 097068d commit 0b34121

21 files changed

+290
-1023
lines changed

.licenserc.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ header:
165165
- 'src/runtime/global_config.h'
166166
- 'src/common/gpid.h'
167167
- 'src/runtime/rpc/group_address.h'
168-
- 'src/utils/logging_provider.h'
169168
- 'src/runtime/rpc/message_parser.h'
170169
- 'src/runtime/rpc/network.h'
171170
- 'src/runtime/rpc/rpc_address.cpp'
@@ -617,8 +616,6 @@ header:
617616
- 'src/utils/lockp.std.h'
618617
- 'src/utils/logging.cpp'
619618
- 'src/utils/shared_io_service.h'
620-
- 'src/utils/simple_logger.cpp'
621-
- 'src/utils/simple_logger.h'
622619
- 'src/utils/strings.cpp'
623620
- 'src/utils/test/CMakeLists.txt'
624621
- 'src/utils/test/clear.sh'
@@ -637,7 +634,6 @@ header:
637634
- 'src/utils/test/join_point_test.cpp'
638635
- 'src/utils/test/json_helper_test.cpp'
639636
- 'src/utils/test/lock.std.cpp'
640-
- 'src/utils/test/logger.cpp'
641637
- 'src/utils/test/logging.cpp'
642638
- 'src/utils/test/output_utils_test.cpp'
643639
- 'src/utils/test/priority_queue.cpp'

cmake_modules/BaseFunctions.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ function(dsn_setup_compiler_flags)
208208
if(${BUILD_TEST})
209209
add_definitions(-DMOCK_TEST)
210210
endif()
211+
add_definitions(-DSPDLOG_FMT_EXTERNAL)
212+
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
211213

212214
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -gdwarf-4" CACHE STRING "" FORCE)
213215

src/http/pprof_http_service.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static bool has_ext(const std::string &name, const std::string &ext)
9898
static int extract_symbols_from_binary(std::map<uintptr_t, std::string> &addr_map,
9999
const lib_info &lib_info)
100100
{
101-
SCOPED_LOG_TIMING(INFO, "load {}", lib_info.path);
101+
SCOPED_LOG_TIMING(info, "load {}", lib_info.path);
102102
std::string cmd = "nm -C -p ";
103103
cmd.append(lib_info.path);
104104
std::stringstream ss;
@@ -195,7 +195,7 @@ static int extract_symbols_from_binary(std::map<uintptr_t, std::string> &addr_ma
195195

196196
static void load_symbols()
197197
{
198-
SCOPED_LOG_TIMING(INFO, "load all symbols");
198+
SCOPED_LOG_TIMING(info, "load all symbols");
199199
auto fp = fopen("/proc/self/maps", "r");
200200
if (fp == nullptr) {
201201
return;
@@ -267,7 +267,7 @@ static void load_symbols()
267267
extract_symbols_from_binary(symbol_map, info);
268268

269269
size_t num_removed = 0;
270-
LOG_TIMING_IF(INFO, num_removed > 0, "removed {} entries", num_removed);
270+
LOG_TIMING_IF(info, num_removed > 0, "removed {} entries", num_removed);
271271
bool last_is_empty = false;
272272
for (auto it = symbol_map.begin(); it != symbol_map.end();) {
273273
if (it->second.empty()) {

src/meta/duplication/meta_duplication_service.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "utils/ports.h"
5656
#include "utils/string_conv.h"
5757
#include "utils/zlocks.h"
58+
#include "spdlog/common.h"
5859

5960
DSN_DECLARE_bool(dup_ignore_other_cluster_ids);
6061

@@ -174,10 +175,10 @@ void meta_duplication_service::do_modify_duplication(std::shared_ptr<app_state>
174175
} while (0)
175176

176177
#define LOG_WARNING_DUP_HINT_AND_RETURN_IF_NOT(expr, resp, ec, ...) \
177-
LOG_DUP_HINT_AND_RETURN_IF_NOT(expr, resp, ec, LOG_LEVEL_WARNING, __VA_ARGS__)
178+
LOG_DUP_HINT_AND_RETURN_IF_NOT(expr, resp, ec, spdlog::level::warn, __VA_ARGS__)
178179

179180
#define LOG_ERROR_DUP_HINT_AND_RETURN_IF_NOT(expr, resp, ec, ...) \
180-
LOG_DUP_HINT_AND_RETURN_IF_NOT(expr, resp, ec, LOG_LEVEL_ERROR, __VA_ARGS__)
181+
LOG_DUP_HINT_AND_RETURN_IF_NOT(expr, resp, ec, spdlog::level::err, __VA_ARGS__)
181182

182183
// This call will not recreate if the duplication
183184
// with the same app name and remote end point already exists.

src/replica/replica_2pc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "utils/ports.h"
7878
#include "utils/thread_access_checker.h"
7979
#include "utils/uniq_timestamp_us.h"
80+
#include "spdlog/common.h"
8081

8182
DSN_DEFINE_bool(replication,
8283
reject_write_when_disk_insufficient,
@@ -238,13 +239,13 @@ void replica::init_prepare(mutation_ptr &mu, bool reconciliation, bool pop_all_c
238239
const auto request_count = mu->client_requests.size();
239240
mu->data.header.last_committed_decree = last_committed_decree();
240241

241-
log_level_t level = LOG_LEVEL_DEBUG;
242+
spdlog::level::level_enum level = spdlog::level::debug;
242243
if (mu->data.header.decree == invalid_decree) {
243244
mu->set_id(get_ballot(), _prepare_list->max_decree() + 1);
244245
// print a debug log if necessary
245246
if (FLAGS_prepare_decree_gap_for_debug_logging > 0 &&
246247
mu->get_decree() % FLAGS_prepare_decree_gap_for_debug_logging == 0)
247-
level = LOG_LEVEL_INFO;
248+
level = spdlog::level::info;
248249
mu->set_timestamp(_uniq_timestamp_us.next());
249250
} else {
250251
mu->set_id(get_ballot(), mu->data.header.decree);

src/runtime/rpc/rpc_host_port.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ host_port host_port::from_address(rpc_address addr)
5555
{
5656
host_port hp;
5757
SCOPED_LOG_SLOW_EXECUTION(
58-
WARNING, 100, "construct host_port '{}' from rpc_address '{}'", hp, addr);
58+
warn, 100, "construct host_port '{}' from rpc_address '{}'", hp, addr);
5959
switch (addr.type()) {
6060
case HOST_TYPE_IPV4: {
6161
const auto s = lookup_hostname(htonl(addr.ip()), &hp._host);
@@ -81,7 +81,7 @@ host_port host_port::from_string(const std::string &host_port_str)
8181
{
8282
host_port hp;
8383
SCOPED_LOG_SLOW_EXECUTION(
84-
WARNING, 100, "construct host_port '{}' from string '{}'", hp, host_port_str);
84+
warn, 100, "construct host_port '{}' from string '{}'", hp, host_port_str);
8585
const auto pos = host_port_str.find_last_of(':');
8686
if (dsn_unlikely(pos == std::string::npos)) {
8787
return hp;

src/runtime/service_api_c.cpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@
7575
#include "utils/flags.h"
7676
#include "utils/fmt_logging.h"
7777
#include "utils/join_point.h"
78-
#include "utils/logging_provider.h"
7978
#include "utils/process_utils.h"
8079
#include "utils/string_conv.h"
8180
#include "utils/strings.h"
8281
#include "utils/sys_exit_hook.h"
8382
#include "utils/threadpool_spec.h"
83+
#include "utils/logging.h"
8484

8585
DSN_DEFINE_bool(
8686
core,
@@ -343,33 +343,6 @@ inline void dsn_global_init()
343343
dsn::service_engine::instance();
344344
}
345345

346-
static std::string dsn_log_prefixed_message_func()
347-
{
348-
const int tid = dsn::utils::get_current_tid();
349-
const auto t = dsn::task::get_current_task_id();
350-
if (t) {
351-
if (nullptr != dsn::task::get_current_worker2()) {
352-
return fmt::format("{}.{}{}.{:016}: ",
353-
dsn::task::get_current_node_name(),
354-
dsn::task::get_current_worker2()->pool_spec().name,
355-
dsn::task::get_current_worker2()->index(),
356-
t);
357-
} else {
358-
return fmt::format(
359-
"{}.io-thrd.{}.{:016}: ", dsn::task::get_current_node_name(), tid, t);
360-
}
361-
} else {
362-
if (nullptr != dsn::task::get_current_worker2()) {
363-
return fmt::format("{}.{}{}: ",
364-
dsn::task::get_current_node_name(),
365-
dsn::task::get_current_worker2()->pool_spec().name,
366-
dsn::task::get_current_worker2()->index());
367-
} else {
368-
return fmt::format("{}.io-thrd.{}: ", dsn::task::get_current_node_name(), tid);
369-
}
370-
}
371-
}
372-
373346
bool run(const char *config_file,
374347
const char *config_arguments,
375348
bool is_server,
@@ -473,10 +446,7 @@ bool run(const char *config_file,
473446
}
474447
475448
// Initialize logging.
476-
dsn_log_init(spec.logging_factory_name,
477-
spec.log_dir,
478-
fmt::format("{}", fmt::join(app_names, ".")),
479-
dsn_log_prefixed_message_func);
449+
dsn_log_init(spec.log_dir, fmt::format("{}", fmt::join(app_names, ".")));
480450
481451
// Prepare the minimum necessary.
482452
::dsn::service_engine::instance().init_before_toollets(spec);

src/runtime/tool_api.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ Component providers define the interface for the local components (e.g., network
5252
// providers
5353
#include "utils/factory_store.h"
5454
#include "utils/join_point.h"
55-
#include "utils/logging_provider.h" // IWYU pragma: keep
5655

5756
namespace dsn {
5857
class service_node;

src/security/sasl_init.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@
2727
#include "utils/flags.h"
2828
#include "utils/fmt_logging.h"
2929
#include "utils/synchronize.h"
30+
#include "spdlog/common.h"
31+
#include "spdlog/spdlog.h"
3032

3133
DSN_DEFINE_string(security, sasl_plugin_path, "/usr/lib/sasl2", "path to search sasl plugins");
3234

3335
namespace dsn {
3436
namespace security {
3537

36-
log_level_t get_log_level(int level)
38+
spdlog::level::level_enum get_log_level(int level)
3739
{
3840
switch (level) {
3941
case SASL_LOG_ERR:
40-
return LOG_LEVEL_ERROR;
42+
return spdlog::level::err;
4143
case SASL_LOG_FAIL:
4244
case SASL_LOG_WARN:
43-
return LOG_LEVEL_WARNING;
45+
return spdlog::level::warn;
4446
case SASL_LOG_NOTE:
45-
return LOG_LEVEL_INFO;
47+
return spdlog::level::info;
4648
default:
47-
return LOG_LEVEL_DEBUG;
49+
return spdlog::level::debug;
4850
}
4951
}
5052

src/utils/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ set(MY_PROJ_LIBS
3737
lz4
3838
zstd
3939
snappy
40-
absl::strings)
40+
absl::strings
41+
spdlog)
4142

4243
# Extra files that will be installed
4344
set(MY_BINPLACES "")

src/utils/api_utilities.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "utils/enum_helper.h"
3232
#include "utils/fmt_utils.h"
3333
#include "utils/ports.h"
34+
#include "spdlog/common.h"
3435

3536
/*!
3637
@defgroup logging Logging Service
@@ -65,12 +66,6 @@ ENUM_END(log_level_t)
6566

6667
USER_DEFINED_ENUM_FORMATTER(log_level_t)
6768

68-
// logs with level smaller than this start_level will not be logged
69-
extern log_level_t log_start_level;
70-
extern log_level_t get_log_start_level();
71-
extern void set_log_start_level(log_level_t level);
72-
extern void global_log(
73-
const char *file, const char *function, const int line, log_level_t log_level, const char *str);
7469
extern void dsn_coredump();
7570

7671
#define dreturn_not_ok_logged(err, ...) \

src/utils/fmt_logging.h

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,51 @@
1919

2020
#pragma once
2121

22+
#include <memory>
2223
#include <fmt/ostream.h>
2324
#include <rocksdb/status.h>
2425

2526
#include "utils/api_utilities.h"
27+
#include "spdlog/common.h"
28+
#include "spdlog/spdlog.h"
29+
#include "utils/logging.h"
2630

27-
// The macros below no longer use the default snprintf method for log message formatting,
28-
// instead we use fmt::format.
29-
// TODO(wutao1): prevent construction of std::string for each log.
31+
extern spdlog::level::level_enum g_stderr_start_level;
32+
extern spdlog::level::level_enum g_file_log_start_level;
33+
extern std::shared_ptr<spdlog::logger> g_stderr_logger;
34+
extern std::shared_ptr<spdlog::logger> g_file_logger;
3035

31-
// __FILENAME__ macro comes from the cmake, in which we calculate a filename without path.
3236
#define LOG(level, ...) \
3337
do { \
34-
if (level >= log_start_level) \
35-
global_log( \
36-
__FILENAME__, __FUNCTION__, __LINE__, level, fmt::format(__VA_ARGS__).c_str()); \
38+
const auto _lvl = (level); \
39+
if (_lvl < g_file_log_start_level) { \
40+
break; \
41+
} \
42+
if (dsn_likely(!!g_file_logger)) { \
43+
g_file_logger->log(_lvl, __VA_ARGS__); \
44+
} else { \
45+
g_stderr_logger->log(_lvl, __VA_ARGS__); \
46+
} \
47+
} while (false)
48+
49+
#define LOG_ON_LEVEL(UPPER_CASE_LEVEL, lower_case_level, ...) \
50+
do { \
51+
if (lower_case_level >= g_stderr_start_level) { \
52+
SPDLOG_LOGGER_##UPPER_CASE_LEVEL(g_stderr_logger, __VA_ARGS__); \
53+
} \
54+
if (dsn_likely(!!g_file_logger)) { \
55+
SPDLOG_LOGGER_##UPPER_CASE_LEVEL(g_file_logger, __VA_ARGS__); \
56+
if (dsn_unlikely(lower_case_level == spdlog::level::critical)) { \
57+
dsn_coredump(); \
58+
} \
59+
} \
3760
} while (false)
3861

39-
#define LOG_DEBUG(...) LOG(LOG_LEVEL_DEBUG, __VA_ARGS__)
40-
#define LOG_INFO(...) LOG(LOG_LEVEL_INFO, __VA_ARGS__)
41-
#define LOG_WARNING(...) LOG(LOG_LEVEL_WARNING, __VA_ARGS__)
42-
#define LOG_ERROR(...) LOG(LOG_LEVEL_ERROR, __VA_ARGS__)
43-
#define LOG_FATAL(...) LOG(LOG_LEVEL_FATAL, __VA_ARGS__)
62+
#define LOG_DEBUG(...) LOG_ON_LEVEL(DEBUG, spdlog::level::debug, __VA_ARGS__)
63+
#define LOG_INFO(...) LOG_ON_LEVEL(INFO, spdlog::level::info, __VA_ARGS__)
64+
#define LOG_WARNING(...) LOG_ON_LEVEL(WARN, spdlog::level::warn, __VA_ARGS__)
65+
#define LOG_ERROR(...) LOG_ON_LEVEL(ERROR, spdlog::level::err, __VA_ARGS__)
66+
#define LOG_FATAL(...) LOG_ON_LEVEL(CRITICAL, spdlog::level::critical, __VA_ARGS__)
4467

4568
#define LOG_WARNING_IF(x, ...) \
4669
do { \

0 commit comments

Comments
 (0)