- package org.apache.pivot.scala.log
-
- import scala.reflect.BeanProperty
- import io.Source
- import org.apache.pivot.wtk.content.ListViewItemRenderer
- import java.lang.String
- import org.apache.pivot.wtkx.{WTKX, WTKXSerializer}
-
- /*Z避免和scala.Application的名U冲H,q里修改了别?/
- import org.apache.pivot.wtk.{ Application => PivotApplication, _}
- import org.apache.pivot.collections.{ArrayList, Map}
-
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2010-8-26
- * Time: 10:36:45
- * To change this template use File | Settings | File Templates.
- */
-
- /*日志记录Bean对象,׃Scala标准生成的字D不使用getter/setter的格式,
- 但是提供了注?nbsp;@scala.reflect.BeanProperty Q得编译器可以生成标准的JavaBean对象的getter/setter接口
- val 只生成了getter
- var 同时生成了getter和setter */
- class LogRecord ( @BeanProperty val threadName : String,
- @BeanProperty val date : String,
- @BeanProperty val time : String,
- @BeanProperty val module : String,
- @BeanProperty val level : String,
- @BeanProperty val content : String){
-
- /*
- 重蝲?nbsp;Any?nbsp;toString接口
- override关键字是必须的,在java中用的是注?nbsp;@override,但是java中@overrideq不是必ȝ?nbsp;
- 省略?nbsp;函数q回|q译器q行cd推演得到
- */
- override def toString() = {
- threadName +" "+date +" "+ time +" "+module +" "+level+" "+content
- }
-
- }
-
- /*
- LogRecord cȝ半生对象
- 定义?nbsp;scala中的术接口 apply Q得可以?nbsp; LogRecord("sting 文本") 可以创徏一个class LogRecord对象?nbsp;
- applyҎ的调用由~译器自动调用,我们只负责定义即?nbsp;
- 我们要解析的日志格式
-
- threadName date time module Level content
- DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - Debug level: 2
- DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - *=ERROR WARNING EXCEPT
- DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - LM=*
-
- */
- object LogRecord {
- def apply( line : String ) : LogRecord = {
-
- /*生成一?nbsp;Regex对象Q用于模式匹?/
- val logRegex = """([A-Za-z0-9]+) +([0-9/]*) +([0-9:]*) +([A-Z]*) +: *([A-Z_]+).*""".r
-
- line match {
- /*如果模式匚w成功QthreadName,date,time,module,level 分配按次序绑定到模式匚w表达式中()的内?nbsp;*/
- case logRegex(threadName,date,time,module,level) =>
- val logRecord: LogRecord = new LogRecord( threadName, date, time, module, level,line)
- logRecord
- /*模式匚w通配W,在没有匹配到的情况下Q做通用处理。如果不dQ在匚w不到时会抛出 MatchError异常*/
- case _ =>
- val logRecord: LogRecord = new LogRecord("N/A","N/A","N/A","N/A","N/A","N/A")
- logRecord
- }
-
- }
- }
-
- /*
- Apache Pivot ListView ItemRenderer
- 重新定义了如何显C?nbsp;LogRecord对象?nbsp;列表目的渲染?nbsp;
- 如果使用默认的,那么ListView昄对象Ӟ直接调用对象的toString获得其字W串表示
- */
- class LogListViewItemRenderer extends ListViewItemRenderer {
-
- imageView.setVisible(false)
-
- override def render(item: AnyRef, index: Int, listView: ListView, selected: Boolean, checked: Boolean, highlighted: Boolean, disabled: Boolean) = {
- if ( item != null && item.isInstanceOf[LogRecord])
- {
- val log = item.asInstanceOf[LogRecord]
- label.setText(log.content)
-
-
- }
- }
-
-
-
- }
-
- /**
- 定义ȝ口界面代?必须l承?nbsp;org.apache.pivot.Application
- 使用?nbsp;@WTKX注解Q该注解属于 wtkx的命名对象的l定语法Q在从wtkx文g加蝲GUIӞ
- 调用bind接口可以自动和wtkx文g中声明的wtkx:id对象q行l定Q无需在掉吗中调用 get("name")
- */
- class MainWindow extends PivotApplication {
- var window : Window = null
- @WTKX var textInputFilePath : TextInput = null
- @WTKX var browsePushButton : PushButton = null
- @WTKX var loadPushButton : PushButton = null
- @WTKX var textInputThreadName :TextInput = null
- @WTKX var textInputModule : TextInput = null
- @WTKX var textInputLevel : TextInput = null
- @WTKX var textInputContent : TextInput = null
- @WTKX var logListView : ListView = null
-
-
- def resume = {}
-
- def suspend = {}
-
- def shutdown(optional: Boolean) = {
- if ( window != null)
- {
- window.close
- true
- }
- false
- }
-
- def startup(display: Display, properties: Map[String, String]) = {
- val wtkxSerializer = new WTKXSerializer()
- var matchString : String = null
-
- /*从xml(wtkx)文g加蝲GUI*/
- window = wtkxSerializer.readObject(this,"MainWindow.xml").asInstanceOf[Window]
-
- wtkxSerializer.bind(this)
- if ( properties containsKey "logfile")
- {
- textInputFilePath setText ( properties get "logfile")
- }
-
- /*l?nbsp;Buttond事g处理函数*/
- browsePushButton.getButtonPressListeners.add( function2Listener (browseButtonPressed ) )
- loadPushButton.getButtonPressListeners.add( function2Listener(loadButtonPressed ))
-
-
- window.open(display)
-
- }
-
- /*览按钮事g处理Q打开一个文件浏览窗口,让用户选择文gQƈ在用户关闭对话框Ӟ捕获用户选择的文件名*/
- def browseButtonPressed( button : Button ) : Unit = {
- val dialog : FileBrowserSheet = new FileBrowserSheet(FileBrowserSheet.Mode.OPEN)
-
- dialog.open( window, new SheetCloseListener() {
- def sheetClosed(sheet: Sheet) = {
- if ( sheet.getResult)
- {
- val fileBrowseSheet = sheet.asInstanceOf[FileBrowserSheet]
- textInputFilePath.setText( fileBrowseSheet.getSelectedFile.getPath.toString)
- }
- }
- })
- }
-
- /*从log文g加蝲内容Q每一行是一个日志记?nbsp;
- for中用了 ifqo器,只有条gW合了,才会q入for循环体?nbsp;
-
- scala没有提供continuQ而是?nbsp;for中提供了条gqo来替?nbsp;
- */
- def loadButtonPressed( button : Button ) : Unit = {
- val logFile = Source.fromFile(textInputFilePath.getText)
- val list = new ArrayList[LogRecord]
- for ( line <- logFile.getLines ; logRecord = LogRecord(line.trim);
- if ( textInputThreadName.getText == "" || textInputThreadName.getText.contains(logRecord.threadName) );
- if ( textInputModule.getText == "" || textInputModule.getText.contains(logRecord.module));
- if ( textInputLevel.getText == "" || textInputLevel.getText.contains(logRecord.level))){
-
- list add logRecord
- }
-
- logListView.setListData( list)
-
- }
- /*按钮事g辅助接口Q用于把一?nbsp;事g处理函数转换为ButtonPressListener对象Q也可以采取更高U的内容Q用implicit,q里没有使用*/
- def function2Listener( fun : Button => Unit ) :ButtonPressListener = {
- val listener = new ButtonPressListener()
- {
- def buttonPressed(button: Button) = {
- fun(button)
- }
- }
-
- listener
- }
- }
-
- /*
- d?nbsp;
- W合Pivotd数入口规?nbsp;
- */
- object LogAnalyse {
- def main ( args : Array[String]) : Unit = {
-
-
- DesktopApplicationContext.main( classOf[MainWindow], args)
-
-
- }
- }
package org.apache.pivot.scala.log
import scala.reflect.BeanProperty
import io.Source
import org.apache.pivot.wtk.content.ListViewItemRenderer
import java.lang.String
import org.apache.pivot.wtkx.{WTKX, WTKXSerializer}
/*Z避免和scala.Application的名U冲H,q里修改了别?/
import org.apache.pivot.wtk.{ Application => PivotApplication, _}
import org.apache.pivot.collections.{ArrayList, Map}
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2010-8-26
* Time: 10:36:45
* To change this template use File | Settings | File Templates.
*/
/*日志记录Bean对象,׃Scala标准生成的字D不使用getter/setter的格式,
但是提供了注?@scala.reflect.BeanProperty Q得编译器可以生成标准的JavaBean对象的getter/setter接口
val 只生成了getter
var 同时生成了getter和setter */
class LogRecord ( @BeanProperty val threadName : String,
@BeanProperty val date : String,
@BeanProperty val time : String,
@BeanProperty val module : String,
@BeanProperty val level : String,
@BeanProperty val content : String){
/*
重蝲?Any?toString接口
override关键字是必须的,在java中用的是注?@override,但是java中@overrideq不是必ȝ?
省略?函数q回|q译器q行cd推演得到
*/
override def toString() = {
threadName +" "+date +" "+ time +" "+module +" "+level+" "+content
}
}
/*
LogRecord cȝ半生对象
定义?scala中的术接口 apply Q得可以? LogRecord("sting 文本") 可以创徏一个class LogRecord对象?
applyҎ的调用由~译器自动调用,我们只负责定义即?
我们要解析的日志格式
threadName date time module Level content
DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - Debug level: 2
DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - *=ERROR WARNING EXCEPT
DVOSMAIN 08/26/10 10:17:17 LOGINFO : INFO - LM=*
*/
object LogRecord {
def apply( line : String ) : LogRecord = {
/*生成一?Regex对象Q用于模式匹?/
val logRegex = """([A-Za-z0-9]+) +([0-9/]*) +([0-9:]*) +([A-Z]*) +: *([A-Z_]+).*""".r
line match {
/*如果模式匚w成功QthreadName,date,time,module,level 分配按次序绑定到模式匚w表达式中()的内?*/
case logRegex(threadName,date,time,module,level) =>
val logRecord: LogRecord = new LogRecord( threadName, date, time, module, level,line)
logRecord
/*模式匚w通配W,在没有匹配到的情况下Q做通用处理。如果不dQ在匚w不到时会抛出 MatchError异常*/
case _ =>
val logRecord: LogRecord = new LogRecord("N/A","N/A","N/A","N/A","N/A","N/A")
logRecord
}
}
}
/*
Apache Pivot ListView ItemRenderer
重新定义了如何显C?LogRecord对象?列表目的渲染?
如果使用默认的,那么ListView昄对象Ӟ直接调用对象的toString获得其字W串表示
*/
class LogListViewItemRenderer extends ListViewItemRenderer {
imageView.setVisible(false)
override def render(item: AnyRef, index: Int, listView: ListView, selected: Boolean, checked: Boolean, highlighted: Boolean, disabled: Boolean) = {
if ( item != null && item.isInstanceOf[LogRecord])
{
val log = item.asInstanceOf[LogRecord]
label.setText(log.content)
}
}
}
/**
定义ȝ口界面代?必须l承?org.apache.pivot.Application
使用?@WTKX注解Q该注解属于 wtkx的命名对象的l定语法Q在从wtkx文g加蝲GUIӞ
调用bind接口可以自动和wtkx文g中声明的wtkx:id对象q行l定Q无需在掉吗中调用 get("name")
*/
class MainWindow extends PivotApplication {
var window : Window = null
@WTKX var textInputFilePath : TextInput = null
@WTKX var browsePushButton : PushButton = null
@WTKX var loadPushButton : PushButton = null
@WTKX var textInputThreadName :TextInput = null
@WTKX var textInputModule : TextInput = null
@WTKX var textInputLevel : TextInput = null
@WTKX var textInputContent : TextInput = null
@WTKX var logListView : ListView = null
def resume = {}
def suspend = {}
def shutdown(optional: Boolean) = {
if ( window != null)
{
window.close
true
}
false
}
def startup(display: Display, properties: Map[String, String]) = {
val wtkxSerializer = new WTKXSerializer()
var matchString : String = null
/*从xml(wtkx)文g加蝲GUI*/
window = wtkxSerializer.readObject(this,"MainWindow.xml").asInstanceOf[Window]
wtkxSerializer.bind(this)
if ( properties containsKey "logfile")
{
textInputFilePath setText ( properties get "logfile")
}
/*l?Buttond事g处理函数*/
browsePushButton.getButtonPressListeners.add( function2Listener (browseButtonPressed ) )
loadPushButton.getButtonPressListeners.add( function2Listener(loadButtonPressed ))
window.open(display)
}
/*览按钮事g处理Q打开一个文件浏览窗口,让用户选择文gQƈ在用户关闭对话框Ӟ捕获用户选择的文件名*/
def browseButtonPressed( button : Button ) : Unit = {
val dialog : FileBrowserSheet = new FileBrowserSheet(FileBrowserSheet.Mode.OPEN)
dialog.open( window, new SheetCloseListener() {
def sheetClosed(sheet: Sheet) = {
if ( sheet.getResult)
{
val fileBrowseSheet = sheet.asInstanceOf[FileBrowserSheet]
textInputFilePath.setText( fileBrowseSheet.getSelectedFile.getPath.toString)
}
}
})
}
/*从log文g加蝲内容Q每一行是一个日志记?
for中用了 ifqo器,只有条gW合了,才会q入for循环体?
scala没有提供continuQ而是?for中提供了条gqo来替?
*/
def loadButtonPressed( button : Button ) : Unit = {
val logFile = Source.fromFile(textInputFilePath.getText)
val list = new ArrayList[LogRecord]
for ( line <- logFile.getLines ; logRecord = LogRecord(line.trim);
if ( textInputThreadName.getText == "" || textInputThreadName.getText.contains(logRecord.threadName) );
if ( textInputModule.getText == "" || textInputModule.getText.contains(logRecord.module));
if ( textInputLevel.getText == "" || textInputLevel.getText.contains(logRecord.level))){
list add logRecord
}
logListView.setListData( list)
}
/*按钮事g辅助接口Q用于把一?事g处理函数转换为ButtonPressListener对象Q也可以采取更高U的内容Q用implicit,q里没有使用*/
def function2Listener( fun : Button => Unit ) :ButtonPressListener = {
val listener = new ButtonPressListener()
{
def buttonPressed(button: Button) = {
fun(button)
}
}
listener
}
}
/*
d?
W合Pivotd数入口规?
*/
object LogAnalyse {
def main ( args : Array[String]) : Unit = {
DesktopApplicationContext.main( classOf[MainWindow], args)
}
}
runQ?/p>
使用 javaӞ
java -classpath
scala-library.jar;pivot-core-1.5.jar;pivot-tools-1.5.jar;pivot-wtk-1.5.jar;pivot-wtk-terra-1.5.jar;.
org.apache.pivot.scala.log.LogAnalyse
使用 scala?/p>
java -classpath
pivot-core-1.5.jar;pivot-tools-1.5.jar;pivot-wtk-1.5.jar;pivot-wtk-terra-1.5.jar;.
org.apache.pivot.scala.log.LogAnalyse