fs_log

 

 

 

#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

 

posted @ 2018-08-31 14:58  mrsandstorm  阅读(213)  评论(0编辑  收藏  举报