码迷,mamicode.com
首页 > Windows程序 > 详细

windows编程按小时生成日志文件

时间:2018-09-13 20:17:26      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:ifd   打印   path   生成   ons   关闭   color   fopen   size   

这是一个简单的日志记录方法,为了避免单个日志文件过大,所以每个小时生成一个新的日志文件

注意: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编程按小时生成日志文件

标签:ifd   打印   path   生成   ons   关闭   color   fopen   size   

原文地址:https://www.cnblogs.com/nanqiang/p/9642231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!