Wish Native way to write Log to file

ema01

Member
Licensed User
Longtime User
From time to time i need to dump logs to file, they are the same logs i use with the Log/LogColor functions.
So, in these cases i just create a couple of helper functions that just print the log and, if enable, write the line to a file.

The problem nuisance is that during a debug session clicking on the arrow on the log view will point at the helper function, not at the line that called the log
What could be added is another Log function that can enable/disable writing logs to a file
 

alwaysbusy

Expert
Licensed User
Longtime User
Not sure if it is this you are looking for but in my projects I always run this method to write the log to an external file:

Note, when used, it does not write it in the normal B4J log window anymore.
B4X:
' Redirects Output to a file
' Usage: RedirectOutput(File.DirApp, "logs.txt")
Sub RedirectOutputToFile (Dir As String, FileName As String)
    #if RELEASE
        Dim out As OutputStream = File.OpenOutput(Dir, FileName, True) 'Set to True to append the logs
        Dim ps As JavaObject
        ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
        Dim jo As JavaObject
        jo.InitializeStatic("java.lang.System")
        jo.RunMethod("setOut", Array(ps))
        jo.RunMethod("setErr", Array(ps))
    #end if
End Sub

' Redirects Output back to the (standard) screen
Sub RedirectOutputToScreen (Dir As String, FileName As String)
    #if RELEASE
        Dim out As OutputStream = File.OpenOutput(Dir, FileName, False) 'Set to True to append the logs
        Dim ps As JavaObject
        ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
        Dim fd As JavaObject
        fd.InitializeStatic("java.io.FileDescriptor")
        Dim jout As JavaObject
        jout.InitializeNewInstance("java.io.FileOutputStream", Array(fd.GetField("out")))
        ps.InitializeNewInstance("java.io.PrintStream", Array(jout, True, "utf8"))

        Dim jo As JavaObject
        jo.InitializeStatic("java.lang.System")
        jo.RunMethod("setOut", Array(ps))
        jo.RunMethod("setErr", Array(ps))
    #end if
End Sub

Alwaysbusy
 

AnandGupta

Expert
Licensed User
Longtime User
just create a couple of helper functions that just print the log and, if enable, write the line to a file.
Since actual b4x log() is in helper function, it will show there.
May be some call stack/trace will help, if any.
 
Top