C++ cmake实现日志类的示例代码

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。本文就来利用cmake实现日志类,感兴趣的小伙伴可以了解一下

Logger.h

#pragma once #include  #include  #include  #include  #define NAME_SPACE_START(name) namespace name { #define NAME_SPACE_END } #ifndef _LOGGER_ #define _LOGGER_ NAME_SPACE_START(Log) class Logger{ public: Logger() = delete; Logger(const Logger&) = delete; Logger(std::string logFilePath = "", std::string logFileName = ""); ~Logger() = default; void LogStart(std::string context); void LogEnd(std::string context); void Debug(std::string context); void Error(std::string context); void Warning(std::string context); void Info(std::string context); bool OpenFile(std::string absolutePath); bool CloseFile(); std::stringstream GetCurrentTime(); public: static std::string m_logFilePath; static std::string m_title; private: std::ofstream _file; std::string absPath; }; NAME_SPACE_END #endif //!_LOGGER_ 

Logger.cpp

#include "logger.h" #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  std::string basePath = "C:\\";  //日志路径 std::string baseTitle = "logger.txt"; //日志文件名 NAME_SPACE_START(Log) std::string Logger::m_logFilePath = basePath; std::string Logger::m_title = baseTitle; Logger::Logger(std::string logFilePath, std::string logFileName){ std::string absolutePath = ""; if(logFilePath != ""){ absolutePath += logFilePath; } else{ absolutePath += Logger::m_logFilePath; } if(logFileName != ""){ absolutePath += logFileName; } else{ absolutePath += Logger::m_title; } this->absPath = absolutePath; } void Logger::LogStart(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<CloseFile(); } void Logger::LogEnd(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<CloseFile(); } void Logger::Debug(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<CloseFile(); } void Logger::Error(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<CloseFile(); } void Logger::Warning(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<CloseFile(); } void Logger::Info(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<CloseFile(); } bool Logger::OpenFile(std::string absolutePath){ try { this->_file.open(absolutePath, std::ios::out | std::ios::app); if(!_file.is_open()){ return false; } return true; } catch (std::exception ex) { return false; } } bool Logger::CloseFile(){ try{ this->_file.close(); return true; } catch(std::exception ex){ return false; } } std::stringstream Logger::GetCurrentTime(){ std::stringstream ss; time_t now=time(nullptr); tm curr_tm; localtime_s(&curr_tm, &now); ss<

main.cpp

#include  #include "logger.h" using namespace std; using namespace Log; int main(){ Logger log("F:/Visual-Studio-practice/vscode/mySource/"); log.LogStart("main"); log.Debug("main"); log.Warning("main"); log.Error("main"); log.Info("main"); log.LogEnd("main"); return 0; } 

日志图片

本程序使用cmake生成

cmake文件

cmake_minimum_required(VERSION 3.0.0)
project(logger CXX)
set(CMAKE_INSTALL_PREFIX "F:/Visual-Studio-practice/vscode/mySource/build")
file(GLOB SOURCE_FILE ./src/logger.cpp)
add_library(logger_static STATIC ${SOURCE_FILE})
target_include_directories(logger_static PUBLIC header)

最外层cmake

cmake_minimum_required(VERSION 3.0.0)
project(MAIN VERSION 0.1.0)
set(CMAKE_BUILD_TYPE Debug)
set(UTILS_PATH ${PROJECT_SOURCE_DIR}/utils)
add_subdirectory(utils/Log)
add_executable(MAIN main.cpp)
include_directories(${UTILS_PATH}/Log/header)
target_link_libraries(MAIN logger_static)
enable_testing()
add_test(NAME MAIN_TEST COMMAND MAIN)

目录结构如下

到此这篇关于C++ cmake实现日志类的示例代码的文章就介绍到这了,更多相关C++ cmake日志类内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C++ cmake实现日志类的示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言