Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0f4106da4dfc52369a7137dcdf2bad1519b8ef61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef _PRAGMA_COPYRIGHT_
#define _PRAGMA_COPYRIGHT_
#pragma comment(copyright, "%Z% %I% %W% %D% %T%\0")
#endif /* _PRAGMA_COPYRIGHT_ */
/****************************************************************************

* Copyright (c) 2008, 2010 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0s
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html

 Classes: Envvar

 Description: Environment variable manipulation.
   
 Author: Liu Wei, Tu HongJ

 History:
   Date     Who ID    Description
   -------- --- ---   -----------
   10/06/08 lwbjcdl      Initial code (D153875)

****************************************************************************/

#include "log.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>

const char *logHeader[] = {
    "[CRIT]",
    "[ERROR]",
    "[WARN]",
    "[INFO]",
    "[DEBUG]",
    "[PERF]",
    "[OTHER]",
};

Log *Log::logger = NULL;

Log::Log()
{
}

Log::~Log()
{
}

void Log::init(const char *directory, const char * filename, int level)
{
    assert(filename);
    assert(directory);
    
    char node[256] = {0};
    gethostname(node, sizeof(node));

    logDir = (char *) directory;
    sprintf(logPath, "%s/%s.%s.%d" , directory, node, filename, (int)getpid());
    permitLevel = level;
}

void Log::print(int level, char *srcFile, int srcLine, const char *format, ...)
{
    if(level > permitLevel)
        return;
    
    char tmMsg[MAX_LOG_LEN];
    time_t time1;
    struct tm tm1;
    va_list args;
    
    va_start(args, format);
    memset(tmMsg, 0, MAX_LOG_LEN);
    time(&time1);
    localtime_r(&time1, &tm1);
    strftime(tmMsg, MAX_LOG_LEN, "%y%m%d-%H:%M:%S", &tm1);
    
    FILE *fp = fopen(logPath, "a");
    if (fp) {
        fprintf(fp, "%s", tmMsg);
        fprintf(fp, "%s ", (char *)logHeader[level]);
        vfprintf(fp, format, args);
        fprintf(fp, " (%s:%d|%lu)\n", srcFile, srcLine, pthread_self());
        fclose(fp);
    }
    
    va_end(args);
}

Back to the top