StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
cursesLogBrowser.C
1 //
2 // $Id: cursesLogBrowser.C,v 1.2 2008/02/27 19:12:21 fine Exp $
3 //
4 // Based on critServer.C written by Tonko Ljubicic
5 // Curses functionality and command line args added by M.J. LeVine 01/2003
6 //
7 
8 #include <stdio.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <strings.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <curses.h>
16 
17 #define MAX_LOGFILES 128
18 
19 
20 //#define DEBUG
21 static char buff[1024] ;
22 static struct stat statb ;
23 
24 static char level_str[] = "dummy string";
25 
26 static char logfiles[MAX_LOGFILES][128] = {
27  "/RTS/log/rts.log",
28  "/RTS/log/trigger.log",
29  "/RTS/log/evp.log",
30  "/RTS/log/det.log",
31 } ;
32 
33 static FILE *files[MAX_LOGFILES] ;
34 
35 static int oldsizes[MAX_LOGFILES] ;
36 
37 // we define the colors here
38 // put near top of file for visibility
39 void define_colors(){
40  // palette:
41  // COLOR_BLACK
42  // COLOR_RED
43  // COLOR_GREEN
44  // COLOR_YELLOW
45  // COLOR_BLUE
46  // COLOR_MAGENTA
47  // COLOR_CYAN
48  // COLOR_WHITE
49 
50  // init_pair(refno,foreground color, background color)
51  init_pair(1,COLOR_BLACK,COLOR_WHITE); // used for DEBUG & NOTICE
52  init_pair(2,COLOR_CYAN,COLOR_WHITE); // used for WARNING
53  init_pair(3,COLOR_MAGENTA,COLOR_WHITE); // used for ERROR
54  init_pair(4,COLOR_MAGENTA,COLOR_YELLOW); // used for OPERATOR
55  init_pair(5,COLOR_RED,COLOR_BLACK); // used for CRITICAL & TERR
56 
57  attron(COLOR_PAIR(1));
58  bkgd(' ');
59 }
60 
61 int main(int argc, char *argv[])
62 {
63  char *fret ;
64  int ret ;
65  int i,j, level ;
66  int data_in ;
67 
68  for (i=2; i<=argc; i++) {
69  if (strncmp(argv[i-1],"-h",2)==0) {
70  initscr();
71  printw("usage: logBrowser [-n ALARM_LEVEL] [file1 [,file2....]]\n\n\
72  ALARM_LEVEL={DEBug,NOTice,WARNing,ERRor,OPERator,CRITical,TERR}\n\
73  specifies the lowest level to be printed.\n\
74  Values of ALARM_LEVEL are not case sensitive.\n\
75  Values may be abbreviated as indicated in upper case.\n\
76  If no alarm level specified all messages will be displayed.\n\n\
77  Log files specified by filename[s] will be displayed.\n\
78  If none are specified, all log files will be shown.\n");
79  getch(); //wait for keyboard input - avoids window disappearing
80  exit(0);
81  }
82  }
83  for (j=0,i=2; i<=argc; i++) {
84  if (strncasecmp(argv[i-1],"-n",2)==0) {
85  strcpy(level_str,argv[i++]);
86  if (strncasecmp(level_str,"DEB",3)==0) level = 0;
87  else if (strncasecmp(level_str,"NOT",3)==0) level = 1;
88  else if (strncasecmp(level_str,"WARN",4)==0) level = 2;
89  else if (strncasecmp(level_str,"ERR",3)==0) level = 3;
90  else if (strncasecmp(level_str,"OPER",4)==0) level = 4;
91  else if (strncasecmp(level_str,"CRIT",4)==0) level = 5;
92  else if (strncasecmp(level_str,"TERR",4)==0) level = 5;
93  else level = 0;
94  }
95  else {
96  strcpy(logfiles[j++],argv[i-1]);
97  }
98  }
99 
100 #ifdef DEBUG
101  if (j>0) logfiles[j][0] = 0;
102 
103  printf("level=%d\n",level);
104  j = 0;
105  while (logfiles[j][0] != 0) {
106  printf("filename[%d]: %s\n",j,logfiles[j++]);
107  }
108  printf("\n");
109 #endif
110 //==================finished parsing command line =================
111 
112  i = 0;
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  initscr();
125 
126  if(has_colors() == FALSE) {
127  endwin();
128  printf("This terminal does not support color: \n\
129 use /usr/dt/bin/dtterm -background white\n");
130  exit(1);
131  }
132 
133  scrollok(stdscr,TRUE);
134  start_color();
135  define_colors();
136  intrflush(stdscr,TRUE); // flush output after keyboard interrupt
137 
138  for(;;) {
139 
140  int inlevel;
141 
142  data_in = 0 ;
143  for(i=0;i<MAX_LOGFILES;i++) {
144 
145  if(logfiles[i][0]==0) continue ;
146 
147  errno = 0 ;
148  fret = fgets(buff,sizeof(buff),files[i]) ;
149  if(fret == NULL) {
150  if(errno) {
151  perror(logfiles[i]) ;
152  sleep(1) ;
153  continue ;
154  }
155  // let's check the file
156  ret = stat(logfiles[i],&statb) ;
157  if(ret < 0) {
158  perror(logfiles[i]) ;
159  sleep(1) ;
160  continue ;
161  }
162 
163  if(statb.st_size < oldsizes[i]) {
164  fclose(files[i]) ;
165  files[i] = fopen(logfiles[i],"r") ;
166  oldsizes[i] = 0 ;
167  continue ;
168  // reopen
169  }
170  oldsizes[i] = statb.st_size ;
171  continue ; // no data...
172  }
173 
174  data_in++ ; // we have something
175 
176  inlevel = 0;
177  if(strstr(buff,": DEBUG:") != NULL) inlevel=0;
178  else if(strstr(buff,": NOTICE:") != NULL) inlevel = 1;
179  else if(strstr(buff,": WARNING:") != NULL) inlevel = 2;
180  else if(strstr(buff,": ERROR:") != NULL) inlevel = 3;
181  else if(strstr(buff,": OPERATOR:") != NULL) inlevel = 4;
182  else if(strstr(buff,": TERR:") != NULL) inlevel = 5;
183  else if(strstr(buff,": CRITICAL:") != NULL) inlevel = 5;
184 
185  if (inlevel<level) continue;
186 
187  switch (inlevel) {
188  case 0:
189  attron(COLOR_PAIR(1));
190  break;
191  case 1:
192  attron(COLOR_PAIR(1));
193  break;
194  case 2:
195  attron(COLOR_PAIR(2));
196  break;
197  case 3:
198  attron(COLOR_PAIR(3));
199  attron(A_BOLD);
200  break;
201  case 4:
202  attron(COLOR_PAIR(4));
203  attron(A_BOLD);
204  break;
205  case 5:
206  attron(COLOR_PAIR(5));
207  attron(A_BOLD);
208  break;
209  default:
210  break;
211  }
212 
213  printw("%s",buff) ; // write the line
214  attron(COLOR_PAIR(1)); // back to black on white
215  attroff(A_BOLD); // turn off the bold
216  refresh(); // paint the screen
217  }
218 
219  if(!data_in) sleep(1) ;
220 
221  } // FOREVER
222 
223  endwin();
224  return -1 ; // UNREACHABLE
225 }
226 
227 
228 
229