1 | #include "globals.h"
|
---|
2 | #include <syslog.h>
|
---|
3 |
|
---|
4 | char logfile[256]=CS_LOGFILE;
|
---|
5 |
|
---|
6 | static FILE *fp=(FILE *)0;
|
---|
7 | static FILE *fps=(FILE *)0;
|
---|
8 | static int use_syslog=0;
|
---|
9 | static int use_stdout=0;
|
---|
10 |
|
---|
11 | #ifdef CS_ANTICASC
|
---|
12 | FILE *fpa=(FILE *)0;
|
---|
13 | int use_ac_log=0;
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | static 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 | void 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 |
|
---|
66 | int cs_init_log(char *file)
|
---|
67 | {
|
---|
68 | static char *head = ">> STREAMBOARD << mp-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>> STREAMBOARD << mp-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 |
|
---|
108 | static char *get_log_header(int m, char *txt)
|
---|
109 | {
|
---|
110 | if (m)
|
---|
111 | {
|
---|
112 | sprintf(txt, "%6ld ", 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 |
|
---|
139 | static 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, 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, "%03.3d", 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 |
|
---|
194 | void 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 |
|
---|
205 | void cs_close_log(void)
|
---|
206 | {
|
---|
207 | if (use_stdout || use_syslog || !fp) return;
|
---|
208 | fclose(fp);
|
---|
209 | fp=(FILE *)0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | void cs_debug(char *fmt,...)
|
---|
213 | {
|
---|
214 | char txt[256];
|
---|
215 |
|
---|
216 | //cs_log("cs_debug called, cs_ptyp=%d, cs_dblevel=%d", cs_ptyp, cs_dblevel);
|
---|
217 | // if ((cs_ptyp & cs_dblevel)==cs_ptyp)
|
---|
218 | if ((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp)
|
---|
219 | {
|
---|
220 | va_list params;
|
---|
221 | va_start(params, fmt);
|
---|
222 | vsprintf(txt+11, fmt, params);
|
---|
223 | va_end(params);
|
---|
224 | write_to_log(1, txt);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | void cs_dump(uchar *buf, int n, char *fmt, ...)
|
---|
229 | {
|
---|
230 | int i;
|
---|
231 | char txt[512];
|
---|
232 |
|
---|
233 | if( fmt )
|
---|
234 | cs_log(fmt);
|
---|
235 |
|
---|
236 | for( i=0; i<n; i+=16 )
|
---|
237 | {
|
---|
238 | sprintf(txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
239 | write_to_log(i==0, txt);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void cs_ddump(uchar *buf, int n, char *fmt, ...)
|
---|
244 | {
|
---|
245 | int i;
|
---|
246 | char txt[512];
|
---|
247 |
|
---|
248 | // if (((cs_ptyp & cs_dblevel)==cs_ptyp) && (fmt))
|
---|
249 | if (((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp) && (fmt))
|
---|
250 | {
|
---|
251 | va_list params;
|
---|
252 | va_start(params, fmt);
|
---|
253 | vsprintf(txt+11, fmt, params);
|
---|
254 | va_end(params);
|
---|
255 | write_to_log(1, txt);
|
---|
256 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
257 | }
|
---|
258 | // if (((cs_ptyp | D_DUMP) & cs_dblevel)==(cs_ptyp | D_DUMP))
|
---|
259 | if (((cs_ptyp | D_DUMP) & client[cs_idx].dbglvl)==(cs_ptyp | D_DUMP))
|
---|
260 | {
|
---|
261 | for (i=0; i<n; i+=16)
|
---|
262 | {
|
---|
263 | sprintf(txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
264 | write_to_log(i==0, txt);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | int cs_init_statistics(char *file)
|
---|
270 | {
|
---|
271 | if ((!fps) && (file[0]))
|
---|
272 | {
|
---|
273 | if ((fps=fopen(file, "a"))<=(FILE *)0)
|
---|
274 | {
|
---|
275 | fps=(FILE *)0;
|
---|
276 | cs_log("couldn't open statistics file: %s", file);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | return(fps<=(FILE *)0);
|
---|
280 | }
|
---|
281 |
|
---|
282 | void cs_statistics(int idx)
|
---|
283 | {
|
---|
284 | time_t t;
|
---|
285 | struct tm *lt;
|
---|
286 |
|
---|
287 | if (fps)
|
---|
288 | {
|
---|
289 | float cwps;
|
---|
290 |
|
---|
291 | switch_log(cfg->usrfile, &fps, cs_init_statistics);
|
---|
292 | time(&t);
|
---|
293 | lt=localtime(&t);
|
---|
294 | if (client[idx].cwfound+client[idx].cwnot>0)
|
---|
295 | {
|
---|
296 | cwps=client[idx].last-client[idx].login;
|
---|
297 | cwps/=client[idx].cwfound+client[idx].cwnot;
|
---|
298 | }
|
---|
299 | else
|
---|
300 | cwps=0;
|
---|
301 |
|
---|
302 | fprintf(fps, "%02d.%02d.%02d %02d:%02d:%02d %3.1f %s %s %d %d %d %d %d %d %s\n",
|
---|
303 | lt->tm_mday, lt->tm_mon+1, lt->tm_year%100,
|
---|
304 | lt->tm_hour, lt->tm_min, lt->tm_sec, cwps,
|
---|
305 | client[idx].usr[0] ? client[idx].usr : "-",
|
---|
306 | cs_inet_ntoa(client[idx].ip), client[idx].port,
|
---|
307 | client[idx].cwfound, client[idx].cwcache, client[idx].cwnot,
|
---|
308 | client[idx].login, client[idx].last,
|
---|
309 | ph[client[idx].ctyp].desc);
|
---|
310 | fflush(fps);
|
---|
311 | }
|
---|
312 | }
|
---|