programatically check for debug?

techknight

Well-Known Member
Licensed User
Longtime User
Can you check for debug mode programatically? Reason I ask is I have logcat log prints everywhere and I dont want them when compiled in release mode.

is there a way to conditionally compile these "Log(XXXX)" when in debug mode, but not in release mode?

I know with VB you could do #ifdef or something like that.
 

mc73

Well-Known Member
Licensed User
Longtime User
You can create a custom log sub for e.g.
B4X:
sub customLog(message as string)
log(message)
end sub
Then, replace all your logs, with customLog and comment or uncomment the log statement inside your sub.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I guess thats one way to do it. Wonder if you can check if debug option is selected in the IDE, and have it do it automatically, but i guess this will work too.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use reflection to check whether your app is in debug mode:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim r As Reflector
   Dim debug As Boolean = r.GetStaticField("anywheresoftware.b4a.BA", "debugMode")
   Log(debug)
End Sub
Call this method once and store the result in a process global variable.
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Apologies for posting on old thread but I have a new comment to make . I have exactly the same issue as technight, lots of log statements I don't want in the production version.

I stopped the log being created by using calling this sub 'Blog' instead of log.

B4X:
 Sub Blog(msg as string)
#if debug
Log(msg)
#end if
End sub

This is all very well, but the code of the calls to the sub is still generated and all the strings etc are in the compiled code. As I understand it, these strings are not obfuscated, which is not ideal then.

What would be useful then is a contracted one line form of the #if x ... #end if construct.

Eg

#debug log(xyz)

Then you could do a global replace on all the log statements!

Derek
 
Last edited:
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
Apologies for posting on old thread but I have a new comment to make . I have exactly the same issue as technight, lots of log statements I don't want in the production version.
I stopped the log being created by using calling this sub 'Blog' instead of log.

Response : That's the mistake, don't put the log in a sub.

B4X:
 Sub Blog(msg as string)
#if debug
Log(msg)
#end if
End sub

This is all very well, but the code of the calls to the sub is still generated and all the strings etc are in the compiled code. As I understand it, these strings are not obfuscated, which is not ideal then.

What would be useful then is a contracted one line form of the #if x ... #end if construct. Indeed that's possible if you use :
#If DEBUG
Log("Anything: " & WhatYouWant)
#End If


Eg

#debug log(xyz)

Then you could do a global replace on all the log statements!

Derek

Hi,
Unless if i don't understand...
I don't know if you found the good manner, but if any people read that, it's interesting they know that's possible.
But directly in code instead to put in a 'Sub', see underlined comments in red.
Or, if you want absolutely to put it in a 'Sub' it's the calls to the 'Sub' that you must put between the #If/#End If statement.

That's all.

Regards.
 
Last edited:
Upvote 0
Top