Skip to content

Commit

Permalink
修改 CMakeList,引入cpp-terminalspdlog。编写 spdlog 设置文件 Log.h ,还需改进。
Browse files Browse the repository at this point in the history
后续考虑编写详细的构建文档。
  • Loading branch information
Mq-b committed Oct 28, 2024
1 parent c44abef commit 0c78a47
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
18 changes: 17 additions & 1 deletion code/ModernCpp-ConcurrentProgramming-Tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "C
add_compile_options("-finput-charset=UTF-8" "-fexec-charset=UTF-8" "-fopenmp")
endif()

add_executable(${PROJECT_NAME} "45原子特化shared_ptr.cpp")
add_executable(${PROJECT_NAME} "test2.cpp")

set(SFML_DIR "D:/lib/SFML-2.6.1-windows-vc17-64-bit/SFML-2.6.1/lib/cmake/SFML")
find_package(SFML 2.6.1 COMPONENTS system window graphics audio network REQUIRED)
Expand All @@ -30,3 +30,19 @@ set(Boost_INCLUDE_DIR "D:/vcpkg-master/installed/x64-windows/include")
include_directories(${Boost_INCLUDE_DIR})
find_package(Boost REQUIRED COMPONENTS system)
target_link_libraries(${PROJECT_NAME} PRIVATE Boost::system)

target_include_directories(${PROJECT_NAME} PRIVATE "D:/project/cpp-terminal/include")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${PROJECT_NAME} PRIVATE
"D:/project/cpp-terminal/lib/cpp-terminal-private.lib"
"D:/project/cpp-terminal/lib/cpp-terminal.lib"
)
else()
target_link_libraries(${PROJECT_NAME} PRIVATE
"D:/project/cpp-terminal/lib/private/debug/cpp-terminal-private.lib"
"D:/project/cpp-terminal/lib/debug/cpp-terminal.lib"
)
endif()

find_package(spdlog REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog::spdlog_header_only)
29 changes: 29 additions & 0 deletions code/ModernCpp-ConcurrentProgramming-Tutorial/Log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>

inline void setupLogging() {
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs.txt");
file_sink->set_level(spdlog::level::debug);
file_sink->set_pattern("[%Y-%m-%d %H:%M:%S] [%@] [%!] [thread %t] [%oms] [%l] %v");

auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::debug);
console_sink->set_pattern("%^[%Y-%m-%d %H:%M:%S] [thread %t] [%oms] [%l] %v%$");

auto logger = std::make_shared<spdlog::logger>("multi_sink", spdlog::sinks_init_list{ file_sink, console_sink });
spdlog::register_logger(logger);

spdlog::set_default_logger(logger);
}

// spdlog 要想输出文件、路径、函数、行号,只能借助此宏,才会显示。
// 其实使用 C++20 std::source_location 也能获取这些信息,后面再考虑单独封装吧,目前这样做导致没办法做格式字符串。

#define LOG_INFO(msg, ...) SPDLOG_LOGGER_INFO(spdlog::get("multi_sink"), msg)
#define LOG_WARN(msg, ...) SPDLOG_LOGGER_WARN(spdlog::get("multi_sink"), msg)
#define LOG_ERROR(msg, ...) SPDLOG_LOGGER_ERROR(spdlog::get("multi_sink"), msg)

const auto init_log = (setupLogging(), 0);
13 changes: 13 additions & 0 deletions code/ModernCpp-ConcurrentProgramming-Tutorial/test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Log.h"
#include <thread>
#include <format>
using namespace std::chrono_literals;

int main() {
LOG_WARN("😅");
std::jthread t{[]{
std::this_thread::sleep_for(100ms);
LOG_ERROR("🤣");
}};
LOG_INFO("👉");
}

0 comments on commit 0c78a47

Please sign in to comment.