-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathWriteToLogs.ahk
68 lines (53 loc) · 2.11 KB
/
WriteToLogs.ahk
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
WriteToLogs(fnActionText,fnDetailsText := "",fnLogFile := 0,fnErrorLogFile := 0,fnDebugFile := 0,fnActionTextWidth := 35)
{
; writes a text string to the provided log file objects
; MsgBox fnActionText: %fnActionText%`nfnDetailsText: %fnDetailsText%`nfnActionTextWidth: %fnActionTextWidth%
; MsgBox % "IsObject(fnLogFile): " IsObject(fnLogFile) "`nIsObject(fnErrorLogFile): " IsObject(fnErrorLogFile) "`nIsObject(fnDebugFile): " IsObject(fnDebugFile)
; MsgBox % "fnLogFile.__Handle: " fnLogFile.__Handle "`nfnErrorLogFile.__Handle: " fnErrorLogFile.__Handle "`nfnDebugFile.__Handle: " fnDebugFile.__Handle
; MsgBox % "fnLogFile address: " &fnLogFile "`nfnErrorLogFile address: " &fnErrorLogFile "`nfnDebugFile address: " &fnDebugFile
; declare local, global, static variables
Try
{
; set default return value
BytesWritten := 0
; validate parameters
; initialise variables
If !fnActionText
fnActionText := "Unspecified action"
; construct log text
Action := SubStr(fnActionText StrReplicate(" ",fnActionTextWidth),1,fnActionTextWidth)
LogText := TimeStampSQL(A_Now) "," Action "," fnDetailsText
; MsgBox Action: %Action%`nLogText: %LogText%
; write to logs
BytesWritten := 0
BytesWritten += fnLogFile.WriteLine(LogText)
BytesWritten += fnErrorLogFile.WriteLine(LogText)
BytesWritten += fnDebugFile.WriteLine(LogText)
OutputDebug, %A_ScriptName%: %LogText%
}
Catch, ThrownValue
{
ReturnValue := !ReturnValue
CatchHandler(A_ThisFunc,ThrownValue.Message,ThrownValue.What,ThrownValue.Extra,ThrownValue.File,ThrownValue.Line,0,0,0)
}
Finally
{
}
; return
Return BytesWritten
}
/* ; testing
TestFileName = %A_Desktop%\testfile.txt
TestFile := FileOpen(TestFileName,"w `n")
MsgBox, % TestFileName "`nIsObject: " IsObject(TestFile) "`nFileHandle: " TestFile.__Handle
LogDetails := "some other details and more"
ReturnValue := WriteToLogs("Test action",LogDetails,0,TestFile)
MsgBox, ReturnValue: %ReturnValue%
; TestFile.Close()
MsgBox, % TestFile.Position
TestFile.Position := 0
MsgBox, % TestFile.Position
Line := TestFile.Read()
MsgBox, % TestFile.Position
MsgBox, FileLine: `n%Line%
; */