- 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}
-
- /*為了避免和scala.Application的名稱沖突,這里修改了別名*/
- 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標準生成的字段不使用getter/setter的格式,
- 但是提供了注解 @scala.reflect.BeanProperty ,使得編譯器可以生成標準的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中@override并不是必須的。
- 省略了 函數還回值,由編譯器進行類型推演得到
- */
- override def toString() = {
- threadName +" "+date +" "+ time +" "+module +" "+level+" "+content
- }
-
- }
-
- /*
- LogRecord 類的半生對象
- 定義了 scala中的魔術接口 apply ,使得可以使用 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對象,用于模式匹配*/
- val logRegex = """([A-Za-z0-9]+) +([0-9/]*) +([0-9:]*) +([A-Z]*) +: *([A-Z_]+).*""".r
-
- line match {
- /*如果模式匹配成功,threadName,date,time,module,level 分配按次序綁定到模式匹配表達式中()的內容 */
- case logRegex(threadName,date,time,module,level) =>
- val logRecord: LogRecord = new LogRecord( threadName, date, time, module, level,line)
- logRecord
- /*模式匹配通配符,在沒有匹配到的情況下,做通用處理。如果不添加,在匹配不到時會拋出 MatchError異常*/
- case _ =>
- val logRecord: LogRecord = new LogRecord("N/A","N/A","N/A","N/A","N/A","N/A")
- logRecord
- }
-
- }
- }
-
- /*
- Apache Pivot ListView ItemRenderer
- 重新定義了如何顯示 LogRecord對象的 列表項目的渲染。
- 如果使用默認的,那么ListView顯示對象時,直接調用對象的toString獲得其字符串表示
- */
- 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)
-
-
- }
- }
-
-
-
- }
-
- /**
- 定義主窗口界面代碼,必須繼承自 org.apache.pivot.Application
- 使用了 @WTKX注解,該注解屬于 wtkx的命名對象的綁定語法,在從wtkx文件加載GUI時,
- 調用bind接口可以自動和wtkx文件中聲明的wtkx:id對象進行綁定,無需在掉嗎中調用 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)文件加載GUI*/
- window = wtkxSerializer.readObject(this,"MainWindow.xml").asInstanceOf[Window]
-
- wtkxSerializer.bind(this)
- if ( properties containsKey "logfile")
- {
- textInputFilePath setText ( properties get "logfile")
- }
-
- /*給 Button添加事件處理函數*/
- browsePushButton.getButtonPressListeners.add( function2Listener (browseButtonPressed ) )
- loadPushButton.getButtonPressListeners.add( function2Listener(loadButtonPressed ))
-
-
- window.open(display)
-
- }
-
- /*瀏覽按鈕事件處理,打開一個文件瀏覽窗口,讓用戶選擇文件,并在用戶關閉對話框時,捕獲用戶選擇的文件名*/
- 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文件加載內容,每一行是一個日志記錄
- for中使用了 if過濾器,只有條件符合了,才會進入for循環體。
-
- scala沒有提供continu,而是在 for中提供了條件過濾來替代
- */
- 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)
-
- }
- /*按鈕事件輔助接口,用于把一個 事件處理函數轉換為ButtonPressListener對象,也可以采取更高級的內容,使用implicit,這里沒有使用*/
- def function2Listener( fun : Button => Unit ) :ButtonPressListener = {
- val listener = new ButtonPressListener()
- {
- def buttonPressed(button: Button) = {
- fun(button)
- }
- }
-
- listener
- }
- }
-
- /*
- 主函數
- 符合Pivot主函數入口規則
- */
- 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}
/*為了避免和scala.Application的名稱沖突,這里修改了別名*/
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標準生成的字段不使用getter/setter的格式,
但是提供了注解 @scala.reflect.BeanProperty ,使得編譯器可以生成標準的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中@override并不是必須的。
省略了 函數還回值,由編譯器進行類型推演得到
*/
override def toString() = {
threadName +" "+date +" "+ time +" "+module +" "+level+" "+content
}
}
/*
LogRecord 類的半生對象
定義了 scala中的魔術接口 apply ,使得可以使用 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對象,用于模式匹配*/
val logRegex = """([A-Za-z0-9]+) +([0-9/]*) +([0-9:]*) +([A-Z]*) +: *([A-Z_]+).*""".r
line match {
/*如果模式匹配成功,threadName,date,time,module,level 分配按次序綁定到模式匹配表達式中()的內容 */
case logRegex(threadName,date,time,module,level) =>
val logRecord: LogRecord = new LogRecord( threadName, date, time, module, level,line)
logRecord
/*模式匹配通配符,在沒有匹配到的情況下,做通用處理。如果不添加,在匹配不到時會拋出 MatchError異常*/
case _ =>
val logRecord: LogRecord = new LogRecord("N/A","N/A","N/A","N/A","N/A","N/A")
logRecord
}
}
}
/*
Apache Pivot ListView ItemRenderer
重新定義了如何顯示 LogRecord對象的 列表項目的渲染。
如果使用默認的,那么ListView顯示對象時,直接調用對象的toString獲得其字符串表示
*/
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)
}
}
}
/**
定義主窗口界面代碼,必須繼承自 org.apache.pivot.Application
使用了 @WTKX注解,該注解屬于 wtkx的命名對象的綁定語法,在從wtkx文件加載GUI時,
調用bind接口可以自動和wtkx文件中聲明的wtkx:id對象進行綁定,無需在掉嗎中調用 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)文件加載GUI*/
window = wtkxSerializer.readObject(this,"MainWindow.xml").asInstanceOf[Window]
wtkxSerializer.bind(this)
if ( properties containsKey "logfile")
{
textInputFilePath setText ( properties get "logfile")
}
/*給 Button添加事件處理函數*/
browsePushButton.getButtonPressListeners.add( function2Listener (browseButtonPressed ) )
loadPushButton.getButtonPressListeners.add( function2Listener(loadButtonPressed ))
window.open(display)
}
/*瀏覽按鈕事件處理,打開一個文件瀏覽窗口,讓用戶選擇文件,并在用戶關閉對話框時,捕獲用戶選擇的文件名*/
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文件加載內容,每一行是一個日志記錄
for中使用了 if過濾器,只有條件符合了,才會進入for循環體。
scala沒有提供continu,而是在 for中提供了條件過濾來替代
*/
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)
}
/*按鈕事件輔助接口,用于把一個 事件處理函數轉換為ButtonPressListener對象,也可以采取更高級的內容,使用implicit,這里沒有使用*/
def function2Listener( fun : Button => Unit ) :ButtonPressListener = {
val listener = new ButtonPressListener()
{
def buttonPressed(button: Button) = {
fun(button)
}
}
listener
}
}
/*
主函數
符合Pivot主函數入口規則
*/
object LogAnalyse {
def main ( args : Array[String]) : Unit = {
DesktopApplicationContext.main( classOf[MainWindow], args)
}
}
run:
使用 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時
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
|