欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ cmake實(shí)現(xiàn)日志類的示例代碼

 更新時(shí)間:2023年03月09日 09:34:52   作者:咩~~  
CMake是一個(gè)跨平臺(tái)的安裝(編譯)工具,可以用簡(jiǎn)單的語句來描述所有平臺(tái)的安裝(編譯過程)。本文就來利用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及初始化列表詳解

    C++類與對(duì)象深入之運(yùn)算符重載與const及初始化列表詳解

    運(yùn)算符是程序中最最常見的操作,例如對(duì)于內(nèi)置類型的賦值我們直接使用=賦值即可,因?yàn)檫@些編譯器已經(jīng)幫我們做好了,但是對(duì)象的賦值呢?能直接賦值嗎
    2022-06-06
  • C++示例詳解Prim算法與優(yōu)先隊(duì)列

    C++示例詳解Prim算法與優(yōu)先隊(duì)列

    這篇文章介紹了C++ Prim算法、優(yōu)先隊(duì)列,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C/C++程序編譯流程詳解

    C/C++程序編譯流程詳解

    C/C++程序編譯過程包括下面4個(gè)階段:1.預(yù)處理,2.編譯,3.匯編,4.鏈接。下面我們就來詳細(xì)分析下這幾個(gè)階段。
    2016-04-04
  • C++判斷矩形相交的方法

    C++判斷矩形相交的方法

    這篇文章主要介紹了C++判斷矩形相交的方法,涉及C++針對(duì)平面坐標(biāo)數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • 華為面試題數(shù)字大小寫轉(zhuǎn)換

    華為面試題數(shù)字大小寫轉(zhuǎn)換

    一個(gè)四位數(shù),如1024,1004,打印出他們的中文形式,如果一千零二十四,一千零四,大家參考使用吧
    2013-12-12
  • 聊聊Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題

    聊聊Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題

    這篇文章主要介紹了Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題,給大家介紹了圖像的克隆及拷貝問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Qt數(shù)據(jù)庫應(yīng)用之超級(jí)自定義委托

    Qt數(shù)據(jù)庫應(yīng)用之超級(jí)自定義委托

    Qt中需要用到自定義委托的情形很多,比如提供下拉框選擇,進(jìn)度條展示下載進(jìn)度啥的,默認(rèn)的單元格是沒有這些效果的,需要自己?jiǎn)为?dú)用委托的形式來展示。本文將為大家介紹Qt中如何進(jìn)行超級(jí)自定義委托,需要的可以參考一下
    2022-03-03
  • VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼

    VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼

    這篇文章主要介紹了VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼,比較實(shí)用的功能,需要的朋友可以參考下
    2014-07-07
  • C++實(shí)現(xiàn)批量圖片拼接

    C++實(shí)現(xiàn)批量圖片拼接

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)批量圖片拼接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • c++核心編程之函數(shù)的重載

    c++核心編程之函數(shù)的重載

    這篇文章主要介紹了c++核心編程之函數(shù)的重載,函數(shù)可以重復(fù)使用,提高了復(fù)用性,但前提是必須在一個(gè)作用域并且函數(shù)名稱相同,下面附代碼詳細(xì)介紹,需要的小伙伴可以參考一下
    2022-03-03

最新評(píng)論