C++ cmake實(shí)現(xiàn)日志類的示例代碼
Logger.h
#pragma once #include <fstream> #include <sstream> #include <iostream> #include <string> #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 <ctime> #include <exception> #include <fstream> #include <ios> #include <iterator> #include <ostream> #include <sstream> #include <streambuf> #include <string> #include <time.h> 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<<ss.str() <<"------------------------------Log Start" <<" "<<context<<" " <<"------------------------------"<<std::endl; this->CloseFile(); } void Logger::LogEnd(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<<ss.str() <<"------------------------------Log End" <<" "<<context<<" " <<"------------------------------"<<std::endl; this->CloseFile(); } void Logger::Debug(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<<ss.str() <<"[Log Debug]:" <<context<<std::endl; this->CloseFile(); } void Logger::Error(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<<ss.str() <<"[Log Error]:" <<context<<std::endl; this->CloseFile(); } void Logger::Warning(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<<ss.str() <<"[Log Warning]:" <<context<<std::endl; this->CloseFile(); } void Logger::Info(std::string context = ""){ if(!this->OpenFile(this->absPath)) return; std::stringstream ss=GetCurrentTime(); _file<<ss.str() <<"[Log Info]:" <<context<<std::endl; this->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<<curr_tm.tm_year<<"-"<<curr_tm.tm_mon<<"-"<<curr_tm.tm_yday <<" "<<curr_tm.tm_hour<<":"<<curr_tm.tm_min<<":"<<curr_tm.tm_sec <<" "; return ss; } NAME_SPACE_END
main.cpp
#include <iostream> #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)
目錄結(jié)構(gòu)如下
到此這篇關(guān)于C++ cmake實(shí)現(xiàn)日志類的示例代碼的文章就介紹到這了,更多相關(guān)C++ cmake日志類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++類與對(duì)象深入之運(yùn)算符重載與const及初始化列表詳解
運(yùn)算符是程序中最最常見的操作,例如對(duì)于內(nèi)置類型的賦值我們直接使用=賦值即可,因?yàn)檫@些編譯器已經(jīng)幫我們做好了,但是對(duì)象的賦值呢?能直接賦值嗎2022-06-06聊聊Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題
這篇文章主要介紹了Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題,給大家介紹了圖像的克隆及拷貝問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Qt數(shù)據(jù)庫應(yīng)用之超級(jí)自定義委托
Qt中需要用到自定義委托的情形很多,比如提供下拉框選擇,進(jìn)度條展示下載進(jìn)度啥的,默認(rèn)的單元格是沒有這些效果的,需要自己?jiǎn)为?dú)用委托的形式來展示。本文將為大家介紹Qt中如何進(jìn)行超級(jí)自定義委托,需要的可以參考一下2022-03-03VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼
這篇文章主要介紹了VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼,比較實(shí)用的功能,需要的朋友可以參考下2014-07-07