-
Notifications
You must be signed in to change notification settings - Fork 83
/
CWLog.c
214 lines (172 loc) · 6.7 KB
/
CWLog.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************************
* Copyright (c) 2006-7 Laboratorio di Sistemi di Elaborazione e Bioingegneria Informatica *
* Universita' Campus BioMedico - Italy *
* *
* This program is free software; you can redistribute it and/or modify it under the terms *
* of the GNU General Public License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with this *
* program; if not, write to the: *
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, *
* MA 02111-1307, USA. *
* *
* In addition, as a special exception, the copyright holders give permission to link the *
* code of portions of this program with the OpenSSL library under certain conditions as *
* described in each individual source file, and distribute linked combinations including *
* the two. You must obey the GNU General Public License in all respects for all of the *
* code used other than OpenSSL. If you modify file(s) with this exception, you may *
* extend this exception to your version of the file(s), but you are not obligated to do *
* so. If you do not wish to do so, delete this exception statement from your version. *
* If you delete this exception statement from all source files in the program, then also *
* delete it here. *
*
* --------------------------------------------------------------------------------------- *
* Project: Capwap *
* *
* Author : Ludovico Rossi (ludo@bluepixysw.com) *
* Del Moro Andrea (andrea_delmoro@libero.it) *
* Giovannini Federica (giovannini.federica@gmail.com) *
* Massimo Vellucci (m.vellucci@unicampus.it) *
* Mauro Bisson (mauro.bis@gmail.com) *
*******************************************************************************************/
#include "CWCommon.h"
//#define WRITE_STD_OUTPUT 1
#ifdef DMALLOC
#include "../dmalloc-5.5.0/dmalloc.h"
#endif
static FILE *gLogFile = NULL;
#ifndef CW_SINGLE_THREAD
CWThreadMutex gFileMutex;
#endif
void CWLogInitFile(char *fileName) {
if(fileName == NULL) {
CWLog("Wrong File Name for Log File");
}
if((gLogFile = fopen(fileName, "w")) == NULL) {
CWLog("Can't open log file: %s", strerror(errno));
exit(1);
}
#ifndef CW_SINGLE_THREAD
if(!CWCreateThreadMutex(&gFileMutex)) {
CWLog("Can't Init File Mutex for Log");
exit(1);
}
#endif
}
CWBool checkResetFile(){
long fileSize=0;
if((fileSize=ftell(gLogFile))==-1)
{
CWLog("An error with log file occurred: %s", strerror(errno));
return 0;
}
if (fileSize>=gMaxLogFileSize)
{
fclose(gLogFile);
if((gLogFile = fopen(gLogFileName, "w")) == NULL)
{
CWLog("Can't open log file: %s", strerror(errno));
return 0;
}
}
return 1;
}
void CWLogCloseFile() {
#ifndef CW_SINGLE_THREAD
CWDestroyThreadMutex(&gFileMutex);
#endif
fclose(gLogFile);
}
__inline__ void CWVLog(const char *format, va_list args) {
char *logStr = NULL;
time_t now;
char *nowReadable = NULL;
if(format == NULL) return;
now = time(NULL);
nowReadable = ctime(&now);
nowReadable[strlen(nowReadable)-1] = '\0';
// return in case of memory err: we're not performing a critical task
CW_CREATE_STRING_ERR(logStr, (strlen(format)+strlen(nowReadable)+100), return;);
//sprintf(logStr, "[CAPWAP::%s]\t\t %s\n", nowReadable, format);
sprintf(logStr, "[CAPWAP::%s]\t%08x\t %s\n", nowReadable, (unsigned int)CWThreadSelf(), format);
if(gLogFile != NULL) {
char fileLine[256];
#ifndef CW_SINGLE_THREAD
CWThreadMutexLock(&gFileMutex);
fseek(gLogFile, 0L, SEEK_END);
#endif
vsnprintf(fileLine, 255, logStr, args);
if(!checkResetFile())
{
CWThreadMutexUnlock(&gFileMutex);
exit (1);
}
fwrite(fileLine, strlen(fileLine), 1, gLogFile);
fflush(gLogFile);
#ifndef CW_SINGLE_THREAD
CWThreadMutexUnlock(&gFileMutex);
#endif
}
#ifdef WRITE_STD_OUTPUT
vprintf(logStr, args);
#endif
CW_FREE_OBJECT(logStr);
}
__inline__ void CWLog(const char *format, ...) {
va_list args;
va_start(args, format);
if (gEnabledLog)
{CWVLog(format, args);}
va_end(args);
}
__inline__ void CWDebugLog(const char *format, ...) {
#ifdef CW_DEBUGGING
char *logStr = NULL;
va_list args;
time_t now;
char *nowReadable = NULL;
if (!gEnabledLog) {return;}
if(format == NULL) {
#ifdef WRITE_STD_OUTPUT
printf("\n");
#endif
return;
}
now = time(NULL);
nowReadable = ctime(&now);
nowReadable[strlen(nowReadable)-1] = '\0';
// return in case of memory err: we're not performing a critical task
CW_CREATE_STRING_ERR(logStr, (strlen(format)+strlen(nowReadable)+100), return;);
//sprintf(logStr, "[[CAPWAP::%s]]\t\t %s\n", nowReadable, format);
sprintf(logStr, "[CAPWAP::%s]\t%08x\t %s\n", nowReadable, (unsigned int)CWThreadSelf(), format);
va_start(args, format);
if(gLogFile != NULL) {
char fileLine[256];
#ifndef CW_SINGLE_THREAD
CWThreadMutexLock(&gFileMutex);
fseek(gLogFile, 0L, SEEK_END);
#endif
vsnprintf(fileLine, 255, logStr, args);
if(!checkResetFile())
{
CWThreadMutexUnlock(&gFileMutex);
exit (1);
}
fwrite(fileLine, strlen(fileLine), 1, gLogFile);
fflush(gLogFile);
#ifndef CW_SINGLE_THREAD
CWThreadMutexUnlock(&gFileMutex);
#endif
}
#ifdef WRITE_STD_OUTPUT
vprintf(logStr, args);
#endif
va_end(args);
CW_FREE_OBJECT(logStr);
#endif
}