我的日志记录函数
来源:互联网 发布:手机看视频免广告 知乎 编辑:程序博客网 时间: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;}
- 我的日志记录函数
- 我的日志记录组件
- 记录通讯日志的函数
- 记录一个日志函数的地址
- VS2010下的日志记录函数
- 今天开始记录我的BMS系统开发日志
- 我眼中最好用的Android日志记录库Logger。
- linux简单的自用的日志记录函数
- 日志的记录
- 简单的日志记录
- 记录工作的日志
- SparkStreaming的日志记录
- Hibernate的日志记录
- 我的工作日志
- 我的工作日志
- 我的日志
- 我的工作日志
- 我的项目日志
- 判断整数序列是不是二叉查找树(BST)的后序遍历结果
- jquery实现input输入框实时输入触发事件
- IOS7隐藏状态栏
- The connection to adb is down, and a severe error has occured.
- HTTP状态码大全
- 我的日志记录函数
- 关于try,finally执行顺序的分析
- Windows环境中同时安装Oracle9i 10g和11g
- php查询mysql表中所有字段详细信息
- 岁月是把杀猪刀
- C语言修改文件中一部分内容
- 程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理(3.0以前版本)
- 从MySQL中获得数据表auto_increment值(基数)的方法
- 译《Understanding Weak References》