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 3b73cc6 commit d3bda41
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 113 deletions.
17 changes: 9 additions & 8 deletions src/utils/fmt_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,47 @@
#include "spdlog/common.h"
#include "spdlog/spdlog.h"

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) \
spdlog::log(level, __VA_ARGS__); \
file_logger->log(level, __VA_ARGS__); \
} while (false)

extern std::shared_ptr<spdlog::logger> stderr_logger;
extern std::shared_ptr<spdlog::logger> file_logger;
extern spdlog::level::level_enum stderr_start_level;
#define LOG_DEBUG(...) \
do { \
SPDLOG_DEBUG(__VA_ARGS__); \
SPDLOG_LOGGER_DEBUG(file_logger, __VA_ARGS__); \
if (spdlog::level::debug >= stderr_start_level) { \
SPDLOG_LOGGER_DEBUG(stderr_logger, __VA_ARGS__); \
} \
} while (false)
#define LOG_INFO(...) \
do { \
SPDLOG_INFO(__VA_ARGS__); \
SPDLOG_LOGGER_INFO(file_logger, __VA_ARGS__); \
if (spdlog::level::info >= stderr_start_level) { \
SPDLOG_LOGGER_INFO(stderr_logger, __VA_ARGS__); \
} \
} while (false)
#define LOG_WARNING(...) \
do { \
SPDLOG_WARN(__VA_ARGS__); \
SPDLOG_LOGGER_WARN(file_logger, __VA_ARGS__); \
if (spdlog::level::warn >= stderr_start_level) { \
SPDLOG_LOGGER_WARN(stderr_logger, __VA_ARGS__); \
} \
} while (false)
#define LOG_ERROR(...) \
do { \
SPDLOG_ERROR(__VA_ARGS__); \
SPDLOG_LOGGER_ERROR(file_logger, __VA_ARGS__); \
if (spdlog::level::err >= stderr_start_level) { \
SPDLOG_LOGGER_ERROR(stderr_logger, __VA_ARGS__); \
} \
} while (false)
#define LOG_FATAL(...) \
do { \
SPDLOG_CRITICAL(__VA_ARGS__); \
SPDLOG_LOGGER_CRITICAL(file_logger, __VA_ARGS__); \
if (spdlog::level::critical >= stderr_start_level) { \
SPDLOG_LOGGER_CRITICAL(stderr_logger, __VA_ARGS__); \
} \
Expand Down
57 changes: 20 additions & 37 deletions src/utils/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ DSN_DEFINE_validator(max_number_of_log_files_on_disk,
[](int32_t value) -> bool { return value > 0; });

DSN_DEFINE_string(tools.simple_logger, base_name, "pegasus", "The default base name for log file");
DSN_DEFINE_string(
tools.simple_logger,
stderr_start_level,
"LOG_LEVEL_WARNING",
"The lowest level of log messages to be copied to stderr in addition to log files");
DSN_DEFINE_validator(stderr_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;
});

spdlog::level::level_enum log_start_level = spdlog::level::info;

namespace dsn {
using namespace tools;
} // namespace dsn

