Question

I am a long time Delphi dev and in the past I use a third party tool for logging and debugging while developing (called Smart Inspect) however now that I've upgraded to Delphi XE I want to try and use the IDE for debugging.

My question is, given a function like

procedure MyFunction;
var
    str : string;
begin
    str := 'Foo';
    //Debug, show value of str?
    str := AnotherFunction(str);
    //Debug, show value of str?
end;

how can I debug and get the value of str, without doing stupid things like showmessage(str);

if there is a video somewhere (or article) then I am more than happy to read.

Is there a way to watch/output the value of variables.

Was it helpful?

Solution

Well, Delphi XE comes with CodeSite logging, but I get the feeling you're talking about the debugger itself.

If you place a breakpoint in a routine, it will break to the debugger when it hits it. From there, you've got a Local Variables pane and a Watches pane along the left side of the IDE. Local Variables will show the value of all locals, and Watches lets you set up expressions whose value it will keep track of.

You can also get something similar to a watch, but with more detailed information (especially for structured types such as objects) by using Inspect (Alt-F5). Also, the Evaluate/Modify (Ctrl-F7) will allow you to type in expressions and evaluate them. It's not quite as detailed as Inspect, but it gives you a lot more flexibility.

If you familiarize yourself with these tools, you'll find debugging much easier.

OTHER TIPS

If you want to use the IDE Debugger only, then do the following:

  • put a breakpoint somewhere
  • right click on the breakpointr circle and choose "Breakpoint Properties ..."
  • press "Advanced" button to show more options
  • uncheck "Break" checkbox
  • then use "Log message" and "Eval expression" edit boxes to enter trace values

Such messages will be send to "Event Log" debugger pane. Right click on the pane and choose "Properties". There you can filter ("Messages") / hilight ("Colors") the trace messages as you whish.

1) You can use OutputDebugString Function to output string to debug window

2) You can use CodeSite Express. I recommend video from CodeRage 5 as a starting point for using CodeSite

Other answers are all correct.

My personal favorite technique (same as the answer by da-soft) is to create a breakpoint, that logs a message to the event log, containing a value that I want logged, and does not actually "break" (that is, execution continues without you hitting the Run icon). Then every time that line of code is reached, I get my message, and my values in the log. Since I can go back and read the history, as well as see the current values, I find this more useful than merely using the debugger watch window.

But since Delphi XE includes CodeSite, you can go far beyond what expression evaluation in breakpoints does for you. Code Site however requires that you modify your code to add some logging. But it's much better than a message box.

You can also use OutputDebugString(PChar(s)) to output any string to the debugger. Since this can contain whatever you want, it's a very nice way to debug but not show stuff to the end user.

In many of my applications, I have a special trace buffer, which is circular (that is, it keeps only the last 500 or so lines). When ever I see a problem, not only do I get a stack traceback, I also save that in-memory trace log, so I have some history on what was going on just before my problem.

You can also check out the Log 4 Delphi project.

I prefer debugger hints. After breaking to the debugger move your mouse to the "str" anywhere in your code and you will see its current value. Also you can highlight some statement by a mouse and evaluate it. For example highlight "AnotherFunction(str)" and place your mouse over it.

Nothing wrong with any of the other answers but I just wanted to add these useful functions.

procedure DebugString ( const s : string ) ; overload ;
begin
  {$IFDEF DEBUG}
  OutputDebugString ( PChar ( s ) ) ;
  {$ENDIF}
end ;

procedure DebugString ( const s : string ; args : array of const ) ; overload ;
begin
  {$IFDEF DEBUG}
  OutputDebugString ( PChar ( Format ( s , args ) ) ) ;
  {$ENDIF}
end ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top