diff --git a/src/utils/fmt_logging.h b/src/utils/fmt_logging.h index 0059d3e44e..65ddd502f9 100644 --- a/src/utils/fmt_logging.h +++ b/src/utils/fmt_logging.h @@ -33,41 +33,41 @@ extern std::shared_ptr file_logger; #define LOG(level, ...) \ do { \ if (level >= log_start_level) \ - file_logger->log(level, __VA_ARGS__); \ + file_logger->log(level, __VA_ARGS__); \ } while (false) extern spdlog::level::level_enum stderr_start_level; #define LOG_DEBUG(...) \ do { \ - SPDLOG_LOGGER_DEBUG(file_logger, __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_LOGGER_INFO(file_logger, __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_LOGGER_WARN(file_logger, __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_LOGGER_ERROR(file_logger, __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_LOGGER_CRITICAL(file_logger, __VA_ARGS__); \ + SPDLOG_LOGGER_CRITICAL(file_logger, __VA_ARGS__); \ if (spdlog::level::critical >= stderr_start_level) { \ SPDLOG_LOGGER_CRITICAL(stderr_logger, __VA_ARGS__); \ } \ diff --git a/src/utils/logging.cpp b/src/utils/logging.cpp index a69db4de98..896ce2566b 100644 --- a/src/utils/logging.cpp +++ b/src/utils/logging.cpp @@ -76,10 +76,10 @@ DSN_DEFINE_validator(max_number_of_log_files_on_disk, 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"); + 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; @@ -115,7 +115,7 @@ void dsn_log_init(const std::string &log_dir, // 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"); } // diff --git a/src/utils/simple_logger.cpp b/src/utils/simple_logger.cpp index a66cc1859d..4453148289 100644 --- a/src/utils/simple_logger.cpp +++ b/src/utils/simple_logger.cpp @@ -68,11 +68,8 @@ simple_logger::simple_logger(const char *log_dir, const char *role_name) // command_manager is destroyed, however 'flush-log' is still registered". // We need to fix it. _cmds.emplace_back(::dsn::command_manager::instance().register_single_command( - "flush-log", - "Flush log to stderr or file", - "", - [](const std::vector &args) { -// this->flush(); + "flush-log", "Flush log to stderr or file", "", [](const std::vector &args) { + // this->flush(); return "Flush done."; })); diff --git a/src/utils/test/logger.cpp b/src/utils/test/logger.cpp deleted file mode 100644 index e4caa4ebba..0000000000 --- a/src/utils/test/logger.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2015 Microsoft Corporation - * - * -=- Robust Distributed System Nucleus (rDSN) -=- - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "gtest/gtest.h" -#include "utils/api_utilities.h" -#include "utils/filesystem.h" -#include "utils/flags.h" -#include "utils/logging_provider.h" -#include "utils/simple_logger.h" -#include "utils/test_macros.h" - -DSN_DECLARE_uint64(max_number_of_log_files_on_disk); - -namespace dsn { -namespace tools { -class logger_test : public testing::Test -{ -}; - -class simple_logger_test : public logger_test -{ -public: - void SetUp() override - { - logger_test::SetUp(); - - std::string cwd; - ASSERT_TRUE(dsn::utils::filesystem::get_current_directory(cwd)); - // NOTE: Don't name the dir with "test", otherwise the whole utils test dir would be - // removed. - test_dir = dsn::utils::filesystem::path_combine(cwd, "simple_logger_test"); - - NO_FATALS(prepare_test_dir()); - std::set files; - NO_FATALS(get_log_files(files)); - NO_FATALS(clear_files(files)); - } - - void get_log_files(std::set &file_names) - { - std::vector sub_list; - ASSERT_TRUE(utils::filesystem::get_subfiles(test_dir, sub_list, false)); - - file_names.clear(); - std::regex pattern(R"(SimpleLogger\.log\.[0-9]{8}_[0-9]{6}_[0-9]{3})"); - for (const auto &path : sub_list) { - std::string name(utils::filesystem::get_file_name(path)); - if (std::regex_match(name, pattern)) { - ASSERT_TRUE(file_names.insert(name).second); - } - } - } - - void compare_log_files(const std::set &before_files, - const std::set &after_files) - { - ASSERT_FALSE(after_files.empty()); - - // One new log file is created. - if (after_files.size() == before_files.size() + 1) { - // All the file names are the same. - for (auto it1 = before_files.begin(), it2 = after_files.begin(); - it1 != before_files.end(); - ++it1, ++it2) { - ASSERT_EQ(*it1, *it2); - } - // The number of log files is the same, but they have rolled. - } else if (after_files.size() == before_files.size()) { - auto it1 = before_files.begin(); - auto it2 = after_files.begin(); - // The first file is different, the one in 'before_files' is older. - ASSERT_NE(*it1, *it2); - - // The rest of the files are the same. - for (++it1; it1 != before_files.end(); ++it1, ++it2) { - ASSERT_EQ(*it1, *it2); - } - } else { - ASSERT_TRUE(false) << "Invalid number of log files, before=" << before_files.size() - << ", after=" << after_files.size(); - } - } - - void clear_files(const std::set &file_names) - { - for (const auto &file_name : file_names) { - ASSERT_TRUE(dsn::utils::filesystem::remove_path(file_name)); - } - } - - void prepare_test_dir() - { - ASSERT_TRUE(dsn::utils::filesystem::create_directory(test_dir)) << test_dir; - } - - void remove_test_dir() - { - ASSERT_TRUE(dsn::utils::filesystem::remove_path(test_dir)) << test_dir; - } - -public: - std::string test_dir; -}; - -TEST_F(simple_logger_test, redundant_log_test) -{ - // Create redundant log files to test if their number could be restricted. - for (unsigned int i = 0; i < FLAGS_max_number_of_log_files_on_disk + 10; ++i) { - std::set before_files; - NO_FATALS(get_log_files(before_files)); - - for (unsigned int i = 0; i != 1000; ++i) { - LOG(spdlog::level::debug, "{}", "test_print"); - } - // logger->flush(); - - std::set after_files; - NO_FATALS(get_log_files(after_files)); - NO_FATALS(compare_log_files(before_files, after_files)); - ::usleep(2000); - } - - std::set files; - NO_FATALS(get_log_files(files)); - ASSERT_FALSE(files.empty()); - ASSERT_EQ(FLAGS_max_number_of_log_files_on_disk, files.size()); -} - -} // namespace tools -} // namespace dsn