From d58f1b07c5f72f5bf6fdfb86b2d01860fafc4438 Mon Sep 17 00:00:00 2001 From: home Date: Sun, 5 May 2024 19:20:42 +0800 Subject: [PATCH] =?UTF-8?q?spdlog=E5=90=8C=E6=AD=A5=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=8C=E4=BD=86=E6=98=AF=E8=BF=98=E6=98=AF?= =?UTF-8?q?=E6=9C=89=E7=82=B9=E9=97=AE=E9=A2=98=EF=BC=8C=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=AF=94=E5=90=8C=E6=AD=A5=E6=85=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 -- TODO | 2 +- logs/basic.txt | 8 +++++ main.cpp | 2 +- test/AsyncLog.cpp | 81 ++++++++++++++++++++++++++++----------------- test/CMakeLists.txt | 4 +-- vcpkg.json | 2 +- 7 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 logs/basic.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 744b3f1..7c67475 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,8 +8,6 @@ file(GLOB FILES ) message("test: ${FILES}") add_executable(ZRTC main.cpp ${FILES}) -find_package(glog CONFIG REQUIRED) - target_link_libraries(ZRTC PRIVATE glog::glog) add_subdirectory(test) diff --git a/TODO b/TODO index da9d02d..a3ae6c6 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,3 @@ -1.异步日志 读写测试(使用gtest) +1.异步日志 读写测试(使用spdlog) 2.编写单元测试 3.cicd \ No newline at end of file diff --git a/logs/basic.txt b/logs/basic.txt new file mode 100644 index 0000000..9c70a8c --- /dev/null +++ b/logs/basic.txt @@ -0,0 +1,8 @@ +[2024-05-05 17:15:07.428] [sbasic_logger] [info] Hello world +[2024-05-05 17:21:01.574] [sbasic_logger] [info] Hello world +[2024-05-05 17:22:24.249] [sbasic_logger] [info] Hello world +[2024-05-05 17:22:52.744] [sbasic_logger] [info] Hello world +[2024-05-05 17:26:44.928] [sbasic_logger] [info] Hello world +[2024-05-05 17:27:45.337] [sbasic_logger] [info] Hello world +[2024-05-05 17:28:27.521] [sbasic_logger] [info] Hello world +[2024-05-05 17:30:23.957] [sbasic_logger] [info] Hello world diff --git a/main.cpp b/main.cpp index 425374c..88ba080 100755 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,7 @@ // #include -#include +// #include int main(int argc, char** argv) { // printf("asd"); // google::InitGoogleLogging(argv[0]); diff --git a/test/AsyncLog.cpp b/test/AsyncLog.cpp index 09f37de..c13cc14 100644 --- a/test/AsyncLog.cpp +++ b/test/AsyncLog.cpp @@ -1,10 +1,14 @@ -#include #include #include #include #include +#include "spdlog/async.h" +#include "spdlog/sinks/basic_file_sink.h" +#include "spdlog/sinks/rotating_file_sink.h" +#include "spdlog/spdlog.h" + TEST(HelloWorldTest, BasicTest) { // Test code goes here // You can use ASSERT_* or EXPECT_* macros to perform assertions @@ -13,35 +17,52 @@ TEST(HelloWorldTest, BasicTest) { // 创建要写入的文件夹并指定要写入的日志文件名称 TEST(WriteFile, BasicTest) { - std::string log_path = "../../log"; - FLAGS_log_dir = log_path; - printf("test\n"); - google::SetLogSymlink(google::GLOG_INFO, "Info"); - google::SetLogSymlink(google::GLOG_WARNING, "Warning"); - google::SetLogSymlink(google::GLOG_ERROR, "Error"); - google::InitGoogleLogging("WriteLog"); - if (!std::filesystem::exists(log_path)) { - if (std::filesystem::create_directories(log_path)) { - LOG(INFO) << "创建成功"; - } - } else { - LOG(WARNING) << "文件夹" + log_path + "已经存在,不用创建"; + spdlog::info("Test: {}", 1); + + try { + // 在logs/basic.txt中写下Hello world + auto my_logger = + spdlog::basic_logger_mt("sbasic_logger", "./logs/basic.txt"); + my_logger->info("Hello {}", "world"); + } catch (const spdlog::spdlog_ex& ex) { + std::cout << "Log initialization failed: " << ex.what() << std::endl; } - LOG(INFO) << "file"; - // Most flags work immediately after updating values. - FLAGS_logtostderr = 1; - LOG(INFO) << "stderr"; - FLAGS_logtostderr = 0; - // This won’t change the log destination. If you want to set this - // value, you should do this before google::InitGoogleLogging . - // FLAGS_log_dir = "/some/log/directory"; - LOG(INFO) << "the same file"; - - for (int i = 0; i < 100; i++) { - if (i % 2 == 0) { - LOG(INFO) << "Test Write INFO" << i; - } else { - LOG(ERROR) << "Test Write ERROR" << i; - } +} + +// 异步写入文件 +TEST(AsyncWrite, BasicTest) { + auto start = std::chrono::system_clock::now(); + spdlog::init_thread_pool(10000, 10); + auto file_logger = spdlog::rotating_logger_mt( + "Async", "./logs/AsyncLogs", 1024 * 1024 * 5, 100); + int i = 0; + file_logger->set_level(spdlog::level::debug); + while (i < 1000000) { + file_logger->debug("Async message #{}", i); + i++; } + auto end = std::chrono::system_clock::now(); + auto diff = end - start; + file_logger->info("Async Duration: {}", diff.count()); + spdlog::drop_all(); +} + +// 同步写入文件 +TEST(SyncWrite, BasicTest) { + auto start = std::chrono::system_clock::now(); + + auto file_logger = + spdlog::rotating_logger_mt("Sync", "SyncLogs", 1024 * 1024 * 5, 100); + file_logger->set_level(spdlog::level::debug); + int i = 0; + while (i < 1000000) { + file_logger->debug("Async message #{}", i); + i++; + } + + auto end = std::chrono::system_clock::now(); + auto diff = end - start; + file_logger->info("Sync Duration: {}", diff.count()); + // spdlog::info("Sync Duration: {}", diff.count()); + spdlog::drop_all(); } \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c3e87c8..7bb2685 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ add_executable(AsyncLogTest AsyncLog.cpp) -find_package(glog CONFIG REQUIRED) -target_link_libraries(AsyncLogTest PRIVATE glog::glog) +find_package(spdlog CONFIG REQUIRED) +target_link_libraries(AsyncLogTest PRIVATE spdlog::spdlog_header_only) enable_testing() find_package(GTest CONFIG REQUIRED) diff --git a/vcpkg.json b/vcpkg.json index ef61f51..c39dea6 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -3,6 +3,6 @@ "version": "0.15.2", "dependencies": [ "gtest", - "glog" + "spdlog" ] } \ No newline at end of file