1. 程式人生 > >windows編程按小時生成日誌文件

windows編程按小時生成日誌文件

cep oid tchar filename vfp eat fwrite va_arg 簡單的

這是一個簡單的日誌記錄方法,為了避免單個日誌文件過大,所以每個小時生成一個新的日誌文件

註意:g_pLogPath 可以帶路徑,但是必須手動創建好路徑,保證目錄存在。而且要詳細到log文件名,不能帶後綴,後綴默認為.log

   後綴名需要在createLogFileName()中修改,可以改為.txt

log.h

#pragma once  
#define WRITE_LOG_ENABLE        //啟用日誌打印

#include <string>  
#include <Windows.h>  
#include <stdio.h>
using
std::string; using std::wstring; //extern const char* g_pLogPath; string createLogFileName(); string GetTime(); int myLog(const char* pSourcePath, const char* pFunName, const long lLine, const char* fmt, ...); void myLogStr(const char* pSourcePath, const char* pFunName, const long lLine, const char
* pLogText); #ifdef WRITE_LOG_ENABLE #define WRITELOG(format, ...) myLog(__FILE__, __FUNCTION__, __LINE__, format, ##__VA_ARGS__) #else #define WRITELOG(format, ...) #endif

log.cpp

#include  "Log.h"
#include "StdAfx.h"  
#include <string>  
#include <Windows.h>  
#include 
<stdio.h> using std::string; using std::wstring; const char* g_pLogPath = ".\\log\\test"; string createLogFileName() { char logPath[128] = { 0 }; SYSTEMTIME st; ::GetLocalTime(&st); char szTime[26] = { 0 }; sprintf(szTime, "%04d-%02d-%02d-%02d", st.wYear, st.wMonth, st.wDay, st.wHour); sprintf(logPath, "%s%s%s", g_pLogPath, szTime, ".log"); // printf("%s\n",logPath); return logPath; } string GetTime() { SYSTEMTIME st; ::GetLocalTime(&st); char szTime[26] = { 0 }; sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d %d ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); return szTime; } int myLog(const char* pFileName, const char* pFunName, const long lLine, const char* fmt, ...) { int ret = 0; //va_list是一個字符串指針,用於獲取不確定個數的參數 va_list args; //讀取可變參數的過程其實就是在堆棧中,使用指針,遍歷堆棧段中 //的參數列表,從低地址到高地址一個一個的把參數內容讀出來的過程 va_start(args, fmt); //該函數會根據參數fmt字符串來轉換格式並格式化數據,然後將結果輸出到參數Stream指定的文件中 //直到出現字符串結束的\0為止。 FILE* fp = NULL; fp = fopen(createLogFileName().c_str(), "a+"); string strTime = GetTime(); fprintf(fp, "%s ", strTime.c_str());//寫時間 int nFileNameLen = strlen(pFileName); char szLine[10] = { 0 }; sprintf(szLine, "%ld", lLine); int nLineLen = strlen(szLine); int nSpaceLen = 30 - nFileNameLen - nLineLen; for (int i = 0; i < nSpaceLen; ++i) { fwrite(" ", 1, 1, fp); } fprintf(fp, "%s:%ld ", pFileName, lLine); ret = vfprintf(fp, fmt, args); //獲取完所有參數之後,為了避免發生程序癱瘓,需要將 ap指針關閉,其實這個函數相當於將args設置為NULL va_end(args); fflush(fp); fclose(fp); return ret; } void myLogStr(const char* pSourcePath, const char* pFunName, const long lLine, const char* pLogText) { if (pLogText == NULL) return; int nLogLen = strlen(pLogText); if (nLogLen == 0) return; int nSourceLen = strlen(pSourcePath); int nFunLen = strlen(pFunName); char szLine[10] = { 0 }; sprintf(szLine, "%ld", lLine); int nLineLen = strlen(szLine); int nSpaceLen = 80 - nSourceLen - nFunLen - nLineLen; string strTime = GetTime(); FILE* fp = NULL; fp = fopen(createLogFileName().c_str(), "a+"); fwrite(strTime.c_str(), strTime.size(), 1, fp); fwrite(" ", 1, 1, fp); fwrite(pSourcePath, nSourceLen, 1, fp); for (int i = 0; i<nSpaceLen; ++i) fwrite(" ", 1, 1, fp); fwrite(pFunName, nFunLen, 1, fp); fwrite(":", 1, 1, fp); fwrite(szLine, nLineLen, 1, fp); fwrite(" ", 1, 1, fp); fwrite(pLogText, nLogLen, 1, fp); fwrite("\n", 1, 1, fp); fclose(fp); }

main.cpp

// WriteLog.cpp : 定義控制臺應用程序的入口點。
//

#include "stdafx.h"
#include "log.h"


int _tmain(int argc, _TCHAR* argv[])
{
    for (int i = 0; i < 5; i++)
    {
        WRITELOG("打印%s%d%s%\n", "hello...", 123, "hello...");
    }
    string test = "結束打印";
    WRITELOG("%s\n", test.c_str());
    WRITELOG("\n");
    system("pause");
    return 0;
}

windows編程按小時生成日誌文件