Wish Custom Log Routines

JohnC

Expert
Licensed User
Longtime User
As mentioned in this thread (https://www.b4x.com/android/forum/threads/log-colors-default-for-an-app.113062/), I also wanted to create a more robust "Log" function, but as mentioned, it would hide the real/original line number in the log listing which will break the "Click to go to code line" feature to not work properly.

So, would it be possible to instead have a reserved variable like "#CallingCodeLine" that will contain the line number within the procedure that called the current sub.

Then, we could create a custom log function like below and still have the "Click to go to code" work properly:

B4X:
Sub Main
    Activity.LoadLayout("main")
    MyLog("Main Activity Loaded", Colors.Black)
End Sub

Sub MyLog(LogMessage As String, LogColor As Int) As String
    Dim CL As Object
    Dim LT As String
    Dim L As String

    CL = #CallingCodeLine   'I am saving the original line number here in case this sub needs to call other subs which would update the @CallingCodeLine value and overwrite the original line number
    LT = LogTime
    L = LT & ": " & LogMessage

    'here you can save L to disk so remote users can submit the log file(s) for diagnostics

    LogColor(L, LogColor, CL)    'CL contains caling line number so "Click to go to code" will properly go to line "3" in above sub "Main"

    'which will create a log entry like: "03:34:92.1356 Main Activity Loaded"

End Sub

Sub LogTime() As String
    'display high-precision time format for benchmarking/performance purposes
    'ex. "03:34:92.9302 This is the log message"
    Dim L As String
    Dim M As Long
    Dim N As Long = DateTime.Now

    M = (DateTime.GetHour(N) * DateTime.TicksPerHour) + (DateTime.GetMinute(N) * DateTime.TicksPerMinute) + (DateTime.GetSecond(N) * DateTime.TicksPerSecond)
    L = Pad(DateTime.GetHour(N),2,"0") & ":" & Pad(DateTime.GetMinute(N),2,"0") & ":" & Pad(DateTime.GetSecond(N),2,"0") & "." & Pad(N-M,4,"0")

    Return L
End Sub
 
Last edited:
  • Like
Reactions: Dey
Top