CLog 头 代码很简单 如果需要的直接Ctrl+C  ----Ctrl+V 即可

 #ifndef __CLOG__
#define __CLOG__
#include <windows.h>
#include <string>
#include <fstream>
#include <tchar.h>
#include <ctime>
class CLog
{
public:
CLog();
CLog(const std::string LogFile);
~CLog();
template <class T>
static void WriteLog(T x);
//支持格式化输出多参数输出
static void WriteLogFormat(const char* format, ...);
private:
static std::string GetFilePath();
std::string m_LogFilePath;
static std::string GetSystemTimes();
static bool IsPathExist(const std::string FilePath);
};
//支持输出int double 文本
template <class T> void CLog::WriteLog(T x)
{
std::fstream of(GetFilePath(), std::ios::app);
if (!of.is_open())return;
of.seekp(std::ios::end); //设置文件指针到文件尾部
of << GetSystemTimes() <<_T("line: ")<<__LINE__<<_T(" value: ")<< x << std::endl;
of.close(); //关闭文件;
}
#endif

CLog.cpp

 #include "Log.h"
CLog::CLog()
:m_LogFilePath("")
{
m_LogFilePath = GetFilePath();
if (IsPathExist(m_LogFilePath))
DeleteFile(m_LogFilePath.c_str()); } CLog::CLog(const std::string LogFile)
:m_LogFilePath(LogFile)
{
if (IsPathExist(m_LogFilePath))
DeleteFile(m_LogFilePath.c_str());
} CLog::~CLog()
{
} void CLog::WriteLogFormat(const char* format, ...)
{
va_list arglist;
std::string strArgData;
char szBuffer[0x1024];
ZeroMemory(szBuffer, 0x1024);
va_start(arglist, format);
vsprintf_s(szBuffer, format, arglist);
va_end(arglist);
strArgData = szBuffer;
std::fstream of(GetFilePath(), std::ios::app);
if (!of.is_open())return;
of << GetSystemTimes() << " Line: " << __LINE__ << " Value: " << strArgData << std::endl;
of.close();
} std::string CLog::GetFilePath()
{
std::string FlieTmp;
TCHAR szPath[MAX_PATH];
::ZeroMemory(szPath, MAX_PATH);
if (!::GetCurrentDirectory(MAX_PATH, szPath))return FlieTmp;
FlieTmp = szPath;
FlieTmp += _T("\\log.txt");
return FlieTmp;
} std::string CLog::GetSystemTimes()
{
time_t Time;
CHAR strTime[MAX_PATH];
ZeroMemory(strTime, MAX_PATH);
time(&Time);
tm t;
localtime_s(&t, &Time);
strftime(strTime, , _T("%Y-%m-%d %H:%M:%S "), &t);
std::string strTimes = strTime;
return strTimes;
} bool CLog::IsPathExist(const std::string FilePath)
{
DWORD dwAttribute = ::GetFileAttributes(FilePath.c_str());
return dwAttribute != INVALID_FILE_ATTRIBUTES;
}

C++简单实现Log日志类轻量级支持格式化输出变量的更多相关文章

  1. 模块(二)——简单的log日志

    简单的log日志 鉴于任何功能模块或系统在调试时都需要日志打印,这里随便写了一下,作为以后代码调试之用,只实现了不同等级的日志输出功能,其他的调试功能以后再行添加:使用方法简单,只需要在头文件里事先按 ...

  2. 简单实用的日志类CLog (Python版)

    #coding: utf-8 import time ''' /***************************************************************** Fu ...

  3. 20181015记录一个简单的TXT日志类

    20190422添加换行以及时间记录 using System; using System.Collections.Generic; using System.IO; using System.Lin ...

  4. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  5. Python之自定义封装一个简单的Log类

    参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...

  6. 【个人使用.Net类库】(2)Log日志记录类

    开发接口程序时,要保证程序稳定运行就要时刻监控接口程序发送和接收的数据,这就需要一个日志记录的类将需要的信息记录在日志文件中,便于自己维护接口程序.(Web系统也是如此,只是对应的日志实现比这个要复杂 ...

  7. 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)

    并发编程概述   前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...

  8. 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  9. VC++ 一个简单的Log类

    在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...

随机推荐

  1. Android SwipeActionAdapter结合Pinnedheaderlistview实现复杂列表的左右滑动操作

    在上一篇博客<Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作>里,已经介绍了利用SwipeActionAdapter来左右滑动操作列表: 然,有时 ...

  2. python常量和变量

    1.1 常量 常量是内存中用于保存固定值的单元,在程序中常量的值不能发生改变:python并没有命名常量,也就是说不能像C语言那样给常量起一个名字. python常量包括:数字.字符串.布尔值.空值: ...

  3. 2019-8-31-dotnet-使用-MessagePack-序列化对象

    title author date CreateTime categories dotnet 使用 MessagePack 序列化对象 lindexi 2019-08-31 16:55:58 +080 ...

  4. 微信支付、支付宝支付和QQ钱包支付

    最近忙于对接微信支付和支付宝支付,注册微信公众号,认证公众号,注册微信支付商户号并进行认证: 签约支付宝支付产品(手机网站支付.PC网站支付),注册支付宝企业账号(企业账号权限更大): 注册QQ钱包商 ...

  5. SQLSTATE[HY000] [2002] 错误

    http://www.thinkphp.cn/topic/36194.html 使用tp框架 3.2.3 ,在windows上跑的时候没有任何问题,但是部署到linux系统和是哪个,就会报这个错,不知 ...

  6. centos 磁盘挂载

    1.更改磁盘格式 fdisk -l fdisk /dev/vdb mkfs.xfs /dev/vdb1 mkfs.xfs /dev/vdb1 -f 2.查看UUID blkid 3.挂载文件夹 vim ...

  7. poj 2451 Uyuw's Concert (半平面交)

    2451 -- Uyuw's Concert 继续半平面交,这还是简单的半平面交求面积,不过输入用cin超时了一次. 代码如下: #include <cstdio> #include &l ...

  8. 洛谷P5019 铺设道路 题解 模拟/贪心基础题

    题目链接:https://www.luogu.org/problemnew/show/P5019 这道题目是一道模拟题,但是它有一点贪心的思想. 我们假设当前最大的深度是 \(d\) ,那么我们需要把 ...

  9. 在eclipse中建立lua开发环境

    1. 给你的eclipse安装LuaEclipse,新增Eclipse Software Update Site“http://luaeclipse.luaforge.net/update-site ...

  10. hibernate无限递归问题

    项目异常如下: 2018-01-26 17:12:38.162 WARN 3128 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionReso ...