溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用C++?cmake實(shí)現(xiàn)日志類

發(fā)布時(shí)間:2023-03-09 10:37:50 來(lái)源:億速云 閱讀:157 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“怎么使用C++ cmake實(shí)現(xiàn)日志類”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“怎么使用C++ cmake實(shí)現(xiàn)日志類”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

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;
}

日志圖片

怎么使用C++?cmake實(shí)現(xiàn)日志類

本程序使用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)如下

怎么使用C++?cmake實(shí)現(xiàn)日志類

讀到這里,這篇“怎么使用C++ cmake實(shí)現(xiàn)日志類”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI