欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Lex将log日志转换为HTML HTMLF#VBVB.NETEXT 

程序员文章站 2022-07-05 23:15:03
...

们经常在程序里添加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示如下

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 24pt; HEIGHT: 24pt" alt="" type="#_x0000_t75"><img alt="" src="http://blog.csdn.net/images/blog_csdn_net/tyrone1979/lex1.gif"></shape>