static void log_on_sys_exit(::dsn::sys_exit_type)
{
dsn::logging_provider *logger = dsn::logging_provider::instance();
logger->flush();
}
std::shared_ptr<spdlog::logger> file_logger;
void dsn_log_init(const std::string &log_dir,
const std::string &role_name,
Expand All @@ -103,24 +107,17 @@ void dsn_log_init(const std::string &log_dir,

// CHECK_NE_MSG(
// log_start_level, LOG_LEVEL_INVALID, "invalid [core] logging_start_level specified");
spdlog::set_level(spdlog::level::info);
spdlog::flush_on(spdlog::level::err);
file_logger->set_level(spdlog::level::info);
file_logger->flush_on(spdlog::level::err);
if (FLAGS_fast_flush) {
spdlog::flush_every(std::chrono::seconds(1));
}

// register log flush on exit
if (FLAGS_logging_flush_on_exit) {
::dsn::tools::sys_exit.put_back(log_on_sys_exit, "log.flush");
// ::dsn::tools::sys_exit.put_back(log_on_sys_exit, "log.flush");
}

// // The leading character of each log line, corresponding to the log level
// // D: Debug
// // I: Info
// // W: Warning
// // E: Error
// // F: Fatal
// static char s_level_char[] = "DIWEF";
//
// uint64_t ts = dsn_now_ns();
// std::string time_str;
Expand All @@ -130,11 +127,17 @@ void dsn_log_init(const std::string &log_dir,
// const auto header = fmt::format(
// "{}{} ({} {}) {}", s_level_char[log_level], time_str, ts, tid,
// log_prefixed_message_func());
// The leading character of each log line, corresponding to the log level
// D: Debug
// I: Info
// W: Warning
// E: Error
// C: Critical
if (FLAGS_short_header) {
spdlog::set_pattern("[:%Y-%m-%d %H:%M:%S %z] [%^%L%$] [thread %t] %v");
file_logger->set_pattern("[%^%L%$%Y-%m-%d %H:%M:%S %z] [thread %t] %v");
} else {
// const auto long_header = fmt::format("{}:{}:{}(): ", file, line, function);
spdlog::set_pattern("[:%Y-%m-%d %H:%M:%S %z] [%^%L%$] [thread %t] %v");
file_logger->set_pattern("[%^%L%$%Y-%m-%d %H:%M:%S %z] [thread %t] %v");
}

// Flush when exit
Expand All @@ -146,23 +149,3 @@ void dsn_log_init(const std::string &log_dir,
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; }

namespace dsn {

std::unique_ptr<logging_provider> logging_provider::_logger;

logging_provider *logging_provider::instance() { return _logger ? _logger.get() : nullptr; }

void logging_provider::set_logger(logging_provider *logger) { _logger.reset(logger); }

namespace tools {
namespace internal_use_only {
bool register_component_provider(const char *name,
logging_provider::factory f,
::dsn::provider_type type)
{
return dsn::utils::factory_store<logging_provider>::register_factory(name, f, type);
}
} // namespace internal_use_only
} // namespace tools
} // namespace dsn
48 changes: 0 additions & 48 deletions src/utils/logging_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,6 @@
#include "utils/command_manager.h"
#include "utils/factory_store.h"

namespace dsn {

/*!
@addtogroup tool-api-providers
@{
*/
class logging_provider
{
public:
template <typename T>
static logging_provider *create(const char *log_dir, const char *role_name)
{
return new T(log_dir, role_name);
}

typedef logging_provider *(*factory)(const char *, const char *);

public:
virtual ~logging_provider() = default;

// singleton
static logging_provider *instance();

// not thread-safe
static void set_logger(logging_provider *logger);

virtual void flush() = 0;

void deregister_commands() { _cmds.clear(); }

protected:
static std::unique_ptr<logging_provider> _logger;

logging_provider(log_level_t stderr_start_level) : _stderr_start_level(stderr_start_level) {}

std::vector<std::unique_ptr<command_deregister>> _cmds;
const log_level_t _stderr_start_level;
};

namespace tools {
namespace internal_use_only {
bool register_component_provider(const char *name,
logging_provider::factory f,
::dsn::provider_type type);
} // namespace internal_use_only
} // namespace tools
} // namespace dsn

extern void dsn_log_init(const std::string &log_dir,
const std::string &role_name,
const std::function<std::string()> &dsn_log_prefixed_message_func);
15 changes: 2 additions & 13 deletions src/utils/simple_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,12 @@
#include "utils/strings.h"
#include "utils/time_utils.h"

DSN_DEFINE_string(
tools.simple_logger,
stderr_start_level,
"LOG_LEVEL_WARNING",
"The lowest level of log messages to be copied to stderr in addition to log files");
DSN_DEFINE_validator(stderr_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_DECLARE_string(logging_start_level);

namespace dsn {
namespace tools {

simple_logger::simple_logger(const char *log_dir, const char *role_name)
: logging_provider(enum_from_string(FLAGS_stderr_start_level, LOG_LEVEL_INVALID))
{
// TODO(yingchun): simple_logger is destroyed after command_manager, so will cause crash like
// "assertion expression: [_handlers.empty()] All commands must be deregistered before
Expand All @@ -82,8 +71,8 @@ simple_logger::simple_logger(const char *log_dir, const char *role_name)
"flush-log",
"Flush log to stderr or file",
"",
[this](const std::vector<std::string> &args) {
this->flush();
[](const std::vector<std::string> &args) {
// this->flush();
return "Flush done.";
}));

Expand Down
4 changes: 3 additions & 1 deletion src/utils/simple_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ namespace tools {
* simple_logger provides a logger which writes to file.
* The max number of lines in a logger file is 200000.
*/
class simple_logger : public logging_provider
class simple_logger
{
public:
// 'log_dir' is the directory to store log files, 'role_name' is the name of the process,
// such as 'replica_server', 'meta_server' in Pegasus.
simple_logger(const char *log_dir, const char *role_name);

std::vector<std::unique_ptr<command_deregister>> _cmds;
};
} // namespace tools
} // namespace dsn
6 changes: 0 additions & 6 deletions src/utils/test/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ namespace dsn {
namespace tools {
class logger_test : public testing::Test
{
public:
void SetUp() override
{
// Deregister commands to avoid re-register error.
dsn::logging_provider::instance()->deregister_commands();
}
};

class simple_logger_test : public logger_test
Expand Down

0 comments on commit d3bda41

Please sign in to comment.