source: trunk/oscam-log.c@ 70

Last change on this file since 70 was 70, checked in by rorothetroll, 12 years ago

csctapi/io_serial.c : fix the serial port read/write wait routine to use select instead of poll

This make them compatible with Mac OS X and now oscamd works on OS X. The same code off course
still works on linux and other unix platforms. I let that code ran for 24h before commiting this code.
If you have any issue let me know and I'llr evert to poll for non compatible machines.

all the others : fix all the warning due to sign difference (mostly uchar versus char). This make the code compile

with -Werror on the more strict version of gcc (which is the case on OS X). I also noticed that in a lot of places
the code use a buffer defined as an uchar * ... and use strings functions (strnXXXX) where some memcpy/memcmp .. function
would have been preferable as we're suposedly manipulation a binary buffer. Anyway I fixed all of them and it now compile
without any warning or error on linux and OS X (and also probably on all the other unices but we will have to try before
adding -Werror on the other platform).

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