c语言将操作信息写入日志文件,C语言日志操作类实例

#include "joefunction.h"

FILE *g_logFile = NULL;

void writeLog(char *fmt, ...)// write log infor to log file

{

#if defined(DEBUG) || defined(_DEBUG)// output log infor when debug

va_list args;

static char logStr[MAX_BUFSIZE] = {0};// store log string

va_start(args, fmt);

vsprintf(logStr, fmt, args);// format log infor to logStr[]

va_end(args);

writeFile(g_logFile, logStr, 1);// write log string to log file

#endif

}

#ifdef WIN32

#include int getCurFilePath(char *lpOut)// get full path of the executable file

{

char chPath[MAX_BUFSIZE] = {0};

int len = GetModuleFileName(NULL, chPath, MAX_BUFSIZE);

if(len > 0)

{

strcpy(lpOut, chPath);

return 1;

}

return 0;

}

int getCurDir(char *lpOutDir)// get directory-path of current executable-file

{

char chPath[MAX_BUFSIZE] = {0};

char drive[4] = {0}, subdir[MAX_BUFSIZE] = {0}, fn[MAX_BUFSIZE] = {0}, ext[MAX_BUFSIZE] = {0};

if(getCurFilePath(chPath) > 0)

{

_splitpath(chPath, drive, subdir, fn, ext);

sprintf(lpOutDir, "%s%s", drive, subdir);

return 1;

}

return 0;

}

#else

int getCurFilePath(char *lpOut)// get full path of the executable file

{

char chPath[MAX_BUFSIZE] = {0};

int len = readlink("/proc/self/exe", chPath, sizeof(chPath)); // get full path of the current-executable file

if(len >= 0)

{

strcpy(lpOut, chPath);

return 1;

}

return 0;

}

int getCurDir(char *lpOutDir)// get directory-path of current executable-file

{

char chPath[MAX_BUFSIZE] = {0};

if( getCurFilePath(chPath) > 0 )

{

dirname(chPath);// dirname will change value of "chPath"(contain result)

strcpy(lpOutDir, chPath);// copy result to out-param

return 1;

}

return 0;

}

#endif

/*

功能:获取当前系统时间

返回值:0-成功,-1-失败

out:保存返回的系统时间,格式由fmt决定

fmt:0-返回:yyyy-mm-dd hh24:mi:ss, 1-返回:yyyy-mm-dd, 2-返回:hh24:mi:ss

*/

int getTime(char *out, int fmt)// 获取当前系统时间

{

time_t t;

struct tm *tp;

if(out == NULL)

return -1;

t = time(NULL);

tp = localtime(&t);

if(fmt == TIME_FORMAT_DATETIME)

sprintf(out, "%02d/%02d/%02d %02d:%02d:%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);

else if(fmt == TIME_FORMAT_DATE)

sprintf(out, "%02d/%02d/%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);

else if(fmt == TIME_FORMAT_TIME)

sprintf(out, "%02d:%02d:%02d", tp->tm_hour, tp->tm_min, tp->tm_sec);

else if(fmt == TIME_FORMAT_FILENAME)

sprintf(out, "%02d%02d%02d_%02d%02d%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);

return 0;

}

FILE* openFile(const char *fileName, const char *mode)// 打开文本文件

{

FILE *fp = fopen(fileName, mode);

return fp;

}

/*

功能:将str写入到文件

返回值:写文件成功返回0,否则返回-1

fp:文件指针

str:待写入的字符串

bLog:1-是日志文件,0-不是日志文件

说明:如果是日志文件,将会在str前加上当前时间(格式如:2011-04-12 12:10:20)

*/

int writeFile(FILE *fp, const char *str, int bLog)// 写字符串到文件,bLog表明是否为日志文件

{

char curTime[MAX_BUFSIZE] = {0};

int ret = -1;

if(bLog) // 获取当前系统时间

{

getTime(curTime, 0);

ret = fprintf(fp, "[%s] %s\n", curTime, str);

}

else

ret = fprintf(fp, "%s\n", str);

if(ret >= 0)

{

fflush(fp);

return 0; // 写文件成功

}

else

return -1;

}

int closeFile(FILE *fp)

{

return fclose(fp);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值