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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
#include "Logger.h"
#include <windows.h>
#include <string.h>
#define LOGGER_DATE_STRING_LENGTH 25
/**
* Constructs new Logger object.
* @param pLogFile the filename of the log file.
* @param pLogPath the path of the log file.
*/
Logger::Logger(const char* pLogFile, const char* pLogPath, int pLogLevel)
{
logLevel = pLogLevel;
canWriteInFile = false;
if(pLogPath != NULL && strlen(pLogPath) != 0)
{
logPath = (char*)malloc((strlen(pLogPath)+1)*sizeof(char));
memcpy(logPath, pLogPath, strlen(pLogPath) + 1);
if(pLogFile != NULL && strlen(pLogFile) != 0)
{
// This code enables different log files for every instance of the application.
// char *dateString = (char*)malloc(LOGGER_DATE_STRING_LENGTH*sizeof(char));
// getCurrentTimeString(dateString);
// logFile = (char*)malloc((strlen(pLogPath) + strlen(pLogFile) + strlen(dateString) + 1)*sizeof(char));
// sprintf(logFile, "%s%s%s", pLogPath, dateString, pLogFile);
// free(dateString);
logFile = (char*)malloc((strlen(pLogPath) + strlen(pLogFile) + 1)*sizeof(char));
sprintf(logFile, "%s%s", pLogPath, pLogFile);
file = fopen(logFile, "w");
if(file != NULL)
{
canWriteInFile = true;
}
}
}
if(!canWriteInFile)
{
logPath = NULL;
logFile = NULL;
file = NULL;
}
}
Logger::~Logger()
{
if(logPath != NULL)
{
free(logPath);
}
if(logFile != NULL)
{
free(logFile);
}
if(canWriteInFile)
fclose(file);
}
const char* Logger::getCurrentFile()
{
return "";
}
/**
* Returns current timestamp string.
*/
void Logger::getCurrentTimeString(char* dateString)
{
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
sprintf(dateString,"%u-%02u-%02u-%02u-%02u-%02u.%u",
systemTime.wYear,
systemTime.wMonth,
systemTime.wDay,
systemTime.wHour,
systemTime.wMinute,
systemTime.wSecond,
systemTime.wMilliseconds);
}
/**
* Logs a message
* @param message the message.
*/
void Logger::log(const char* message)
{
if(canWriteInFile && logLevel >= LOGGER_LEVEL_TRACE)
{
char *dateString = (char*)malloc(LOGGER_DATE_STRING_LENGTH*sizeof(char));
getCurrentTimeString(dateString);
fprintf(file, "%s %s: %s\n",dateString, getCurrentFile(), message);
fflush(file);
free(dateString);
}
}
/**
* Logs a message
* @param message the message.
*/
void Logger::logInfo(const char* message)
{
if(canWriteInFile && logLevel >= LOGGER_LEVEL_INFO )
{
char *dateString = (char*)malloc(LOGGER_DATE_STRING_LENGTH*sizeof(char));
getCurrentTimeString(dateString);
fprintf(file, "%s %s: %s\n",dateString, getCurrentFile(), message);
fflush(file);
free(dateString);
}
}
/**
* Returns the path of the log file.
*/
char* Logger::getLogPath()
{
return logPath;
}
/**
* Returns the current log level.
*/
int Logger::getLogLevel()
{
return logLevel;
}
|