StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
critServer.C
1 #include <stdio.h>
2 #include <errno.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 
9 #define MAX_LOGFILES 10
10 
11 
12 static char buff[1024] ;
13 static struct stat statb ;
14 
15 #ifndef PROJDIR
16 #warning "PROJDIR not defined! Assuming /RTS"
17 #define PROJDIR "/RTS"
18 #endif
19 
20 static char logfiles[MAX_LOGFILES][128] = {
21  PROJDIR"/log/rts.log",
22  PROJDIR"/log/trigger.log",
23  PROJDIR"/log/evp.log",
24  PROJDIR"/log/det.log",
25  PROJDIR"/log/daq.log",
26 } ;
27 
28 static FILE *files[MAX_LOGFILES] ;
29 
30 static int oldsizes[MAX_LOGFILES] ;
31 
32 
33 
34 
35 int main(int argc, char *argv[])
36 {
37  char *fret ;
38  int ret ;
39  int i ;
40  int data_in ;
41  u_int last_flush ;
42 
43 #ifdef OLD_TEST
44  for(i=0;i<MAX_LOGFILES;i++) {
45  printf("%3d: %d %c -%s-\n",i,logfiles[i][0],logfiles[i][0],logfiles[i]) ;
46  }
47  return 0 ;
48 #endif
49 
50  i = 0 ;
51 
52  while(logfiles[i][0] != 0) {
53 
54  files[i] = fopen(logfiles[i],"r") ;
55 
56  if(files[i] == NULL) {
57  perror(logfiles[i]) ;
58  }
59  i++ ;
60  } ;
61 
62 
63  last_flush = 0 ;
64 
65  for(;;) {
66 
67  u_int last_delta = time(NULL) - last_flush ;
68 
69  data_in = 0 ;
70  for(i=0;i<MAX_LOGFILES;i++) {
71 
72  if(logfiles[i][0]==0) continue ;
73 
74 
75 
76  errno = 0 ;
77  fret = fgets(buff,sizeof(buff),files[i]) ;
78  if(fret == NULL) {
79  if(last_delta >= 1) {
80  last_flush = time(NULL) ;
81  last_delta = 0 ;
82  fflush(stdout) ;
83  }
84 
85  if(errno) {
86  perror(logfiles[i]) ;
87  sleep(1) ;
88  continue ;
89  }
90  // let's check the file
91  ret = stat(logfiles[i],&statb) ;
92  if(ret < 0) {
93  perror(logfiles[i]) ;
94  sleep(1) ;
95  continue ;
96  }
97 
98  if(statb.st_size < oldsizes[i]) {
99  fclose(files[i]) ;
100  files[i] = fopen(logfiles[i],"r") ;
101  oldsizes[i] = 0 ;
102  continue ;
103  // reopen
104  }
105  oldsizes[i] = statb.st_size ;
106  continue ; // no data...
107  }
108 
109  data_in++ ; // we have something
110 
111  if(((strstr(buff,"OPERATOR") != NULL) ||
112  (strstr(buff,"CRITICAL") != NULL) ||
113  (strstr(buff,"SHIFTLOG") != NULL))) {
114  printf("%s",buff) ;
115  }
116 
117 
118  }
119 
120  if(!data_in) sleep(1) ;
121 
122  } // FOREVER
123 
124 
125  return -1 ; // UNREACHABLE
126 }
127 
128