StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
operDisplay.C
1 #include <stdio.h>
2 #include <errno.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <utime.h>
11 
12 #define MAX_LOGFILES 10
13 #define LOGLINES 400
14 
15 #define DATA_1 "/online/tonko/log_content_old.html"
16 #define DATA_2 "/online/tonko/log_content.html"
17 #define TOUCH "/online/tonko/log_touch.html"
18 //#define DAQ_TOUCH "/online/tonko/daq_touch.html"
19 
20 int parseLog(char *b, u_int file, u_int line) ;
21 int dumpLines(void) ;
22 
23 static char buff[1024] ;
24 static struct stat statb ;
25 
26 #ifndef PROJDIR
27 #warning "PROJDIR not defined! Assuming /RTS"
28 #define PROJDIR "/RTS"
29 #endif
30 
31 //#define LOG_SUFFIX ".0"
32 #define LOG_SUFFIX ""
33 
34 static char logfiles[MAX_LOGFILES][128] = {
35  PROJDIR"/log/rts.log"LOG_SUFFIX,
36  PROJDIR"/log/trigger.log"LOG_SUFFIX,
37  PROJDIR"/log/evp.log"LOG_SUFFIX,
38  PROJDIR"/log/det.log"LOG_SUFFIX,
39  PROJDIR"/log/daq.log"LOG_SUFFIX,
40  PROJDIR"/log/tpx.log"LOG_SUFFIX,
41  PROJDIR"/log/esb.log"LOG_SUFFIX,
42 
43 } ;
44 
45 // took out PROJDIR"/log/reader.log"LOG_SUFFIX,
46 
47 static FILE *files[MAX_LOGFILES] ;
48 
49 static int oldsizes[MAX_LOGFILES] ;
50 
51 
52 static char data_preamble[] = { " \
53 <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"> \
54 <html> \
55 <head> \
56 <link rel=\"stylesheet\" type=\"text/css\" href=\"daq.css\"> \
57 <title>STAR DAQ Logging</title> \
58 </head> \
59 <body> \
60 " };
61 
62 
63 
64 static struct logline {
65  u_int sec ;
66  char tm[24] ;
67  u_int day ; // day of the year...
68 
69  u_int repeat ;
70 
71  u_int file ;
72  u_int seq ;
73 
74  char node[10] ;
75  char rb[10] ;
76 
77  char sev[32] ;
78 
79  char task[16] ;
80  char src[128] ;
81  u_int line ;
82 
83  char msg[256] ;
84 } loglines[LOGLINES] ;
85 
86 int last_logline = 0 ;
87 
88 
89 int main(int argc, char *argv[])
90 {
91  char *fret ;
92  int ret ;
93  int i ;
94  int data_in, new_lines ;
95  u_int last_flush ;
96  u_int lines[MAX_LOGFILES] ;
97 
98 //#define OLD_TEST
99 #ifdef OLD_TEST
100  for(i=0;i<MAX_LOGFILES;i++) {
101  printf("%3d: %d %c -%s-\n",i,logfiles[i][0],logfiles[i][0],logfiles[i]) ;
102  }
103  return 0 ;
104 #endif
105 
106  i = 0 ;
107 
108 
109  FILE *ff = fopen(TOUCH,"w") ;
110  fprintf(ff,"%u\n",(u_int)time(NULL)) ;
111  fclose(ff) ;
112 
113  while(logfiles[i][0] != 0) {
114 
115  files[i] = fopen(logfiles[i],"r") ;
116 
117  if(files[i] == NULL) {
118  perror(logfiles[i]) ;
119  }
120  i++ ;
121  } ;
122 
123 
124  last_flush = 0 ;
125 
126  memset(lines,0,sizeof(lines)) ;
127 
128  new_lines = 0 ;
129 
130  for(;;) {
131 
132  u_int last_delta = time(NULL) - last_flush ;
133 
134  data_in = 0 ;
135 
136  for(i=0;i<MAX_LOGFILES;i++) {
137 
138  if(logfiles[i][0]==0) continue ;
139 
140 
141 // fprintf(stderr,"Here 1: %d\n",i) ;
142 
143  errno = 0 ;
144  fret = fgets(buff,sizeof(buff),files[i]) ;
145  if(fret == NULL) {
146 
147  if(errno) {
148  perror(logfiles[i]) ;
149  sleep(1) ;
150  continue ;
151  }
152  // let's check the file
153  errno = 0 ;
154  ret = stat(logfiles[i],&statb) ;
155  if(ret < 0) {
156  perror(logfiles[i]) ;
157  sleep(1) ;
158  continue ;
159  }
160 
161  if(statb.st_size < oldsizes[i]) {
162  // reopen
163  fclose(files[i]) ;
164  files[i] = fopen(logfiles[i],"r") ;
165  oldsizes[i] = 0 ;
166  lines[i] = 0 ;
167  continue ;
168  }
169 
170  oldsizes[i] = statb.st_size ;
171 
172  }
173  else {
174  data_in++ ; // we have something
175 
176  lines[i]++ ;
177 
178  if((strstr(buff,"CRITICAL") != NULL) ||\
179  (strstr(buff,"OPERATOR") != NULL) ||\
180  (strstr(buff,"CAUTION") != NULL)) {
181  new_lines++ ;
182  parseLog(buff,i,lines[i]) ;
183  //printf("%s",buff) ;
184  }
185  }
186 
187  }
188 
189 
190 // fprintf(stderr,"Ending...\n") ;
191 
192 
193  if((last_delta >= 60) || (new_lines && (last_delta >= 5))) { // dump every 1 if there is any new content
194  last_flush = time(NULL) ;
195  last_delta = 0 ;
196  dumpLines() ;
197  printf("Dumping %d lines\n",new_lines) ;
198  new_lines = 0 ;
199  }
200 
201  if(!data_in) { // pause if no data...
202  printf("No data...\n") ;
203  sleep(1) ;
204  }
205  else {
206  static u_int tot_data ;
207  tot_data += data_in ;
208 
209  if((tot_data % 10000)==0) {
210  printf("read %d lines\n",tot_data) ;
211  }
212  }
213 
214 
215  } // FOREVER
216 
217 
218  return -1 ; // UNREACHABLE
219 }
220 
221 int parseLog(char *b, u_int file, u_int seq)
222 {
223  u_int cou ;
224  int i ;
225  static char tm[16] ;
226  static char task[128] ;
227  static char line[128] ;
228  static char day[8] ;
229 
230  int use = 0 ;
231  u_int min_sec = 0xffffffff ;
232 
233  for(i=0;i<LOGLINES;i++) {
234  if(loglines[i].sec == 0) { // unassigned - use immediately
235  use = i ;
236  break ;
237  }
238 
239  if(loglines[i].sec <= min_sec) {
240  min_sec = loglines[i].sec ;
241  use = i ;
242  }
243  }
244 
245 
246  loglines[use].repeat = 1 ; // clear to 1...
247 
248 
249  // skip [
250  b++ ;
251 
252 
253  cou = 0 ;
254  while(*b != ' ') {
255  loglines[use].node[cou++] = *b++ ;
256  }
257  loglines[use].node[cou] = 0 ;
258 
259  while(*(++b) == ' ') ; // skip spaces
260 
261  cou = 0 ;
262  while(*b != ' ') {
263  tm[cou++] = *b++ ;
264  }
265  tm[cou] = 0 ;
266 
267  b++ ; // skip space
268 
269  // get day
270  strncpy(day,b,3) ;
271  b += 3 ;
272  loglines[use].day = atoi(day) ;
273 
274 
275  b++ ; // skip ]
276  b++ ; // skip space
277 
278  cou = 0 ;
279  while(*b != ':') {
280  task[cou++] = *b++ ;
281  }
282  task[cou] = 0 ;
283 
284 
285  b++ ; // skip :
286  b++ ; // skip space
287 
288  cou = 0 ;
289  while(*b != ':') {
290  loglines[use].sev[cou++] = *b++ ;
291  }
292  loglines[use].sev[cou] = 0 ;
293 
294 
295  b++ ; // skip :
296  b++ ; // skip space
297 
298  cou = 0 ;
299  while(*b != ' ') {
300  loglines[use].src[cou++] = *b++ ;
301  }
302  loglines[use].src[cou] = 0 ;
303 
304  b += 7 ; // skip space,[,line,space
305 
306  cou = 0 ;
307  while(*b != ']') {
308  line[cou++] = *b++ ;
309  }
310  line[cou] = 0 ;
311 
312  loglines[use].line = atoi(line) ;
313 
314  b += 3 ; // skip ],:,space
315 
316  *(b+strlen(b)-1) = 0 ;
317 
318  strncpy(loglines[use].tm,tm,sizeof(loglines[use].tm)-1) ;
319 
320  char itm[sizeof(tm)] ;
321  strcpy(itm,tm) ;
322 
323  for(cou=0;cou<strlen(tm);cou++) {
324  if(itm[cou] == ':') itm[cou] = 0 ;
325  }
326 
327 
328  loglines[use].sec = loglines[use].day*24*3600 + atoi(itm)*3600 + atoi(itm+3)*60 + atoi(itm+6) ;
329 
330 
331  char *the_task ;
332  char rb[10] ;
333  if((task[1] == 'R') && (task[2] == 'B') && (task[5] == '_') && (task[7] == ']')) { // Receiver Board
334  strncpy(rb+1,task+1,6) ;
335  rb[0] = ':' ;
336  rb[7] = 0 ;
337 
338  cou = 0 ;
339 
340  the_task = &task[9] ;
341  if((task[9] == '0') && (task[10]=='x')) {
342  *(task+strlen(task)-1)=0 ; // kill the last ")"
343  while(*the_task++ != '(') ;
344  }
345 
346  }
347  else {
348  rb[0] = 0 ;
349 
350  the_task = task+1 ; // skip "("
351  *(the_task + strlen(the_task) -1) = 0 ; // kill ")"
352  }
353 
354  if(strlen(the_task) == 0) {
355  strcpy(the_task,"???") ;
356  }
357 
358  strncpy(loglines[use].task,the_task,sizeof(loglines[use].task)-1) ;
359  strncpy(loglines[use].rb,rb,sizeof(loglines[use].rb)-1) ;
360  strncpy(loglines[use].msg,b,sizeof(loglines[use].msg)-1) ;
361 
362  printf("Adding %d, last %d: file %d, line %d....\n",use,last_logline,file,seq) ;
363 
364  loglines[use].seq = seq ;
365  loglines[use].file = file ;
366 
367  for(i=0;i<LOGLINES;i++) {
368  if(i==use) continue ;
369  if(loglines[i].sec == 0) continue ;
370 
371  if(loglines[i].line == loglines[use].line) {
372  if(loglines[i].file == loglines[use].file) {
373  if(loglines[i].sec == loglines[use].sec) {
374  if(strcmp(loglines[i].node,loglines[use].node)==0) {
375  if(strcmp(loglines[i].src,loglines[use].src) == 0) {
376  if(strcmp(loglines[i].task,loglines[use].task) == 0) {
377  loglines[use].repeat = loglines[i].repeat + 1 ;
378  loglines[i].sec = 0 ;
379  }}}}}}
380  }
381 
382 
383  last_logline = use ;
384 
385  return use ;
386 }
387 
388 
389 int compare(const void *s1, const void *s2)
390 {
391  struct logline *l1 = (struct logline *)s1 ;
392  struct logline *l2 = (struct logline *)s2 ;
393 
394  if(l1->sec < l2->sec) return 1 ;
395  if(l1->sec > l2->sec) return -1 ;
396 
397  if(l1->file == l2->file) {
398  if(l1->seq < l2->seq) return 1 ;
399  if(l1->seq > l2->seq) return -1 ;
400  return 0 ;
401  }
402 
403  // not from the same file...
404  if(l1->file < l2->file) return 1 ;
405  if(l1->file > l2->file) return -1 ;
406 
407  else return 0 ;
408 }
409 
410 int dumpLines(void)
411 {
412  int i ;
413 
414 
415 
416  time_t now = time(NULL) ;
417  struct tm *stm = localtime(&now) ;
418 
419  errno = 0 ;
420  FILE *o = fopen(DATA_1,"w") ;
421  if(o==NULL) {
422  perror(DATA_1) ;
423  return -1 ;
424  }
425 
426  fprintf(o,"%s\n",data_preamble) ;
427 
428  fprintf(o,"<table width=100%% border=1 cellspacing=0>\n") ;
429  fprintf(o,"<tr %s><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n",
430  "",
431  "Time","#","Node","Severity","Task","Source#line","Msg") ;
432 
433 
434 
435 
436  int sec = stm->tm_yday*24*3600 + stm->tm_hour*3600 + stm->tm_min*60 + stm->tm_sec ;
437 
438 
439  qsort(loglines,LOGLINES,sizeof(loglines[0]),compare) ;
440 
441 
442  for(i=0;i<LOGLINES;i++) {
443  if(loglines[i].sec == 0) continue ;
444 
445  int delta = sec - loglines[i].sec ;
446 
447  char *color ;
448 
449  if(delta > 120) { // older than 20 minutes...
450  if(strncmp(loglines[i].sev,"CRIT",4)==0) color = "style=color:#FF3333" ;
451  else if(strncmp(loglines[i].sev,"CAUT",4)==0) color = "style=color:#FF33FF" ;
452  else if(strncmp(loglines[i].sev,"OPER",4)==0) color = "style=color:#3333FF" ;
453  else color = "style=color:#333333" ;
454 
455  }
456  else {
457  if(strncmp(loglines[i].sev,"CRIT",4)==0) color = "style=color:#FF0000;font-weight:bold" ;
458  else if(strncmp(loglines[i].sev,"CAUT",4)==0) color = "style=color:#FF00FF;font-weight:bold" ;
459  else if(strncmp(loglines[i].sev,"OPER",4)==0) color = "style=color:#0000FF;font-weight:bold" ;
460  else color = "style=color:#000000;font-weight:bold" ;
461  }
462 
463 
464  fprintf(o,"<tr %s><td>%s</td><td>%d</td><td>%s</td><td>%s</td><td>%s%s</td><td>%s#%d</td><td>%s</td></tr>\n",
465  color,
466  loglines[i].tm,loglines[i].repeat,
467  loglines[i].node,loglines[i].sev,loglines[i].task,loglines[i].rb,loglines[i].src,loglines[i].line,loglines[i].msg) ;
468 
469 // printf("%7d [%s] (%d) repeat %d:%s:%s:%s%s:%s:%d:%s\n",sec-loglines[i].sec,loglines[i].tm,loglines[i].day,loglines[i].repeat,
470 // loglines[i].node,loglines[i].sev,loglines[i].rb,loglines[i].task,loglines[i].src,loglines[i].line,loglines[i].msg) ;
471  }
472 
473  fprintf(o,"</table>\n") ;
474 
475  fprintf(o,"\n</body></html>\n") ;
476  fclose(o) ;
477 
478  rename(DATA_1,DATA_2) ;
479 
480  // touch(TOUCH) ;
481  now = time(NULL) ;
482  struct utimbuf utim ;
483  utim.actime = now ;
484  utim.modtime = now ;
485 
486  utime(DATA_2,&utim) ;
487  utime(TOUCH,&utim) ;
488 // utime(DAQ_TOUCH,&utim) ;
489 
490  return 0 ;
491 }
492 
Definition: rb.hh:21