open-license-manager
2014-09-08 75b92a1795415e03113c5b11654144d18a1086ec
logging
1个文件已修改
2个文件已添加
92 ■■■■■ 已修改文件
src/library/base/CMakeLists.txt 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/logger.c 70 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/logger.h 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/CMakeLists.txt
@@ -1,6 +1,7 @@
ADD_LIBRARY(base STATIC
    EventRegistry.cpp
    StringUtils.cpp
    logger.c
)
add_dependencies( base public_key )
src/library/base/logger.c
New file
@@ -0,0 +1,70 @@
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#ifdef __unix__
#define MAX_PATH 255
#else
#include <windows.h>
#endif
static FILE *logFile = NULL;
static void timenow(char * buffer) {
    time_t rawtime;
    struct tm *timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(buffer, 64, "%Y-%m-%d %H:%M:%S", timeinfo);
}
static void getLogFname(char* logpath) {
#ifdef __unix__
    char const *folder = getenv("TMPDIR");
    if (folder == 0) {
        folder = "/tmp";
    }
    strcpy(logpath, folder);
    strcat(logpath, "/open-license.log");
#else
    int plen=GetTempPath(MAX_PATH,logpath);
    if(plen == 0) {
        fprintf(stderr, "Error getting temporary directory path");
    }
    strcat(logpath,"open-license.log");
#endif
}
void _log(char* format, ...) {
    char logpath[MAX_PATH];
    va_list args;
    char * buffer;
    if (logFile == NULL) {
        getLogFname(logpath);
        logFile = fopen(logpath, "a");
        if (logFile == NULL) {
            //what shall we do here?
            return;
        }
    }
    buffer = (char *) malloc(sizeof(char) * strlen(format) + 64);
    timenow(buffer);
    sprintf(&buffer[strlen(buffer) - 1], "-[%d]-", getpid());
    strcat(buffer, format);
    va_start(args, format);
    vfprintf(logFile, buffer, args);
    va_end(args);
    free(buffer);
}
void _shutdown_log() {
    if (logFile != NULL) {
        fclose(logFile);
        logFile = NULL;
    }
}
src/library/base/logger.h
New file
@@ -0,0 +1,21 @@
#ifndef logger_INCLUDED
#define logger_INCLUDED
#ifndef LOG_ENABLED
#ifdef NDEBUG
#define LOG_DEBUG(M, ...) _log("[INFO] %s (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define LOG_INFO(M, ...) _log("[INFO] %s (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define LOG_WARN(M, ...) _log("[WARN] %s (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define LOG_ERROR(M, ...) _log("[ERROR] %s (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#else
#define LOG_DEBUG(M,...)
#define LOG_INFO(M, ...)
#define LOG_WARN(M, ...)
#define LOG_ERROR(M, ...)
#endif
void _log(char* format, ...);
void _shutdown_log();
#endif