Android Question Get Stack programmatically - How to

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Hi i need to get the stack as the following image by code for tracking some error in my app.
can i have help please?
tks

1617264631704.png
 

DonManfred

Expert
Licensed User
Longtime User
Hi i need to get the stack as the following image by code for tracking some error in my app.
can i have help please?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The only way I know of to get a stack trace is to catch an Exception and use the ExceptionEx object in my Threading library | B4X Programming Forum
B4X:
    Dim Ex As ExceptionEx
    Ex = LastException
You can also use it to Throw your own Exception if needed.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This will work in B4A and B4J, it will log the underlying Java stack:
B4X:
Sub LogStackTrace
    Dim jo As JavaObject
    Dim stack() As Object = jo.InitializeStatic("java.lang.Thread").RunMethodJO("currentThread", Null).RunMethod("getStackTrace", Null)
    For Each st As JavaObject In stack
        Log(st.RunMethod("getClassName", Null) & ": " & st.RunMethod("getMethodName", Null))
    Next
End Sub
It will make more sense in release mode.
 
Upvote 0

Gianni Sassanelli

Active Member
Licensed User
Longtime User

thanks Manfred
i don't access to log but simply i need to know what is sub tha have called a second sub from second sub
for example
i have a pubblic sub named Log_generic that is called some times.
From this sub i need to know who have called it without pass subFrom parameter if possible.

B4X:
sub s1
msgbox("first sub","")
log_Generic("s1","detail of log..")
end sub

sub log_Generic(subFrom as String, LogData as String)
'i need here to know subFrom without pass the parameter if possible

LOG($"
  Procedure:  ${subFrom}
  Detail:     ${logData}
"$)

end sub
 
Upvote 0
Top