00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <errno.h>
00010 #include <unistd.h>
00011 #include <strings.h>
00012 #include <string.h>
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <curses.h>
00016
00017 #define MAX_LOGFILES 128
00018
00019
00020
00021 static char buff[1024] ;
00022 static struct stat statb ;
00023
00024 static char level_str[] = "dummy string";
00025
00026 static char logfiles[MAX_LOGFILES][128] = {
00027 "/RTS/log/rts.log",
00028 "/RTS/log/trigger.log",
00029 "/RTS/log/evp.log",
00030 "/RTS/log/det.log",
00031 } ;
00032
00033 static FILE *files[MAX_LOGFILES] ;
00034
00035 static int oldsizes[MAX_LOGFILES] ;
00036
00037
00038
00039 void define_colors(){
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 init_pair(1,COLOR_BLACK,COLOR_WHITE);
00052 init_pair(2,COLOR_CYAN,COLOR_WHITE);
00053 init_pair(3,COLOR_MAGENTA,COLOR_WHITE);
00054 init_pair(4,COLOR_MAGENTA,COLOR_YELLOW);
00055 init_pair(5,COLOR_RED,COLOR_BLACK);
00056
00057 attron(COLOR_PAIR(1));
00058 bkgd(' ');
00059 }
00060
00061 int main(int argc, char *argv[])
00062 {
00063 char *fret ;
00064 int ret ;
00065 int i,j, level ;
00066 int data_in ;
00067
00068 for (i=2; i<=argc; i++) {
00069 if (strncmp(argv[i-1],"-h",2)==0) {
00070 initscr();
00071 printw("usage: logBrowser [-n ALARM_LEVEL] [file1 [,file2....]]\n\n\
00072 ALARM_LEVEL={DEBug,NOTice,WARNing,ERRor,OPERator,CRITical,TERR}\n\
00073 specifies the lowest level to be printed.\n\
00074 Values of ALARM_LEVEL are not case sensitive.\n\
00075 Values may be abbreviated as indicated in upper case.\n\
00076 If no alarm level specified all messages will be displayed.\n\n\
00077 Log files specified by filename[s] will be displayed.\n\
00078 If none are specified, all log files will be shown.\n");
00079 getch();
00080 exit(0);
00081 }
00082 }
00083 for (j=0,i=2; i<=argc; i++) {
00084 if (strncasecmp(argv[i-1],"-n",2)==0) {
00085 strcpy(level_str,argv[i++]);
00086 if (strncasecmp(level_str,"DEB",3)==0) level = 0;
00087 else if (strncasecmp(level_str,"NOT",3)==0) level = 1;
00088 else if (strncasecmp(level_str,"WARN",4)==0) level = 2;
00089 else if (strncasecmp(level_str,"ERR",3)==0) level = 3;
00090 else if (strncasecmp(level_str,"OPER",4)==0) level = 4;
00091 else if (strncasecmp(level_str,"CRIT",4)==0) level = 5;
00092 else if (strncasecmp(level_str,"TERR",4)==0) level = 5;
00093 else level = 0;
00094 }
00095 else {
00096 strcpy(logfiles[j++],argv[i-1]);
00097 }
00098 }
00099
00100 #ifdef DEBUG
00101 if (j>0) logfiles[j][0] = 0;
00102
00103 printf("level=%d\n",level);
00104 j = 0;
00105 while (logfiles[j][0] != 0) {
00106 printf("filename[%d]: %s\n",j,logfiles[j++]);
00107 }
00108 printf("\n");
00109 #endif
00110
00111
00112 i = 0;
00113 while(logfiles[i][0] != 0) {
00114
00115 files[i] = fopen(logfiles[i],"r") ;
00116
00117 if(files[i] == NULL) {
00118 perror(logfiles[i]) ;
00119 }
00120 i++ ;
00121 } ;
00122
00123
00124 initscr();
00125
00126 if(has_colors() == FALSE) {
00127 endwin();
00128 printf("This terminal does not support color: \n\
00129 use /usr/dt/bin/dtterm -background white\n");
00130 exit(1);
00131 }
00132
00133 scrollok(stdscr,TRUE);
00134 start_color();
00135 define_colors();
00136 intrflush(stdscr,TRUE);
00137
00138 for(;;) {
00139
00140 int inlevel;
00141
00142 data_in = 0 ;
00143 for(i=0;i<MAX_LOGFILES;i++) {
00144
00145 if(logfiles[i][0]==0) continue ;
00146
00147 errno = 0 ;
00148 fret = fgets(buff,sizeof(buff),files[i]) ;
00149 if(fret == NULL) {
00150 if(errno) {
00151 perror(logfiles[i]) ;
00152 sleep(1) ;
00153 continue ;
00154 }
00155
00156 ret = stat(logfiles[i],&statb) ;
00157 if(ret < 0) {
00158 perror(logfiles[i]) ;
00159 sleep(1) ;
00160 continue ;
00161 }
00162
00163 if(statb.st_size < oldsizes[i]) {
00164 fclose(files[i]) ;
00165 files[i] = fopen(logfiles[i],"r") ;
00166 oldsizes[i] = 0 ;
00167 continue ;
00168
00169 }
00170 oldsizes[i] = statb.st_size ;
00171 continue ;
00172 }
00173
00174 data_in++ ;
00175
00176 inlevel = 0;
00177 if(strstr(buff,": DEBUG:") != NULL) inlevel=0;
00178 else if(strstr(buff,": NOTICE:") != NULL) inlevel = 1;
00179 else if(strstr(buff,": WARNING:") != NULL) inlevel = 2;
00180 else if(strstr(buff,": ERROR:") != NULL) inlevel = 3;
00181 else if(strstr(buff,": OPERATOR:") != NULL) inlevel = 4;
00182 else if(strstr(buff,": TERR:") != NULL) inlevel = 5;
00183 else if(strstr(buff,": CRITICAL:") != NULL) inlevel = 5;
00184
00185 if (inlevel<level) continue;
00186
00187 switch (inlevel) {
00188 case 0:
00189 attron(COLOR_PAIR(1));
00190 break;
00191 case 1:
00192 attron(COLOR_PAIR(1));
00193 break;
00194 case 2:
00195 attron(COLOR_PAIR(2));
00196 break;
00197 case 3:
00198 attron(COLOR_PAIR(3));
00199 attron(A_BOLD);
00200 break;
00201 case 4:
00202 attron(COLOR_PAIR(4));
00203 attron(A_BOLD);
00204 break;
00205 case 5:
00206 attron(COLOR_PAIR(5));
00207 attron(A_BOLD);
00208 break;
00209 default:
00210 break;
00211 }
00212
00213 printw("%s",buff) ;
00214 attron(COLOR_PAIR(1));
00215 attroff(A_BOLD);
00216 refresh();
00217 }
00218
00219 if(!data_in) sleep(1) ;
00220
00221 }
00222
00223 endwin();
00224 return -1 ;
00225 }
00226
00227
00228
00229