-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
245 lines (229 loc) · 5.8 KB
/
log.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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.
*
* Copyright (C) 2013-2019 iopsys Software Solutions AB
* Author Mohamed Kallel <[email protected]>
* Author Ahmed Zribi <[email protected]>
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include "cwmp.h"
#include "log.h"
static char *SEVERITY_NAMES[8] = {"[EMERG] ","[ALERT] ","[CRITIC] ","[ERROR] ","[WARNING]","[NOTICE] ","[INFO] ","[DEBUG] "};
static int log_severity = DEFAULT_LOG_SEVERITY;
static long int log_max_size = DEFAULT_LOG_FILE_SIZE;
static char log_file_name[256];
static bool enable_log_file = true;
static bool enable_log_stdout = false;
static pthread_mutex_t mutex_log = PTHREAD_MUTEX_INITIALIZER;
int log_set_severity_idx (char *value)
{
int i;
for (i=0;i<8;i++)
{
if (strstr(SEVERITY_NAMES[i],value)!=NULL)
{
log_severity = i;
return 0;
}
}
return 1;
}
int log_set_log_file_name (char *value)
{
if(value != NULL)
{
strcpy(log_file_name,value);
}
else
{
strcpy(log_file_name,DEFAULT_LOG_FILE_NAME);
}
return 1;
}
int log_set_file_max_size(char *value)
{
if(value != NULL)
{
log_max_size = atol(value);
}
return 1;
}
int log_set_on_console(char *value)
{
if(strcmp(value,"enable") == 0)
{
enable_log_stdout = true;
}
if(strcmp(value,"disable") == 0)
{
enable_log_stdout = false;
}
return 1;
}
int log_set_on_file(char *value)
{
if(strcmp(value,"enable") == 0)
{
enable_log_file = true;
}
if(strcmp(value,"disable") == 0)
{
enable_log_file = false;
}
return 1;
}
void puts_log(int severity, const char *fmt, ...)
{
va_list args;
int i;
time_t t;
struct tm *Tm;
struct timeval tv;
FILE *pLog = NULL;
struct stat st;
long int size = 0;
char log_file_name_bak[256];
char buf[1024];
char buf_file[1024];
if (severity>log_severity)
{
return;
}
pthread_mutex_lock (&mutex_log);
gettimeofday(&tv, 0);
t = time((time_t*)NULL);
Tm= localtime(&tv.tv_sec);
i = sprintf(buf,"%02d-%02d-%4d, %02d:%02d:%02d %s ",
Tm->tm_mday,
Tm->tm_mon+1,
Tm->tm_year+1900,
Tm->tm_hour,
Tm->tm_min,
Tm->tm_sec,
SEVERITY_NAMES[severity]);
if(strlen(log_file_name) == 0)
{
strcpy(log_file_name,DEFAULT_LOG_FILE_NAME);
}
if(enable_log_file)
{
if (stat(log_file_name, &st) == 0)
{
size = st.st_size;
}
if(size >= log_max_size)
{
sprintf(log_file_name_bak,"%s.1",log_file_name);
rename(log_file_name,log_file_name_bak);
pLog = fopen(log_file_name,"w");
}
else
{
pLog = fopen(log_file_name,"a+");
}
}
va_start(args, fmt);
i += vsprintf(buf+i, fmt, args);
if(enable_log_file)
{
strcpy(buf_file,buf);
strcat(buf_file,"\n");
fputs (buf_file, pLog);
}
va_end(args);
if(enable_log_file)
{
fclose(pLog);
}
if(enable_log_stdout)
{
puts(buf);
}
pthread_mutex_unlock (&mutex_log);
}
void puts_log_xmlmsg(int severity, char *msg, int msgtype)
{
va_list args;
int i;
time_t t;
struct tm *Tm;
struct timeval tv;
FILE *pLog = NULL;
struct stat st;
long int size = 0;
char log_file_name_bak[256];
char buf[1024];
char buf_file[1024];
char *description, *separator;
if (severity>log_severity)
{
return;
}
pthread_mutex_lock(&mutex_log);
gettimeofday(&tv, 0);
t = time((time_t*)NULL);
Tm= localtime(&tv.tv_sec);
i = sprintf(buf,"%02d-%02d-%4d, %02d:%02d:%02d %s ",
Tm->tm_mday,
Tm->tm_mon+1,
Tm->tm_year+1900,
Tm->tm_hour,
Tm->tm_min,
Tm->tm_sec,
SEVERITY_NAMES[severity]);
if(strlen(log_file_name) == 0)
{
strcpy(log_file_name,DEFAULT_LOG_FILE_NAME);
}
if (msgtype == XML_MSG_IN) {
description = "MESSAGE IN\n";
separator = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";
}
else {
description = "MESSAGE OUT\n";
separator = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
}
if(enable_log_file)
{
if (stat(log_file_name, &st) == 0)
{
size = st.st_size;
}
if(size >= log_max_size)
{
sprintf(log_file_name_bak,"%s.1",log_file_name);
rename(log_file_name,log_file_name_bak);
pLog = fopen(log_file_name,"w");
}
else
{
pLog = fopen(log_file_name,"a+");
}
fputs (buf, pLog);
fputs(description, pLog);
fputs(separator, pLog);
fputs (msg, pLog);
fputs ("\n", pLog);
fputs(separator, pLog);
fclose(pLog);
}
if(enable_log_stdout)
{
puts (buf);
puts(description);
puts(separator);
puts (msg);
puts ("\n");
puts(separator);
}
pthread_mutex_unlock (&mutex_log);
}