C语言日志操作类实例
生活随笔
收集整理的這篇文章主要介紹了
C语言日志操作类实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
包含三個主要的文件: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 250enum {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 debugva_list args;static char logStr[MAX_BUFSIZE] = {0}; // store log stringva_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 fileif(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-paramreturn 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,否則返回-1fp: 文件指針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);}elseret = fprintf(fp, "%s\n", str);if(ret >= 0){fflush(fp);return 0; // 寫文件成功}elsereturn -1; }int closeFile(FILE *fp) {return fclose(fp); }
總結
以上是生活随笔為你收集整理的C语言日志操作类实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 验证软件包的数字签名
- 下一篇: JAVA_SE基础——24.面向对象的内