add log file output

This commit is contained in:
czzhangheng 2025-07-09 16:02:28 +08:00
parent 9bae918829
commit 2d9f3effda
16 changed files with 2507 additions and 22 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

28
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

View File

@ -15,7 +15,8 @@
#include <sstream>
#include <vector>
#include <stdexcept>
#include <memory>
#include <iostream>
//辅助函数,多个参数转化字符串
@ -74,33 +75,67 @@ enum class Loglevel {
ERROR
};
class Logger {
// 日志输出抽象基类
class LogOutput {
public:
Logger(const std::string& filename) : log_file_(filename, std::ios::out | std::ios::app)
, exit_flag_(false) {
if (!log_file_.is_open()) {
virtual ~LogOutput() = default;
virtual void write(const std::string& msg) = 0;
};
// 文件输出
class FileOutput : public LogOutput {
public:
FileOutput(const std::string& filename) : file_(filename, std::ios::out | std::ios::app) {
if (!file_.is_open()) {
throw std::runtime_error("Failed to open log file");
}
}
void write(const std::string& msg) override {
file_ << msg << std::endl;
}
~FileOutput() {
if (file_.is_open()) file_.close();
}
private:
std::ofstream file_;
};
// 控制台输出
class ConsoleOutput : public LogOutput {
public:
void write(const std::string& msg) override {
std::cout << msg << std::endl;
}
};
// 网络输出(示例,实际实现需补充)
class NetworkOutput : public LogOutput {
public:
void write(const std::string& msg) override {
// 这里可以实现网络发送逻辑
// 示例std::cout << "[Network] " << msg << std::endl;
}
};
class Logger {
public:
// 通过unique_ptr传入多态输出对象
Logger(std::unique_ptr<LogOutput> output_obj)
: output_obj_(std::move(output_obj)), exit_flag_(false) {
worker_thread_ = std::thread([this]() {
std::string msg;
while (log_queue_.pop(msg)) {
log_file_ << msg << std::endl;
output_obj_->write(msg);
}
});
};
}
~Logger() {
exit_flag_ = true;
log_queue_.shutdown();
//如果日志结束了,说明主线程退出,这里是优雅退出,等待其他主线程
if (worker_thread_.joinable()) {
worker_thread_.join();
}
if (log_file_.is_open()) {
log_file_.close();
}
};
}
template<typename ... Args>
void log(Loglevel loglevel, const std::string& format, Args&& ...args) {
@ -120,11 +155,10 @@ public:
break;
}
log_queue_.push(level_str + formatMessage(format, std::forward<Args>(args)...));
};
}
private:
std::string GetTimeStamp() {
static std::string GetTimeStamp() {
std::time_t now = std::time(nullptr);
std::tm* local_time = std::localtime(&now);
char buffer[80];
@ -148,14 +182,12 @@ private:
oss << arg_strings[arg_index];
} else {
oss << "{}";
//如果占位符没有对应参数,保留占位符
}
arg_strings[arg_index++];
pos = placeholder + 2;
placeholder = format.find("{}", pos);
}
oss << format.substr(pos);
while (arg_index < arg_strings.size()) {
oss << arg_strings[arg_index++];
@ -165,7 +197,7 @@ private:
LogQueue log_queue_;
std::thread worker_thread_;
std::ofstream log_file_;
std::unique_ptr<LogOutput> output_obj_; // 多态输出对象
std::atomic<bool> exit_flag_;
};

View File

@ -0,0 +1,161 @@
{
"inputs" :
[
{
"path" : "CMakeLists.txt"
},
{
"isGenerated" : true,
"path" : "cmake-build-debug/CMakeFiles/3.30.5/CMakeSystem.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeSystemSpecificInitialize.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Darwin-Initialize.cmake"
},
{
"isGenerated" : true,
"path" : "cmake-build-debug/CMakeFiles/3.30.5/CMakeCCompiler.cmake"
},
{
"isGenerated" : true,
"path" : "cmake-build-debug/CMakeFiles/3.30.5/CMakeCXXCompiler.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeSystemSpecificInformation.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeGenericSystem.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeInitializeConfigs.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Darwin.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/UnixPaths.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeCInformation.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeLanguageInformation.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/AppleClang-C.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/Clang.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/CMakeCommonCompilerMacros.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/GNU.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/CMakeCommonCompilerMacros.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Apple-AppleClang-C.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Apple-Clang-C.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Apple-Clang.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeCommonLanguageInclude.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeCXXInformation.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeLanguageInformation.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/AppleClang-CXX.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Compiler/Clang.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Apple-AppleClang-CXX.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Apple-Clang-CXX.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/Platform/Apple-Clang.cmake"
},
{
"isCMake" : true,
"isExternal" : true,
"path" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30/Modules/CMakeCommonLanguageInclude.cmake"
}
],
"kind" : "cmakeFiles",
"paths" :
{
"build" : "/Users/czzhangheng/cppTemp/cmake-build-debug",
"source" : "/Users/czzhangheng/cppTemp"
},
"version" :
{
"major" : 1,
"minor" : 1
}
}

View File

@ -0,0 +1,60 @@
{
"configurations" :
[
{
"directories" :
[
{
"build" : ".",
"jsonFile" : "directory-.-Debug-f5ebdc15457944623624.json",
"minimumCMakeVersion" :
{
"string" : "3.30"
},
"projectIndex" : 0,
"source" : ".",
"targetIndexes" :
[
0
]
}
],
"name" : "Debug",
"projects" :
[
{
"directoryIndexes" :
[
0
],
"name" : "cppTemp",
"targetIndexes" :
[
0
]
}
],
"targets" :
[
{
"directoryIndex" : 0,
"id" : "cppTemp::@6890427a1f51a3e7e1df",
"jsonFile" : "target-cppTemp-Debug-c2ba97227afdfe5986ee.json",
"name" : "cppTemp",
"projectIndex" : 0
}
]
}
],
"kind" : "codemodel",
"paths" :
{
"build" : "/Users/czzhangheng/cppTemp/cmake-build-debug",
"source" : "/Users/czzhangheng/cppTemp"
},
"version" :
{
"major" : 2,
"minor" : 7
}
}

