Debug output on distributed exe?

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1109
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Debug output on distributed exe?

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Debug output on distributed exe?

Post 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.
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Debug output on distributed exe?

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Randy Walker
Addict
Addict
Posts: 1109
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Debug output on distributed exe?

Post 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")
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Debug output on distributed exe?

Post by infratec »

'release mode' means the release which is used by a customer (exe) :wink:

Instead of 'Replace all Debug lines With the following' use a Macro
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Debug output on distributed exe?

Post 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
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Debug output on distributed exe?

Post 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
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Debug output on distributed exe?

Post by Fred »

You should not call it the same, but rather "Trace()" or "MyDebug()" etc.
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Debug output on distributed exe?

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Randy Walker
Addict
Addict
Posts: 1109
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Debug output on distributed exe?

Post 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!!!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Debug output on distributed exe?

Post by BarryG »

Ah, so I forgot the brackets. :)
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Debug output on distributed exe?

Post 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.
bye,
Daniel
Randy Walker
Addict
Addict
Posts: 1109
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Debug output on distributed exe?

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Debug output on distributed exe?

Post 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(...).
bye,
Daniel
Randy Walker
Addict
Addict
Posts: 1109
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Debug output on distributed exe?

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply