summaryrefslogtreecommitdiff
path: root/client/Logger.cpp
blob: e0e769f30951a735ec4d565abdbaf6313c07621f (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <time.h>
#include "Logger.h"

using std::string;

LogLevelType Logger::LogLevel = TraceLevel;
FILE* Logger::logFile = NULL;


#ifdef DEBUG

void Logger::InitLogFile()
{
    /*
	if (logFile == NULL)
	{
		logFile = fopen("client.log", "a");
        setbuf(logFile, NULL);
	}
    */
}

void Logger::Trace(string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(TraceLevel, msg, args);
}

void Logger::Debug(string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(DebugLevel, msg, args);
}

void Logger::Info(string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(InfoLevel, msg, args);
}

void Logger::Warn(string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(WarnLevel, msg, args);	
}

void Logger::Error(string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(ErrorLevel, msg, args);
}

void Logger::Fatal(string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(FatalLevel, msg, args);
}

void Logger::Log(LogLevelType logLevel, string msg, ...)
{
	va_list args;
	va_start(args, msg);
	Logger::Log(logLevel, msg, args);
	va_end(args);
}

void Logger::Log(LogLevelType logLevel, string msg, va_list args)
{
	time_t rawtime = time(NULL);
	struct tm *lctime = localtime(&rawtime);
	char time_str[10] = {0};
	strftime(time_str, 10, "%H:%M:%S", lctime);
		
	string log_level_str;

	switch (logLevel)
	{
		default:
		case TraceLevel:
			log_level_str = "Trace";
			break;
		case DebugLevel:
			log_level_str = "Debug";
			break;
		case InfoLevel:
			log_level_str = "Info";
			break;
		case WarnLevel:
			log_level_str = "Warn";
			break;
		case ErrorLevel:
			log_level_str = "Error";
			break;
		case FatalLevel:
			log_level_str = "Fatal";
			break;
	}
	
	char logstr[1024] = {0};
	char *newfmt = new char[msg.size() + 24];
	sprintf(newfmt, "[%s] %-6s: %s", time_str, log_level_str.c_str(), msg.c_str());
	vsprintf(logstr, newfmt, args);
	fprintf(stderr, logstr);
    /*
    if (logFile != NULL)
	{
		fprintf(logFile, logstr);
	}
    */
	delete [] newfmt;
}

#endif