iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++cmake实现日志类的示例代码
  • 126
分享到

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

C++cmake实现日志类C++cmake日志类C++日志类 2023-03-09 17:03:50 126人浏览 薄情痞子
摘要

Logger.h #pragma once #include <fstream> #include <sstream> #include <iOStr

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日志类内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++cmake实现日志类的示例代码

本文链接: https://www.lsjlt.com/news/199112.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c语言怎么计算字符串长度
    在C语言中,可以使用内置的strlen()函数来计算字符串的长度。该函数位于string.h头文件中。以下是一个示例代码: #inc...
    99+
    2024-05-14
    c语言
  • c语言怎么判断三个数的大小
    可以通过比较三个数之间的大小关系来判断它们的大小。以下是一个示例代码: #include <stdio.h> int ...
    99+
    2024-05-14
    c语言
  • c++排序函数sort怎么使用
    在C++中,使用sort函数对容器中的元素进行排序,可以按照默认的升序排列,也可以自定义排序规则。 下面是一个简单的例子,对一个ve...
    99+
    2024-05-14
    c++
  • c++中string的compare函数怎么使用
    在C++中,可以使用string的compare函数来比较两个string对象的大小。该函数的原型如下: int compare(c...
    99+
    2024-05-14
    c++
  • c++中string的compare函数有什么用
    c++中string的compare函数用于比较两个字符串的大小关系。该函数返回一个整数值,表示两个字符串的比较结果。 具体来说,如...
    99+
    2024-05-14
    c++
  • c++中print函数的功能有哪些
    在C++中,通常使用std::cout来进行输出操作,而不是使用print函数。std::cout是C++标准库中的一个输出流对象,...
    99+
    2024-05-14
    c++
  • c语言数据结构怎么使用
    在C语言中,数据结构可以通过定义结构体来实现。以下是一些常见的数据结构的使用方法示例: 链表: #include <std...
    99+
    2024-05-14
    c语言
  • c++中print函数的用法是什么
    在C++中没有内置的print函数,但可以使用cout来输出内容到控制台。cout是C++标准库中的一个输出流对象,可以使用<...
    99+
    2024-05-14
    c++
  • c++中concept的用法是什么
    在C++20中,Concept是一种新的语言特性,用于定义类型要求和约束。Concept可以被用来约束函数模板、类模板和普通函数的参...
    99+
    2024-05-14
    c++
  • c++中concept的作用是什么
    在C++中,concept的作用是定义一种通用的约束,用于限制模板参数的类型范围。通过使用concept,可以在编译时对模板参数进行...
    99+
    2024-05-14
    c++
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作