日志

日志信息可以输出到命令行实时观测,也需要输入到文件保存日志日后查看

下面的两个操作将实现这个功能,注意这个里面用了MFC的类,所以需要在支持MFC的工程中才可以使用,以后会修改成是需要STL库或不需要

文件操作:

log_file.h

#ifndef _LOG_FILE_H
#define _LOG_FILE_H
typedef enum{
    OP_REJECT,
    OP_ACCESS
}OP_TYPE;

class operator_file{
private:
    CFile c_file;
    OP_TYPE op_type;
    CString BASE_DIR;
public:
    operator_file()
    {
        op_type = OP_REJECT;
        BASE_DIR = CString("G:\\");
    };
    operator_file(CString file_path);
    ~operator_file();
    BOOL open_file(CString file_path);
    void write_log(CString log_string);
    void close_file();
};

#endif//_LOG_FILE_H



log_file.cpp



#include "log_file.h"

operator_file::operator_file(CString file_path)
{
    op_type = OP_REJECT;
    BASE_DIR = CString("G:\\");
    (void)open_file(BASE_DIR + file_path);
}

operator_file::~operator_file()
{
    close_file();
}

BOOL operator_file::open_file(CString file_path)
{
    BOOL ret = FALSE;
    if (OP_REJECT == op_type)
    {
        ret = c_file.Open(BASE_DIR + file_path, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate | CFile::typeText);
        if (TRUE == ret)
        {
            op_type = OP_ACCESS;
        }
    }
    return ret;
}

void operator_file::write_log(CString log_string)
{
    c_file.Seek(0, CFile::end);
    CString fmt_string;
    fmt_string.Format("\r\n\r\n%s", log_string.GetBuffer());
    c_file.Write(fmt_string.GetBuffer(), fmt_string.GetLength());
    close_file();
}

void operator_file::close_file()
{
    if (OP_ACCESS == op_type)
    {
        c_file.Close();
        op_type = OP_REJECT;
    }
}

写日志文件:

#ifndef _LOG_H
#define _LOG_H
#include <Time.h>
#include <cstdarg>

#define _WRITE_LOG_FILE

#ifdef _WRITE_LOG_FILE
#include "log_file.h"
#endif

typedef enum _PCCOLOR
{/*颜色同义词*/
  BLACK        = 0,
  BLUE         = 1,
  DARK_GREEN   = 2,
  LIGHT_BLUE   = 3,
  RED          = 4,
  PURPLE       = 5,
  ORANGE       = 6,
  GREY         = 7,
  DARKER_GREY  = 8,
  MEDIUM_BLUE  = 9,
  LIGHT_GREEN  = 10,
  TEAL         = 11,
  RED_ORANGE   = 12,
  LIGHT_PURPLE = 13,
  YELLOW       = 14,
  WHITE        = 15
}PCCOLOR;

void log_msg(unsigned char color, char* fmt,...);
CString fmt_time(int year, int month, int day, int hour, int minute, int second);
CString format_current_time();
int setTextColor(unsigned char fColor);
int setBackColor(unsigned char bColor);
void log_message(char* fmt,...);
void log_major(char* fmt,...);
void log_success(char* fmt,...);

#endif


#include "log.h"

#ifdef _WRITE_LOG_FILE
operator_file g_op_file;
#endif
 
/*设置字体颜色*/
int setTextColor(unsigned char fColor)
{
   HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO csbInfo;
   GetConsoleScreenBufferInfo(hd, &csbInfo);
   return SetConsoleTextAttribute(hd, fColor | (csbInfo.wAttributes&~0x0F));
}
/*设置背景颜色*/
int setBackColor(unsigned char bColor)
{
   HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO csbInfo;
   GetConsoleScreenBufferInfo(hd, &csbInfo);
   return SetConsoleTextAttribute(hd, (bColor << 4) | (csbInfo.wAttributes&~0xF0));
}

void log_message(char* fmt,...)
{
    log_msg(WHITE, fmt);
}

void log_major(char* fmt,...)
{
    log_msg(RED, fmt);
}

void log_success(char* fmt,...)
{
    log_msg(LIGHT_GREEN, fmt);
}

void log_msg(unsigned char color, char* fmt,...)
{
    CString strTime = format_current_time();

    CString report;
    report.Format(_T("%s[%s]\n\n"), strTime.GetBuffer(), fmt);

    char* last_report = report.GetBuffer();
    
    int i;
    char buf[126];

    va_list arg = (va_list)((char*)(&fmt) + 4);

    i = vsprintf_s(buf, 126, last_report, arg);
    
    setTextColor(color);
    printf(buf);
    setTextColor(WHITE);
#ifdef _WRITE_LOG_FILE
    CString file_name = strTime.Mid(1,10);
    file_name.Replace('-', '_');
    file_name = file_name + ".txt";
    if (TRUE == g_op_file.open_file(file_name))
    {
        g_op_file.write_log(CString(buf));
    }
    else
    {
        printf("%s-->[open file failed!]\n", strTime.GetBuffer());
    }
#endif
}

CString format_current_time()
{
    CTime current_time = CTime::GetCurrentTime();
    CString str_time;

    int year = current_time.GetYear();
    int month = current_time.GetMonth();
    int day = current_time.GetDay();
    int hour = current_time.GetHour();
    int minute = current_time.GetMinute();
    int second = current_time.GetSecond();

    return fmt_time(year, month, day, hour, minute, second);
}

CString fmt_time(int year, int month, int day, int hour, int minute, int second)
{
    CString ret_value;
    CString fmt_str;
    fmt_str.Format(_T("[%d-"), year);
    ret_value.Append(fmt_str);
    if (month < 10)
    {
        fmt_str.Format(_T("0%d-"), month);
    }
    else
    {
        fmt_str.Format(_T("%d-"), month);
    }
    ret_value.Append(fmt_str);
    if (day < 10)
    {
        fmt_str.Format(_T("0%d  "), day);
    }
    else
    {//
        fmt_str.Format(_T("%d  "), day);
    }
    ret_value.Append(fmt_str);
    if (hour < 10)
    {
        fmt_str.Format(_T("0%d:"), hour);
    }
    else
    {
        fmt_str.Format(_T("%d:"), hour);
    }
    ret_value.Append(fmt_str);
    if (minute < 10)
    {
        fmt_str.Format(_T("0%d:"), minute);
    }
    else
    {
        fmt_str.Format(_T("%d:"), minute);
    }
    ret_value.Append(fmt_str);
    if (second < 10)
    {
        fmt_str.Format(_T("0%d]-->"), second);
    }
    else
    {
        fmt_str.Format(_T("%d]-->"), second);
    }
    ret_value.Append(fmt_str);
    return ret_value;
}
posted @ 2012-12-02 20:32  哈哈开心  阅读(325)  评论(0编辑  收藏  举报