c++封装日志类

知识点

1、控制台彩色输出

2、枚举

3、文本文件读写

4、获取当前时间及其格式化

5、定时器

封装日志类代码

#pragma once
#include <stdio.h>
#include "stdafx.h"
#include <afxinet.h>
#include <time.h>
enum COLOR
{
DARKBLUE = 1,
DARKGREEN,
DARKTEAL,
DARKRED,
DARKPINK,
DARKYELLOW,
GRAY,
DARKGRAY,
BLUE,
GREEN,
TEAL,
RED,
PINK,
YELLOW,
WHITE
};
static void ColorPrintf(const char szText[],COLOR color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
printf(szText);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),WHITE);
}

char szLogText[1024]="####################################################################################################\n## 歡迎使用<<砍柴人>>日誌係統! ##\n## ————作者:砍柴人 ##\n## ————日期:壬辰年壬寅月辛亥日 ##\n####################################################################################################\n";
class LogEvent
{
public:
FILE *fp;
LogEvent()
{
char szLogPath[MAX_PATH]={0};
char szTemp[MAX_PATH]={0};
char *szpPath;
GetModuleFileNameA(NULL,szTemp,sizeof(szTemp)-1);
szpPath=strrchr(szTemp,'.');
strncpy(szLogPath,szTemp,strlen(szTemp)-sizeof(szpPath));
strcat(szLogPath,".log");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BLUE);
fp=fopen(szLogPath,"w");
if (fp!=NULL)
{
fputs(szLogText,fp);
time_t currenttime;
struct tm* timeinfo;
time(&currenttime);
timeinfo=localtime(&currenttime);
printf("Pragram runing time:%s\n",asctime(timeinfo));
fprintf(fp,"\n\t\tPragram runing time:%s\n",asctime(timeinfo));
fputs("####################################################################################################\n",fp);
}
else
{
delete fp;
}
}
void CPrintf(const char szText[],COLOR color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
time_t currenttime;
struct tm* timeinfo;
time(&currenttime);
timeinfo=localtime(&currenttime);
fprintf(fp,"Event:%s,Time:%s\n",szText,asctime(timeinfo));
printf("Event:%s,Time:%s\n",szText,asctime(timeinfo));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),WHITE);
}
void Release()
{
fclose(fp);
delete fp;
}
};

转载于:https://www.cnblogs.com/shya/archive/2012/02/21/2361194.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值