View File

@ -0,0 +1,108 @@
{
"cmake" :
{
"generator" :
{
"multiConfig" : false,
"name" : "Unix Makefiles"
},
"paths" :
{
"cmake" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/cmake",
"cpack" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/cpack",
"ctest" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/ctest",
"root" : "/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/share/cmake-3.30"
},
"version" :
{
"isDirty" : false,
"major" : 3,
"minor" : 30,
"patch" : 5,
"string" : "3.30.5",
"suffix" : ""
}
},
"objects" :
[
{
"jsonFile" : "codemodel-v2-f561106a0bc6c24e5958.json",
"kind" : "codemodel",
"version" :
{
"major" : 2,
"minor" : 7
}
},
{
"jsonFile" : "cache-v2-0b161b1deaddf269a855.json",
"kind" : "cache",
"version" :
{
"major" : 2,
"minor" : 0
}
},
{
"jsonFile" : "cmakeFiles-v1-71f2239649e78bf7c37e.json",
"kind" : "cmakeFiles",
"version" :
{
"major" : 1,
"minor" : 1
}
},
{
"jsonFile" : "toolchains-v1-aceb16b2b4f16c8fa500.json",
"kind" : "toolchains",
"version" :
{
"major" : 1,
"minor" : 0
}
}
],
"reply" :
{
"cache-v2" :
{
"jsonFile" : "cache-v2-0b161b1deaddf269a855.json",
"kind" : "cache",
"version" :
{
"major" : 2,
"minor" : 0
}
},
"cmakeFiles-v1" :
{
"jsonFile" : "cmakeFiles-v1-71f2239649e78bf7c37e.json",
"kind" : "cmakeFiles",
"version" :
{
"major" : 1,
"minor" : 1
}
},
"codemodel-v2" :
{
"jsonFile" : "codemodel-v2-f561106a0bc6c24e5958.json",
"kind" : "codemodel",
"version" :
{
"major" : 2,
"minor" : 7
}
},
"toolchains-v1" :
{
"jsonFile" : "toolchains-v1-aceb16b2b4f16c8fa500.json",
"kind" : "toolchains",
"version" :
{
"major" : 1,
"minor" : 0
}
}
}
}

View File

@ -0,0 +1,119 @@
{
"artifacts" :
[
{
"path" : "cppTemp"
}
],
"backtrace" : 1,
"backtraceGraph" :
{
"commands" :
[
"add_executable"
],
"files" :
[
"CMakeLists.txt"
],
"nodes" :
[
{
"file" : 0
},
{
"command" : 0,
"file" : 0,
"line" : 6,
"parent" : 0
}
]
},
"compileGroups" :
[
{
"compileCommandFragments" :
[
{
"fragment" : "-g -std=gnu++20 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.5.sdk -fcolor-diagnostics"
}
],
"language" : "CXX",
"languageStandard" :
{
"backtraces" :
[
1
],
"standard" : "20"
},
"sourceIndexes" :
[
0,
1
]
}
],
"id" : "cppTemp::@6890427a1f51a3e7e1df",
"link" :
{
"commandFragments" :
[
{
"fragment" : "-g",
"role" : "flags"
},
{
"fragment" : "",
"role" : "flags"
}
],
"language" : "CXX"
},
"name" : "cppTemp",
"nameOnDisk" : "cppTemp",
"paths" :
{
"build" : ".",
"source" : "."
},
"sourceGroups" :
[
{
"name" : "Source Files",
"sourceIndexes" :
[
0,
1
]
},
{
"name" : "Header Files",
"sourceIndexes" :
[
2
]
}
],
"sources" :
[
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "main.cpp",
"sourceGroupIndex" : 0
},
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "Logger.cpp",
"sourceGroupIndex" : 0
},
{
"backtrace" : 1,
"path" : "Logger.h",
"sourceGroupIndex" : 1
}
],
"type" : "EXECUTABLE"
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,3 @@
Start testing: Jul 09 14:56 CST
Start testing: Jul 09 16:01 CST
----------------------------------------------------------
End testing: Jul 09 14:56 CST
End testing: Jul 09 16:01 CST

Binary file not shown.

View File

@ -0,0 +1,9 @@
[INFO] 2025-07-09 14:56:25 User 42 performed login in 2.333 seconds
[DEBUG] 2025-07-09 14:56:25 Hello World
[WARNING] 2025-07-09 14:56:25 This is a log message.
[ERROR] 2025-07-09 14:56:25 Multiple placeholders : 1, 2, 3
[INFO] 2025-07-09 15:08:38 User 42 performed login in 2.333 seconds
[DEBUG] 2025-07-09 15:08:38 Hello World
[WARNING] 2025-07-09 15:08:38 This is a log message.
[ERROR] 2025-07-09 15:08:38 Multiple placeholders : 1, 2, 3

BIN
main Executable file

Binary file not shown.

View File

@ -4,7 +4,8 @@
int main() {
try {
Logger logger("log.txt");
Logger logger(std::make_unique<ConsoleOutput>());
//Logger logger(std::make_unique<FileOutput>("log.txt"));
int id = 42;
std::string action = "login";
double duration = 2.333;

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.main</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

Binary file not shown.

View File

@ -0,0 +1,5 @@
---
triple: 'arm64-apple-darwin'
binary-path: '/Users/czzhangheng/cppTemp/main'
relocations: []
...