使用Lex将log日志转换为HTML

来源:互联网 发布:linux查看iscsi盘符 编辑:程序博客网 时间:2024/05/18 01:54

们经常在程序里添加log, 用于在维护时查找异常时间和地点。通常log文件内容很多,将log文件格式化为带颜色不同字体的html可以方便阅读

例如VB目中log如下:


===========================================  START  DBS  ===========================================
DEBUG_MODE_OFF
ETC_OCX_OFF
LOG_LEVEL = 2
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBDBS114
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBDBS114//PSBDBS11413.log
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBIMG003
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBIMG003//PSBIMG00313.log
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBIMC104
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBIMC104//PSBIMC10413.log
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBDBA106
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBDBA106//PSBDBA10613.log
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始 ***

省略

===========================================  START  DBS  ===========================================表示一log

2006/04/12 07:59:06log记载时间

basDBSCommon(DelLogFile)ModuleFunction

*** 開始 ***函数始;*** 終了 ***函数

1. 使用Lex 加行数

非常简单,而且非常常

/*****************************************************************************
 *
 * Copyright (C) 2006 by Zhao Lei
 *
 */
 
%{

/*
 * includes
 */
#include <stdio.h>
#include <stdlib.h>
int lineno=1;

%}

LINE   .*/n

%%

{LINE}        { printf( "%5d %s", lineno++, yytext ); }

%%

main(){
    yylex();
    return 0;
}

编译运行addline.exe <PSBDBS11412.LOG >source.txt

果如下:

    1
    2 ===========================================  START  DBS  ===========================================
    3 DEBUG_MODE_OFF
    4 ETC_OCX_OFF
    5 LOG_LEVEL = 2
    6 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
    7 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBDBS114
    8 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
    9 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBDBS114//PSBDBS11413.log
   10 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
   11 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
   12 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
   13 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBIMG003
   14 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
   15 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBIMG003//PSBIMG00313.log
   16 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
   17 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
   18 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
   19 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBIMC104
   20 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
   21 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBIMC104//PSBIMC10413.log
   22 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
   23 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
   24 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
   25 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBDBA106
   26 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
   27 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBDBA106//PSBDBA10613.log
   28 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了
***
   29 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
終了
***
   30 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):***
開始
***
   31 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DelLogFile):strAppName = PSBDBC110
   32 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
開始
***
   33 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):strFileName = D:/logs/batch/sb/PSBDBC110//PSBDBC11013.log
   34 2006/04/12 07:59:06 -------- ------- I 1 ------- basDBSCommon(DeleteFile):***
終了 ***

省略

2. 转换为HTML

将已加上行数的log转换为HTMLLex如下:


/*
 * includes
 */
#include <stdio.h>
#include <stdlib.h>
int section=1;
%}

LINE           /n
TITLELINE  .*("=")+(" ")+START(" ")+.+(" ")+("=")+  
DIGITAL      [0-9]
LEVEL        [0-9]
TIME          200[0-9]"/"[0-1][0-9]"/"[0-1][0-9]" "[0-2][0-9]":"[0-5][0-9]":"[0-5][0-9]  //
时间的正表达式

LOG_LEVEL .*LOG_LEVEL" ""="" "{LEVEL}
MESSAGE   I|W|E
TRUE       "True"
FALSE     "False"
FUNCTION  "(".+")"
START     "*** ŠJŽn ***"
END         "*** I
—¹ ***"
STAT       "***".+"***"
ERROR    ."★★★ ERROR ★★★"|"★★★★★
 異常終了 ★★★★★"

MENUS     "-""-"+
ONE         " "+1" "+
OTHERS   .*
SIMPLE         {TIME}{MENUS}{MESSAGE}{ONE}{OTHERS}{FUNCTION}":"
%%
{TITLELINE}  { printf("</div><p></p><div style=/"background-color: #CCFFFF; layer-background-color: #CCFFFF; border: 1px none #000000;/"><p>section:<b>%d</b></p>",section++);}
{MENUS}       { printf("");}
{ERROR}        { printf("<font color=/"#FF0000/">"); ECHO;printf("</font>");}
{DIGITAL}       { printf("<font color=/"#0033FF/">"); ECHO; printf("</font>");}
{TIME}          { printf("<em>"); ECHO; printf("</em>");}
{LINE}            { ECHO; printf("<br>");}
{TRUE}|{FALSE}     { printf("<font color=/"#FF0099/">"); ECHO; printf("</font>");}
{FUNCTION}   { printf("<b>"); ECHO; printf("</b>");}
{STAT}       { printf("<b><font color=/"#00FF00/">"); ECHO; printf("</font></b>");}

%%
main(void){
 printf("<html><body>/n");
 yylex();
 printf("/n</html></body>");
 return 0;
}

编译后,运行:

DBSLog.exe < source.txt > dest.htm

dest.htm示如下

原创粉丝点击