source: trunk/oscam-log.c@ 109

Last change on this file since 109 was 100, checked in by polo, 11 years ago

remove card_info logging after log rotation
card_info is buggy for some reader

File size: 6.8 KB
Line 
1#include "globals.h"
2#include <syslog.h>
3
4char logfile[256]=CS_LOGFILE;
5
6static FILE *fp=(FILE *)0;
7static FILE *fps=(FILE *)0;
8static int use_syslog=0;
9static int use_stdout=0;
10
11#ifdef CS_ANTICASC
12FILE *fpa=(FILE *)0;
13int use_ac_log=0;
14#endif
15
16static void switch_log(char* file, FILE **f, int (*pfinit)(char*))
17{
18 if( cfg->max_log_size && mcl)
19 {
20 struct stat stlog;
21 if( stat(file, &stlog)!=0 )
22 {
23 fprintf(stderr, "stat('%s',..) failed (errno=%d)\n", file, errno);
24 return;
25 }
26
27 if( stlog.st_size >= cfg->max_log_size*1024 )
28 {
29 int rc;
30 char prev_log[128];
31 sprintf(prev_log, "%s-prev", file);
32 fprintf(*f, "switch log file\n");
33 fflush(*f);
34 fclose(*f);
35 *f=(FILE *)0;
36 rc=rename(file, prev_log);
37 if( rc!=0 ) {
38 fprintf(stderr, "rename(%s, %s) failed (errno=%d)\n",
39 file, prev_log, errno);
40 }
41 else if( pfinit(file))
42 cs_exit(0);
43
44
45 }
46 }
47}
48
49void cs_write_log(char *txt)
50{
51#ifdef CS_ANTICASC
52 if( use_ac_log && fpa )
53 {
54 switch_log(cfg->ac_logfile, &fpa, ac_init_log);
55 fprintf(fpa, "%s", txt);
56 fflush(fpa);
57 }
58 else
59#endif
60 if (fp || use_stdout)
61 {
62 if( !use_stdout && !use_syslog) switch_log(logfile, &fp, cs_init_log);
63 fprintf(fp, "%s", txt);
64 fflush(fp);
65 }
66}
67
68int cs_init_log(char *file)
69{
70 static char *head = ">> OSCam << cardserver started";
71
72 if (!strcmp(file, "stdout"))
73 {
74 use_stdout=1;
75 fp=stdout;
76 cs_log(head);
77 return(0);
78 }
79 if (strcmp(file, "syslog"))
80 {
81 if (!fp)
82 {
83 if ((fp=fopen(file, "a+"))<=(FILE *)0)
84 {
85 fp=(FILE *)0;
86 fprintf(stderr, "couldn't open logfile: %s (errno %d)\n", file, errno);
87 }
88 else
89 {
90 time_t t;
91 char line[80];
92 memset(line, '-', sizeof(line));
93 line[(sizeof(line)/sizeof(char))-1]='\0';
94 time(&t);
95 fprintf(fp, "\n%s\n>> OSCam << cardserver started at %s%s\n", line, ctime(&t), line);
96 cs_log_config();
97 }
98 }
99 return(fp<=(FILE *)0);
100 }
101 else
102 {
103 openlog("oscam", LOG_NDELAY, LOG_DAEMON);
104 use_syslog=1;
105 cs_log(head);
106 return(0);
107 }
108}
109
110static char *get_log_header(int m, char *txt)
111{
112 if (m)
113 {
114 sprintf(txt, "%6d ", getpid());
115 if (cs_idx)
116 switch (client[cs_idx].typ)
117 {
118 case 'r':
119 case 'p': sprintf(txt+7, "%c%02d ", client[cs_idx].typ, cs_idx-1);
120 break;
121 case 'm':
122 case 'c': sprintf(txt+7, "%c%02d ", client[cs_idx].typ, cs_idx-cdiff);
123 break;
124#ifdef CS_ANTICASC
125 case 'a':
126#endif
127 case 'l':
128 case 'n': sprintf(txt+7, "%c " , client[cs_idx].typ);
129 break;
130 }
131 else
132 strcpy(txt+7, "s ");
133 }
134 else
135 {
136 sprintf(txt, "%-11.11s", "");
137 }
138 return(txt);
139}
140
141static void write_to_log(int flag, char *txt)
142{
143 //static int logcounter=0;
144 int i;
145 time_t t;
146 struct tm *lt;
147 char buf[512], sbuf[16];
148
149 get_log_header(flag, sbuf);
150 memcpy(txt, sbuf, 11);
151
152 if (use_syslog && !use_ac_log) // system-logfile
153 syslog(LOG_INFO, "%s", txt);
154 else {
155 time(&t);
156 lt=localtime(&t);
157 sprintf(buf, "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s\n",
158 lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
159 lt->tm_hour, lt->tm_min, lt->tm_sec, txt);
160
161/*
162 #ifdef CS_ANTICASC
163 if (fp || fpa || use_stdout) // logfile
164 #else
165 if (fp || use_stdout) // logfile
166 #endif
167 {
168*/
169 if ((*log_fd) && (client[cs_idx].typ!='l') && (client[cs_idx].typ!='a'))
170 write_to_pipe(*log_fd, PIP_ID_LOG, (uchar *) buf+8, strlen(buf+8));
171 else
172 cs_write_log(buf+8);
173// }
174 }
175 store_logentry(buf);
176
177 for (i=0; i<CS_MAXPID; i++) // monitor-clients
178 {
179 if ((client[i].pid) && (client[i].log))
180 {
181 if (client[i].monlvl<2)
182 {
183 if ((client[cs_idx].typ!='c') && (client[cs_idx].typ!='m'))
184 continue;
185 if (strcmp(client[cs_idx].usr, client[i].usr))
186 continue;
187 }
188 sprintf(sbuf, "%03d", client[i].logcounter);
189 client[i].logcounter=(client[i].logcounter+1) % 1000;
190 memcpy(buf+4, sbuf, 3);
191 monitor_send_idx(i, buf);
192 }
193 }
194}
195
196void cs_log(char *fmt,...)
197{
198 char txt[256+11];
199
200 va_list params;
201 va_start(params, fmt);
202 vsprintf(txt+11, fmt, params);
203 va_end(params);
204 write_to_log(1, txt);
205}
206
207void cs_close_log(void)
208{
209 if (use_stdout || use_syslog || !fp) return;
210 fclose(fp);
211 fp=(FILE *)0;
212}
213
214void cs_debug(char *fmt,...)
215{
216 char txt[256];
217
218// cs_log("cs_debug called, cs_ptyp=%d, cs_dblevel=%d, %d", cs_ptyp, client[cs_idx].dbglvl ,cs_ptyp & client[cs_idx].dbglvl);
219
220// if ((cs_ptyp & cs_dblevel)==cs_ptyp)
221 if ((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp)
222 {
223 va_list params;
224 va_start(params, fmt);
225 vsprintf(txt+11, fmt, params);
226 va_end(params);
227 write_to_log(1, txt);
228 }
229}
230
231void cs_dump(uchar *buf, int n, char *fmt, ...)
232{
233 int i;
234 char txt[512];
235
236 if( fmt )
237 cs_log(fmt);
238
239 for( i=0; i<n; i+=16 )
240 {
241 sprintf(txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
242 write_to_log(i==0, txt);
243 }
244}
245
246void cs_ddump(uchar *buf, int n, char *fmt, ...)
247{
248 int i;
249 char txt[512];
250
251// if (((cs_ptyp & cs_dblevel)==cs_ptyp) && (fmt))
252 if (((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp) && (fmt))
253 {
254 va_list params;
255 va_start(params, fmt);
256 vsprintf(txt+11, fmt, params);
257 va_end(params);
258 write_to_log(1, txt);
259//printf("LOG: %s\n", txt); fflush(stdout);
260 }
261// if (((cs_ptyp | D_DUMP) & cs_dblevel)==(cs_ptyp | D_DUMP))
262 if (((cs_ptyp | D_DUMP) & client[cs_idx].dbglvl)==(cs_ptyp | D_DUMP))
263 {
264 for (i=0; i<n; i+=16)
265 {
266 sprintf(txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
267 write_to_log(i==0, txt);
268 }
269 }
270}
271
272int cs_init_statistics(char *file)
273{
274 if ((!fps) && (file[0]))
275 {
276 if ((fps=fopen(file, "a"))<=(FILE *)0)
277 {
278 fps=(FILE *)0;
279 cs_log("couldn't open statistics file: %s", file);
280 }
281 }
282 return(fps<=(FILE *)0);
283}
284
285void cs_statistics(int idx)
286{
287 time_t t;
288 struct tm *lt;
289
290 if (fps)
291 {
292 float cwps;
293
294 switch_log(cfg->usrfile, &fps, cs_init_statistics);
295 time(&t);
296 lt=localtime(&t);
297 if (client[idx].cwfound+client[idx].cwnot>0)
298 {
299 cwps=client[idx].last-client[idx].login;
300 cwps/=client[idx].cwfound+client[idx].cwnot;
301 }
302 else
303 cwps=0;
304
305 fprintf(fps, "%02d.%02d.%02d %02d:%02d:%02d %3.1f %s %s %d %d %d %d %ld %ld %s\n",
306 lt->tm_mday, lt->tm_mon+1, lt->tm_year%100,
307 lt->tm_hour, lt->tm_min, lt->tm_sec, cwps,
308 client[idx].usr[0] ? client[idx].usr : "-",
309 cs_inet_ntoa(client[idx].ip), client[idx].port,
310 client[idx].cwfound, client[idx].cwcache, client[idx].cwnot,
311 client[idx].login, client[idx].last,
312 ph[client[idx].ctyp].desc);
313 fflush(fps);
314 }
315}
Note: See TracBrowser for help on using the repository browser.