views:

2862

answers:

3

I want to have a way to report the stack trace to the user if an exception is thrown. What is the best way to do this? Does it take huge amounts of extra code?

To answer questions:

I'd like it to be portable if possible. I want information to pop up, so the user can copy the stack trace and email it to me if an error comes up.

+9  A: 

It depends which platform.

On GCC it's pretty trivial, see this post for more details.

On MSVC then you can use the StackWalker library that handles all of the underlying API calls needed for Windows.

You'll have to figure out the best way to integrate this functionality into your app, but the amount of code you need to write should be minimal.

Andrew Grant
A: 

rlbond - you've probably noticed already, but for anyone else in the future...

I've just been using the StackWalker library and after some testing, I realised that if you're using it for anything other than displaying information on an application-stopping exception, it's worth creating a single instance of the StackWalker object. For a multithreaded application, you could create an instance per thread using TLS to avoid synchronisation issues (haven't tried this yet though - be warned).

The reason for this is that 99%+ of the time to generate a trace is spent loading in all of the symbols. This percentage will vary - the 99% is for my setup, where I subclass and override OnOutput() to save everything into a buffer rather than displaying there and then but the point is the same - don't use them as a single-use object if you're going to use them a lot.

My timings were 42 ms to create an instance, get the stack trace and throw it away compared to around 0.6 ms to generate second and subsequent traces using the same object.

Just something to bear in mind.

Kev
A: 

On Windows, check out BugTrap

Stuart