现在的位置: 首页 > 综合 > 正文

C语言日志操作类实例

2018年04月03日 ⁄ 综合 ⁄ 共 3743字 ⁄ 字号 评论关闭

包含三个主要的文件:joefunction.h(c), m.c(主函数文件)

1. m.c

#include <stdio.h>
#include <string.h>
#include <time.h>
#include "joefunction.h"

extern FILE *g_logFile;

int main(int argc, char *argv[])
{
	char temp[16] = {0}, fname[20] = {0};
	getTime(temp, TIME_FORMAT_FILENAME);
	sprintf(fname, "%s.log", temp);
	g_logFile = openFile(fname, "a+");
	if(g_logFile)
	{
		int i;
		for(i = 1; i <= 20; i++)
			writeLog("%s()-%dL: CC-[%s%d]", __FILE__, __LINE__, "Hello world", i);
		fclose(g_logFile);
	}

	return 0;
}

2. joefunction.h

// Author:		Joe Black
// Time:		2011-4-5
// Note:		This is a shared file which contains the most useful functions.


#include <stdio.h>

#define MAX_BUFSIZE 250

enum
{
	TIME_FORMAT_DATETIME,
	TIME_FORMAT_TIME,
	TIME_FORMAT_DATE,
	TIME_FORMAT_FILENAME
};

// 路径相关
int getCurFilePath(char *lpOut);				// get full path of the executable file
int getCurDir(char *lpOutDir);					// get directory-path of current executable-file

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

// 文件操作
void writeLog(char *fmt, ...);					// 写日志信息到文件
FILE* openFile(const char *fileName, const char *mode);	// 打开文本文件
int writeFile(FILE *fp, const char *str, int bLog);		// 写字符串到文件,bLog表明是否为日志文件
int closeFile(FILE *fp);

3. joefunction.c

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdarg.h>
#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 <windows.h>
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);
}

抱歉!评论已关闭.