zoukankan      html  css  js  c++  java
  • 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;
    }
    };
  • 相关阅读:
    16. 3Sum Closest
    17. Letter Combinations of a Phone Number
    20. Valid Parentheses
    77. Combinations
    80. Remove Duplicates from Sorted Array II
    82. Remove Duplicates from Sorted List II
    88. Merge Sorted Array
    257. Binary Tree Paths
    225. Implement Stack using Queues
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/shya/p/2361194.html
Copyright © 2011-2022 走看看