Page 1 of 2
Debug output on distributed exe?
Posted: Mon Nov 04, 2024 9:30 am
by Randy Walker
Is there a way to produce debug output on a distributed copy of a PB 5.xx exe file?
I think that should be clear enough, but just in case, what I am looking for is preferably a debugger output window as we get when we compile with the dewbugger. Otherwise. have the debug output go directly to a file. This would be on a distributed exe file compiled using the PB IDE.
Re: Debug output on distributed exe?
Posted: Mon Nov 04, 2024 10:16 am
by Fred
No, it's not possible as you would need to distribute the PB GUI debugger with it. The easier for you is to do some wrapper around the Debug command to write it to a file if you are in release mode.
Re: Debug output on distributed exe?
Posted: Mon Nov 04, 2024 11:15 am
by Axolotl
Hi Randy,
first and important info: All following solutions need a recreation and distibution of the exe.
First sugestion:
On windows (only) you can use this tool from sysinternals called
DebugView......
To send a message you need this
Code: Select all
;
; Link: https://www.purebasic.fr/english/posting.php?mode=reply&t=85664&sid=f17527e83d31cfbc570e80b1ca62806e
;
; void OutputDebugStringA(
; [in, optional] LPCSTR lpOutputString
; );
;
Macro AppendDebug(Message)
OutputDebugString_(Message)
EndMacro
; acc. to MSDN (never used or tested)
Macro ClearDebug(Message)
OutputDebugString_("DBGVIEWCLEAR")
EndMacro
Second suggestion:
I know there are other (smaller/bigger/better) solutions, but this worked for me very well........
Code: Select all
; define Logging (log to file)
;
Structure TLogging
File$
bEnabled.i ; #False (0) == disabled (1) == enabled for messages, (2) == <?>
EndStructure
; semiglobal (cannot be used by accident) -- real world app: updates this structure variables via preference file.
;
Define Logging.TLogging
Logging\File$ = "" ; <?>
Logging\bEnabled = 0 ; initially disabled
; write to a defined log file
;
Procedure WriteLogMessageToFile(Message$) ; returns nothing
Shared Logging.TLogging ; semiglobal
Protected FN
Debug "LOGOUT: '" + Message$ + "' ; '" + Logging\File$ + "' " + Logging\bEnabled
If Logging\File$ <> "" And Logging\bEnabled <> #False ; valid filename and enabled logging -> go for it
FN = OpenFile(#PB_Any, Logging\File$, #PB_File_SharedRead|#PB_File_NoBuffering|#PB_UTF8|#PB_File_Append)
If FN <> 0
WriteStringN(FN, FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss", Date()) + " > " + Message$)
CloseFile(FN)
EndIf
EndIf
EndProcedure
; Used Macro, with default info on source code (maybe helpful)
;
Macro LogWrite(Message)
WriteLogMessageToFile(Message + " // File: " + #PB_Compiler_Filename + ", " + #PB_Compiler_Line)
EndMacro
Third suggestion: (Win only)
you can use a WM_COPYDATA based Client/Server app combo to do the trick.
Re: Debug output on distributed exe?
Posted: Mon Nov 04, 2024 9:28 pm
by Randy Walker
Fred wrote: Mon Nov 04, 2024 10:16 am
write it to a file if you are in release mode.
Uhhhh. What is a "release mode"? Can't find it in the F1 help >search.
Anyway, looks like it just boils down to workarounds -- No compiler directive or switch I was unaware of so I think the following looks like my preferred workaround:
Code: Select all
; Add this window to top of file:
If OpenWindow(0, 0, 0, 422, 350, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 396, 333)
EndIf
Replace all Debug lines With the following
AddGadgetItem(0, -1, "Desired debug values")
Re: Debug output on distributed exe?
Posted: Mon Nov 04, 2024 10:01 pm
by infratec
'release mode' means the release which is used by a customer (exe)
Instead of 'Replace all Debug lines With the following' use a Macro
Re: Debug output on distributed exe?
Posted: Mon Nov 04, 2024 10:32 pm
by BarryG
infratec wrote: Mon Nov 04, 2024 10:01 pmInstead of 'Replace all Debug lines With the following' use a Macro
That's what I thought, but it doesn't work because you can't change the "Debug" command like you can with other commands:
Code: Select all
Macro Debug(text) ; Doesn't work.
AddGadgetItem(0, -1, text)
EndMacro
If OpenWindow(0, 0, 0, 422, 350, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 396, 333)
For n=1 To 10
Debug Str(n)
Next
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Re: Debug output on distributed exe?
Posted: Tue Nov 05, 2024 4:39 am
by PBJim
BarryG wrote: Mon Nov 04, 2024 10:32 pm
That's what I thought, but it doesn't work because you can't change the "Debug" command like you can with other commands:
I don't have any prior experience of macros as I have not used them at all, but your comment sparked my interest Barry.
It seems to me though, that Debug isn't special in any way, and if you change the name of the macro, it makes the reason clear — it's that in order to match this particular macro (which has a parameter), the brackets must be included, which they are not, so this example won't compile :
Code: Select all
Macro Deb(text)
AddGadgetItem(0, -1, text)
EndMacro
If OpenWindow(0, 0, 0, 422, 350, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 396, 333)
For n=1 To 10
Deb Str(n)
Next
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
The example is trying to mix what the documentation refers to as
"simple macros (without parameters) and complex (with parameters, needs the parentheses when calling it)."
Omitting the brackets after the debug in the for/next loop, fails to match the macro, so you have to include them and then it works perfectly:
Code: Select all
Macro Debug(text)
AddGadgetItem(0, -1, text)
EndMacro
If OpenWindow(0, 0, 0, 422, 350, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 396, 333)
For n=1 To 10
Debug(Str(n)) ; <--- Change to parameter
Next
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Re: Debug output on distributed exe?
Posted: Tue Nov 05, 2024 10:37 am
by Fred
You should not call it the same, but rather "Trace()" or "MyDebug()" etc.
Re: Debug output on distributed exe?
Posted: Tue Nov 05, 2024 3:49 pm
by Axolotl
Randy Walker wrote: Mon Nov 04, 2024 9:28 pm
Uhhhh. What is a "release mode"? Can't find it in the F1 help >search.
Well, in my understanding "release mode" is when I use the Menu Item "Create Executable....." and honestly I have never thought in any other direction.
BTW: A
fourth suggestion could be the use of network client/server stuff. And surely another workaround, if you want to call it that.
Acc. to the Macro discussion: For me Macro is a smart text replacement. With a lot of cons and pros.
The biggest problem IMHO is the side effects, similar to the use of define in C code.
Re: Debug output on distributed exe?
Posted: Wed Nov 06, 2024 5:16 am
by Randy Walker
PBJim wrote: Tue Nov 05, 2024 4:39 am
Code: Select all
Macro Debug(text)
AddGadgetItem(0, -1, text)
EndMacro
If OpenWindow(0, 0, 0, 422, 350, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 396, 333)
For n=1 To 10
Debug(Str(n)) ; <--- Change to parameter
Next
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Not sure exactly who posted the above code but it's a nearly PERFECT solution -- THANKS!!!!
Re: Debug output on distributed exe?
Posted: Wed Nov 06, 2024 5:59 am
by BarryG
Ah, so I forgot the brackets.

Re: Debug output on distributed exe?
Posted: Wed Nov 06, 2024 7:31 am
by DarkDragon
Fred wrote: Tue Nov 05, 2024 10:37 am
You should not call it the same, but rather "Trace()" or "MyDebug()" etc.
Please don't forget this. It should not be called Debug.
Re: Debug output on distributed exe?
Posted: Wed Nov 06, 2024 7:48 am
by Randy Walker
DarkDragon wrote: Wed Nov 06, 2024 7:31 am
Please don't forget this. It should not be called Debug.
I don't understand the warning. If you're worried my kids will be born naked, It's happened already.
Re: Debug output on distributed exe?
Posted: Wed Nov 06, 2024 8:00 am
by DarkDragon
Randy Walker wrote: Wed Nov 06, 2024 7:48 am
DarkDragon wrote: Wed Nov 06, 2024 7:31 am
Please don't forget this. It should not be called Debug.
I don't understand the warning. If you're worried my kids will be born naked, It's happened already.
Calling macros or procedures like keywords is a huge no-go in programming. It's as if you named your kid like an article or a common verb or place. It makes communication complicated.
Macro Trace ... instead of Macro Debug. And then use Trace(...) instead of Debug(...).
Re: Debug output on distributed exe?
Posted: Thu Nov 07, 2024 6:42 am
by Randy Walker
So this is what I came up with at the top of the file:
Code: Select all
Select MessageRequester("Debug Enabler", "Do you wish to enable Debug Mode?", #MB_YESNO|#MB_ICONQUESTION|#MB_DEFBUTTON2)
Case #IDYES
on =1
Case #IDNO
on =0
EndSelect
Macro trace(text)
If on
AddGadgetItem(0, -1, text)
EndIf
EndMacro
If OpenWindow(0, 30, 30, 422, 350, "Pseudo Debug", #PB_Window_SystemMenu | #PB_Window_Invisible)
EditorGadget(0, 8, 8, 396, 333)
For n=1 To 10
trace(Str(n)) ; <--- Change to parameter
Next
trace("Debug enabled")
If on
HideWindow(0,#False)
EndIf
EndIf