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

 更新时间:2023年03月09日 09:34:52   作者:咩~~  
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。本文就来利用cmake实现日志类,感兴趣的小伙伴可以了解一下

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)

目录结构如下

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

相关文章

  • C/C++中使用局部/全局变量初始值或默认值问题

    C/C++中使用局部/全局变量初始值或默认值问题

    这篇文章主要介绍了C/C++中使用局部/全局变量初始值或默认值问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • OpenCV实现物体的凸包检测的示例代码

    OpenCV实现物体的凸包检测的示例代码

    给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸边形,它是包含点集中所有的点。本文将利用OpenCV实现物体的凸包检测,感兴趣的可以了解一下
    2022-08-08
  • C++面向行输入之get()与getline()实例详解

    C++面向行输入之get()与getline()实例详解

    在c++里当我们输入一个字符串时习惯用cin,但是cin只能读取一段不含空格的字符串,如果我们需要读取一段包含空格的字符串时,就需要用到getline()或get(),下面这篇文章主要给大家介绍了关于C++面向行输入之get()与getline()的相关资料,需要的朋友可以参考下
    2021-10-10
  • 总结C/C++面试中可能会碰到的字符串指针题

    总结C/C++面试中可能会碰到的字符串指针题

    C/C++是最能体现程序员能力的语言之一,其功能强大,在IT行业的各个方面都有大量的应用。下面这篇文章主要介绍了总结了在C/C++面试中可能会碰到的字符串指针题,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • C语言菜鸟基础教程之自定义函数

    C语言菜鸟基础教程之自定义函数

    自定义函数: 必须直接或间接在main中调用,否则该自定义函数不会被执行。 返回值类型 函数名(参数类型 参数名,参数类型 参数名...)
    2017-10-10
  • 一文带你了解Qt中槽的使用

    一文带你了解Qt中槽的使用

    这篇文章主要为大家详细介绍了Qt中槽的使用教程,文中的示例代码讲解详细,对我们学习Qt有一定的帮助,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-12-12
  • C++中访问权限的示例详解

    C++中访问权限的示例详解

    C++通过 public、protected、private 三个关键字来控制成员变量和成员函数的访问权限(也称为可见性),下面这篇文章主要给大家介绍了关于C++中访问权限的相关资料,需要的朋友可以参考下
    2021-07-07
  • 一篇文章带你了解C语言:入门基础(2)

    一篇文章带你了解C语言:入门基础(2)

    这篇文章主要介绍了C语言入门之基础知识详解,文中有非常详细的C语言使用教程及相关基础知识,对正在学习c语言的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-08-08
  • 带你粗略了解c++的最大乘积

    带你粗略了解c++的最大乘积

    这篇文章主要为大家详细介绍了C++的最大乘积,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助
    2021-08-08
  • QT使用QFile进行文件操作

    QT使用QFile进行文件操作

    本文主要介绍了QT使用QFile进行文件操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08

最新评论