C 언어 로그 작업 클래스 인 스 턴 스

4938 단어 CLinux_C
세 개의 주요 파일 포함: joefunction. h (c), m. c (주 함수 파일)
1. m.c
#include 
#include 
#include 
#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 

#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 
#include 
#include 
#include 
#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
", curTime, str); } else ret = fprintf(fp, "%s
", str); if(ret >= 0) { fflush(fp); return 0; // } else return -1; } int closeFile(FILE *fp) { return fclose(fp); }

좋은 웹페이지 즐겨찾기