码迷,mamicode.com
首页 > 其他好文 > 详细

fs_log

时间:2018-08-31 15:52:29      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:file   open   hide   txt   day   out   format   ext   __line__   

 

 

 

技术分享图片
#include "fs_log.h"

void write_log (const char *format, ...)
{
    va_list arg;
    int done;
    time_t time_log;
    struct tm* tm_log;
    FILE* pFile = NULL;
#if LOG_HAVE_WINDOW_OUT
    char buff[300];
    int off = 0;
#endif
    va_start (arg, format);

    time_log = time(NULL);
    tm_log = localtime(&time_log);

    pFile = fopen(LOG_FILE, "a");

    fprintf(pFile,"%04d-%02d-%02d %02d:%02d:%02d :",
        tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
        tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);
    done = vfprintf (pFile, format, arg);
    va_end (arg);
    fflush(pFile);

    fclose(pFile);

#if LOG_HAVE_WINDOW_OUT
    memset(buff, 0, sizeof(buff));
    off = sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d [%s][%d]:",
        tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
        tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);
    sprintf(buff+off, format, arg);
    printf("%s",buff);
#endif

    return;
}

void write_log_cond(char valve, char *file_nameconst, int len, char *format, ...)
{
    va_list arg;
    int done;
    time_t time_log;
    struct tm* tm_log;
    FILE* pFile = NULL;
#if LOG_HAVE_WINDOW_OUT
    char buff[300];
    int off = 0;
#endif

    if (valve == 0)
    {
        return;
    }
    va_start (arg, format);

    time_log = time(NULL);
    tm_log = localtime(&time_log);

    pFile = fopen(LOG_FILE, "a");

    fprintf(pFile,"%04d-%02d-%02d %02d:%02d:%02d [%s][%d]:",
        tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
        tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec,
        file_nameconst,len);
    done = vfprintf (pFile, format, arg);
    va_end (arg);
    fflush(pFile);

    fclose(pFile);

#if LOG_HAVE_WINDOW_OUT
    memset(buff, 0, sizeof(buff));
    off = sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d [%s][%d]:",
        tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday,
        tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec,
        file_nameconst,len);
    sprintf(buff+off, format, arg);
    printf("%s",buff);
#endif

    return;
}
fs_log.h

 

技术分享图片
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#define LOG_FILE "./runlog.txt"
#define LOG_HAVE_WINDOW_OUT     (1)

#define SHORT_FILE strrchr(__FILE__, ‘\\‘) ? strrchr(__FILE__, ‘\\‘) + 1 : __FILE__
#define LOG_DEBUG_SET_LEVLE(a)  (a),(SHORT_FILE),(__LINE__)

#define print_deb    write_log
#define print_info    write_log
#define print_war    write_log
#define print_err    write_log
#define print_cond    write_log_cond

extern void write_log (const char *format, ...);
extern void write_log_cond (char valve, char *file_nameconst, int len, char *format, ...);

#endif
fs_log.h

 

fs_log

标签:file   open   hide   txt   day   out   format   ext   __line__   

原文地址:https://www.cnblogs.com/mrsandstorm/p/9565854.html

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