我的日志记录函数

来源:互联网 发布:手机看视频免广告 知乎 编辑:程序博客网 时间:2024/06/02 19:20

 

class MyLog{public:    MyLog(string path="C:\\");    int LogBegin();    int Log(string text);    int LogEnd();private:    FILE *m_Fp;    time_t m_Time;    string m_Path;};
#include "mylog.h"MyLog::MyLog(string path){    m_Fp = NULL;    m_Path = path;}int MyLog::LogBegin(){    char path[128] = {0};    char temp[128] = {0};    m_Time = time(0);    strftime(temp, sizeof(temp), "Record-%Y%m%d", localtime(&m_Time));    sprintf(path, "%s%s", m_Path, temp);    m_Fp = fopen(path, "a+");    if(m_Fp == NULL)    {        fprintf(stderr, "Open file failed!(%s)\n", path);        perror("SysLogFile open:");        m_Fp = NULL;    }    return 0;}int MyLog::Log(string text){    if(m_Fp == NULL)    {        fprintf(stderr, "file not exist!\n");        m_Fp = NULL;        return 1;    }    char temp[128] = {0};    m_Time = time(0);    strftime(temp, sizeof(temp), "[%H:%M:%S]", localtime(&m_Time)); //%Y-%m-%d    fprintf(m_Fp, "%s %s\n", temp, text.data());    fflush(m_Fp);    return 0;}int MyLog::LogEnd(){    if(m_Fp)    {        fclose(m_Fp);    }    return 0;}.



//创建目录   CString pathContent = GetExeDirectory() + "\\log";  DWORD dwAttr = GetFileAttributes(pathContent);  if( dwAttr==-1 || (dwAttr&FILE_ATTRIBUTE_DIRECTORY) ==0 ) //目录不存在   {      CreateDirectory( pathContent, NULL ); //创建目录   }  


 
//test.cpp//配置文件格式化读写 char *pStr = { "fdsnfks[price]=sendmail" };    char *session = { "price" };    char retStr[100] = { 0 };    int pos = GetSession( pStr, session );    if( pos >= 0 ) //找到节点名    {        GetContent( pStr+pos, session, retStr ); //根据节点名取值    }////////////////////////////////////////////////////////////////////////enum    {      IS_COMMENT ,    //是注释      IS_SESSION ,    //是节名      IS_VALUE,       //是变量的值      NOT_FOUND       //没有找到指定的变量名    };//函数功能: 从指定的字符串中查找一个节(在[]中的字符串)的名称int MainWindow::GetSession( char *pStr, char *sessionName ){    char tmpStr[100] = { 0 };    int n = 0;    char *pFirst = pStr;    while( *pStr == ' ' )    {        pStr++;   //跳过空格    }    while( *pStr != '[' )    {        pStr++;    }    pStr++; //跳过'['    while( *pStr == ' ' )    {        pStr++; //跳过空格    }    int pos = pStr - pFirst;    //获得节名    while( *pStr != ' ' && *pStr != ']' )    {        tmpStr[n] = *pStr;        pStr++;        n++;    }    tmpStr[n] = 0;    if( strcmp( tmpStr, sessionName ) != 0 )    {        return -1; //不是指定的节名    }    return pos;}//根据节点名取值int MainWindow::GetContent( char *pStr, char *valueName, char *retStr ){    char tmpStr[100] = { 0 };    int n = 0;    retStr[0] = 0;    while( *pStr == ' ' )    {        pStr++; //去掉开头的空格    }    if( *pStr == '#' ) return IS_COMMENT;  //是注释    if( *pStr == '[' ) return IS_SESSION;  //是节名    if( *pStr == 0   ) return NOT_FOUND;   //已到行尾,未找到    //获得变量名    while( *pStr != ' ' && *pStr!='\t' && *pStr != '=' && *pStr != 0 )    {        if( *pStr == ']' )        {            pStr++;            continue;        }        tmpStr[n] = *pStr;        pStr++;        n++;    }    tmpStr[n] = 0;    if( strcmp( tmpStr, valueName) != 0 )    {        return NOT_FOUND;  //不是指定的变量    }    while( *pStr == ' ' || *pStr=='\t' || *pStr == '=' )    {        pStr++; //去掉空格及'='    }    //获得变量的值    n=0;    while( *pStr != ' ' && *pStr != '#' && *pStr != 127 )    {        retStr[n] = *pStr;        pStr++;        n++;    }    retStr[n] = 0;    return IS_VALUE;}

 
原创粉丝点击