Skip to content

Commit

Permalink
cleanup more
Browse files Browse the repository at this point in the history
  • Loading branch information
acelyc111 committed Jul 31, 2024
1 parent 3aeb27b commit 287abe1
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/http/pprof_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void load_symbols()
extract_symbols_from_binary(symbol_map, info);

size_t num_removed = 0;
LOG_TIMING_IF(spdlog::level::info, num_removed > 0, "removed {} entries", num_removed);
LOG_TIMING_IF(info, num_removed > 0, "removed {} entries", num_removed);
bool last_is_empty = false;
for (auto it = symbol_map.begin(); it != symbol_map.end();) {
if (it->second.empty()) {
Expand Down
3 changes: 1 addition & 2 deletions src/security/sasl_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ int sasl_simple_logger(void *context, int level, const char *msg)
return SASL_OK;
}

const auto lvl = get_log_level(level);
LOG(lvl, "sasl log info: {}", msg);
LOG(get_log_level(level), "sasl log info: {}", msg);
return SASL_OK;
}

Expand Down
4 changes: 0 additions & 4 deletions src/utils/api_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ ENUM_END(log_level_t)

USER_DEFINED_ENUM_FORMATTER(log_level_t)

// logs with level smaller than this start_level will not be logged
extern spdlog::level::level_enum log_start_level;
extern spdlog::level::level_enum get_log_start_level();
extern void set_log_start_level(spdlog::level::level_enum level);
extern void dsn_coredump();

#define dreturn_not_ok_logged(err, ...) \
Expand Down
28 changes: 0 additions & 28 deletions src/utils/fmt_logging.cpp

This file was deleted.

8 changes: 5 additions & 3 deletions src/utils/fmt_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@
#include "spdlog/common.h"
#include "spdlog/spdlog.h"

extern spdlog::level::level_enum g_log_start_level;
extern std::shared_ptr<spdlog::logger> stderr_logger;
extern std::shared_ptr<spdlog::logger> file_logger;

#define LOG(level, ...) \
do { \
if (level < log_start_level) { \
const auto _lvl = (level); \
if (_lvl < g_log_start_level) { \
break; \
} \
if (dsn_likely(!!file_logger)) { \
file_logger->log(level, __VA_ARGS__); \
file_logger->log(_lvl, __VA_ARGS__); \
} else { \
stderr_logger->log(level, __VA_ARGS__); \
stderr_logger->log(_lvl, __VA_ARGS__); \
} \
} while (false)

Expand Down
31 changes: 19 additions & 12 deletions src/utils/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@
#include "utils/join_point.h"
#include "utils/logging_provider.h"
#include "utils/sys_exit_hook.h"
#include "spdlog/common.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_sinks.h"

DSN_DEFINE_string(core,
logging_start_level,
"LOG_LEVEL_INFO",
"Logs with level larger than or equal to this level be logged");
DSN_DEFINE_validator(logging_start_level, [](const char *value) -> bool {
const auto level = enum_from_string(value, LOG_LEVEL_INVALID);
return LOG_LEVEL_DEBUG <= level && level <= LOG_LEVEL_FATAL;
});

DSN_DEFINE_bool(core,
logging_flush_on_exit,
Expand Down Expand Up @@ -85,9 +92,16 @@ DSN_DEFINE_validator(stderr_start_level, [](const char *value) -> bool {
return LOG_LEVEL_DEBUG <= level && level <= LOG_LEVEL_FATAL;
});

spdlog::level::level_enum log_start_level = spdlog::level::info;
spdlog::level::level_enum g_log_start_level;
spdlog::level::level_enum stderr_start_level = spdlog::level::info;
std::shared_ptr<spdlog::logger> stderr_logger = spdlog::stderr_logger_mt("stderr");
std::shared_ptr<spdlog::logger> file_logger;

std::map<log_level_t, spdlog::level::level_enum> to_spdlog_levels = {
{LOG_LEVEL_DEBUG, spdlog::level::debug},
{LOG_LEVEL_INFO, spdlog::level::info},
{LOG_LEVEL_WARNING, spdlog::level::warn},
{LOG_LEVEL_ERROR, spdlog::level::err},
{LOG_LEVEL_FATAL, spdlog::level::critical}};
void dsn_log_init(const std::string &log_dir,
const std::string &role_name,
const std::function<std::string()> &dsn_log_prefixed_message_func)
Expand All @@ -98,12 +112,9 @@ void dsn_log_init(const std::string &log_dir,
FLAGS_max_log_file_bytes,
FLAGS_max_number_of_log_files_on_disk);
// _symlink_path = utils::filesystem::path_combine(_log_dir, symlink_name);

// log_start_level = enum_from_string(FLAGS_logging_start_level, LOG_LEVEL_INVALID);

// CHECK_NE_MSG(
// log_start_level, LOG_LEVEL_INVALID, "invalid [core] logging_start_level specified");
file_logger->set_level(spdlog::level::info);
const auto spdlog_start_level = to_spdlog_levels[FLAGS_logging_start_level];
g_log_start_level = spdlog_start_level;
file_logger->set_level(spdlog_start_level);
file_logger->flush_on(spdlog::level::err);
if (FLAGS_fast_flush) {
spdlog::flush_every(std::chrono::seconds(1));
Expand Down Expand Up @@ -141,7 +152,3 @@ void dsn_log_init(const std::string &log_dir,
// ::fflush(stderr);
// ::fflush(stdout);
}

spdlog::level::level_enum get_log_start_level() { return log_start_level; }

void set_log_start_level(spdlog::level::level_enum level) { log_start_level = level; }
1 change: 0 additions & 1 deletion src/utils/simple_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ simple_logger::simple_logger(const char *log_dir, const char *role_name)
return "ERROR: invalid level '" + args[0] + "'";
}
}
// set_log_start_level(start_level);
return std::string("OK, current level is ") + enum_to_string(start_level);
}));
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/test/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

TEST(LoggingTest, GlobalLog)
{
std::cout << "logging start level = " << spdlog::level::to_short_c_str(get_log_start_level())
std::cout << "logging start level = " << spdlog::level::to_short_c_str(g_log_start_level)
<< std::endl;
LOG(spdlog::level::info, "in TEST(core, logging)");
}
Expand Down
24 changes: 12 additions & 12 deletions src/utils/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
#include "spdlog/common.h"
#include "spdlog/spdlog.h"

using spdlog::level::critical;
using spdlog::level::debug;
using spdlog::level::err;
using spdlog::level::info;
using spdlog::level::warn;

/// These macros are inspired by Apache Kudu
/// https://github.com/apache/kudu/blob/1.17.0/src/kudu/util/stopwatch.h.

Expand All @@ -47,7 +41,7 @@ using spdlog::level::warn;
for (dsn::timer_internal::LogTiming _l(__FILENAME__, \
__FUNCTION__, \
__LINE__, \
severity, \
spdlog::level::severity, \
prefix, \
fmt::format(__VA_ARGS__), \
-1, \
Expand All @@ -68,15 +62,21 @@ using spdlog::level::warn;

// Macro to log the time spent in the rest of the block.
#define SCOPED_LOG_TIMING(severity, ...) \
dsn::timer_internal::LogTiming VARNAME_LINENUM(_log_timing)( \
__FILENAME__, __FUNCTION__, __LINE__, severity, "", fmt::format(__VA_ARGS__), -1, true);
dsn::timer_internal::LogTiming VARNAME_LINENUM(_log_timing)(__FILENAME__, \
__FUNCTION__, \
__LINE__, \
spdlog::level::severity, \
"", \
fmt::format(__VA_ARGS__), \
-1, \
true);

// Scoped version of LOG_SLOW_EXECUTION().
#define SCOPED_LOG_SLOW_EXECUTION(severity, max_expected_millis, ...) \
dsn::timer_internal::LogTiming VARNAME_LINENUM(_log_timing)(__FILENAME__, \
__FUNCTION__, \
__LINE__, \
severity, \
spdlog::level::severity, \
"", \
fmt::format(__VA_ARGS__), \
max_expected_millis, \
Expand All @@ -87,7 +87,7 @@ using spdlog::level::warn;
dsn::timer_internal::LogTiming VARNAME_LINENUM(_log_timing)(__FILENAME__, \
__FUNCTION__, \
__LINE__, \
severity, \
spdlog::level::severity, \
prefix, \
fmt::format(__VA_ARGS__), \
max_expected_millis, \
Expand All @@ -104,7 +104,7 @@ using spdlog::level::warn;
for (dsn::timer_internal::LogTiming _l(__FILENAME__, \
__FUNCTION__, \
__LINE__, \
severity, \
spdlog::level::severity, \
"", \
fmt::format(__VA_ARGS__), \
max_expected_millis, \
Expand Down

0 comments on commit 287abe1

Please sign in to comment.