Top > OOoBasic > Generic > Logging

ログ Edit

OOo Basic からログを出力します。Basic にはコンソールへの出力方法がありませんが、この方法を利用すると拡張機能などを利用せずにコンソールにログを出力できます。ダイアログ利用時やリスナーなどから msgbox を利用したくないときなどに便利です。

Windows 環境ではコンソールに出力できません。

下記のコードでコンソールまたはファイルへログを出力します。nLogIntoFile_Logging を 1 に変更するとファイルに出力します。

ログの出力形式は CustomLogFormatter_format 関数の返り値を変更してカスタマイズできます。

nThreshold_Logging 以上のレベルのログが表示されます。ログレベルは css.logging.LogLevel の定数で指定します。エラー出力用でなく情報表示用に利用するのであれば 800 に指定します。

Sub Main
 Logg("Log message.", 900)
 Logg("mes3")
 Logg("mes4", 900)
End Sub
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'//-- Customizable Logging function
 
' object for logging
Dim oLogger_Logging As Object
Dim nSequenceNumber_Logging As Long
 
' threshold to show log message
Const nThreshold_Logging = 900 'com.sun.star.logging.LogLevel.WARNING
Const nDefaultLogLevel_Logging = 800 'com.sun.star.logging.LogLevel.INFO
Const sDefaultEncoding_Logging = "utf-8"
Const sLoggerName_Logging = "OOo Basic"
 
' for file output
Const nLogIntoFile_Logging = 0 ' 0 for console output, 1 for file output
' specify a file URL to write, substituted by css.util.PathSubstitution during initialization
Const sFileURL_Logging = "$(user)/temp/basic.log"
 
' to log to standard error using css.logging.ConsoleHandler.
'   sMessage: message string to show information about the situation
'   nLogLevel: one of a member of css.logging.LogLevel constants
'   sSourceClassName: Like a module name, optional
'   sSourceMethodName: sub or function name, optional
Sub Logg(Optional sMessage As String, Optional nLogLevel As Long, _
        Optional sSourceClassName As String, Optional sSourceMethodName As String)
  If IsNull(oLogger_Logging) Then
    ' create new logger
    nSequenceNumber_Logging = 0
    
    If nLogIntoFile_Logging = 0 Then
      ' for consol output
      oLogger_Logging = CreateUnoService("com.sun.star.logging.ConsoleHandler")
      
      oLogFormatter_Logging = CreateUnoListener("CustomLogFormatter_", _
         "com.sun.star.logging.XLogFormatter")
      
      Dim aArgs_Logging(2) As New com.sun.star.beans.NamedValue
      aArgs_Logging(0).Name = "Formatter"
      aArgs_Logging(0).Value = oLogFormatter_Logging
      aArgs_Logging(1).Name = "Encoding"
      aArgs_Logging(1).Value = sDefaultEncoding_Logging
      aArgs_Logging(2).Name = "Level"
      aArgs_Logging(2).Value = nThreshold_Logging
      
      oLogger_Logging.initialize(Array(aArgs_Logging))
      oLogger_Logging.Threshold = nThreshold_Logging
    Else
      ' for file output
      oLogger_Logging = CreateUnoService("com.sun.star.logging.FileHandler")
      
      oLogFormatter_Logging = CreateUnoListener("CustomLogFormatter_", _
         "com.sun.star.logging.XLogFormatter")
      
      Dim aArgs1_Logging(3) As New com.sun.star.beans.NamedValue
      aArgs1_Logging(0).Name = "Formatter"
      aArgs1_Logging(0).Value = oLogFormatter_Logging
      aArgs1_Logging(1).Name = "Encoding"
      aArgs1_Logging(1).Value = sDefaultEncoding_Logging
      aArgs1_Logging(2).Name = "Level"
      aArgs1_Logging(2).Value = nThreshold_Logging
      aArgs1_Logging(3).Name = "FileURL"
      aArgs1_Logging(3).Value = sFileURL_Logging
      
      oLogger_Logging.initialize(Array(aArgs1_Logging))
    End If
    
  End If
  If NOT IsNull(oLogger_Logging) Then
    If IsMissing(nLogLevel) Then nLogLevel = nDefaultLogLevel_Logging
    If IsMissing(sSourceClassName) Then sSourceClassName = ""
    If IsMissing(sSourceMethodName) Then sSourceMethodName = ""
    oLogger_Logging.publish( _
        LogEntry_Logging(sMessage, nLogLevel, _
            sSourceClassName, sSourceMethodName, nSequenceNumber_Logging))
    oLogger_Logging.flush() ' force output
    nSequenceNumber_Logging = nSequenceNumber_Logging + 1
  End If
End Sub
 
 
' create new log entry
' nLogLevel: specified one of entry from constant group of css.logging.LogLevel
Function LogEntry_Logging(sMessage As String, nLogLevel As Long, _
        sSourceClassName As String, sSourceMethodName As String, _
        nSequenceNumber As Long)
  aRecord = CreateUnoStruct("com.sun.star.logging.LogRecord")
  aDateTime = CreateUnoStruct("com.sun.star.util.DateTime")
  vNow = Now()
  With aDateTime
    .Year = Year(vNow)
    .Month = Month(vNow)
    .Day = Day(vNow)
    .Hours = Hour(vNow)
    .Minutes = Minute(vNow)
    .Seconds = Second(vNow)
  End With
  With aRecord
    .LoggerName = sLoggerName
    .SourceClassName = sSourceClassName
    .SourceMethodName = sSourceMethodName
    .Message = sMessage
    .LogTime = aDateTime
    .SequenceNumber = nSequenceNumber
    .ThreadID = 0
    .Level = nLogLevel
  End With
  
  LogEntry_Logging = aRecord
End Function
 
 
' formatting log output
' you can customize output string. 
' Append line feed or carrige return at the end of the line.
Function CustomLogFormatter_format(aRecord As com.sun.star.logging.LogRecord) As String
  CustomLogFormatter_format = FormatDate_Logging(aRecord.LogTime) & _
      " " & aRecord.Message & chr(10)
End Function
 
' they are not called from the console handler
Function CustomLogFormatter_getHead()
  CustomLogFormatter_getHead = ""
End Function
Function CustomLogFormatter_getTail()
  CustomLogFormatter_getTail = ""
End Function
Function CustomLogFormatter_disposing()
End Function
 
 
' to make date and time format
Function FormatDate_Logging(aDateTime As com.sun.star.util.DateTime) As String
  FormatDate_Logging = "[" & Format(aDateTime.Year, "0000-") & _
    Format(aDateTime.Month, "00-") & Format(aDateTime.Day, "00 ") & _
      Format(aDateTime.Hours, "00:") & Format(aDateTime.Minutes, "00:")  & _
    Format(aDateTime.Seconds, "00") & "]"
End Function
 
'//--

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes