views:

36

answers:

1

Hello Fellow Computer People!

Anyone willing to help will have my gratitude ;)

Just wondering what is the best approach to printing errors to a file i.e. so that if my executable crashes I can have a record of what happened.

This would be in Cocoa, Objective-C or C.

Thanks,

Eric

PS. I hope my question is not too general or vague. Please let me know if you need clarification.

+2  A: 

If your app crashes, it'll automatically write a crash log to ~/Library/Logs/CrashReporter. The crash log will include a lot of useful information, including a stack trace and the registry state. You don't need to do anything to enable this functionality.

For other error messages that don't cause crashes, you can use NSLog to write information to the application's log file, which is stored at ~/Library/Logs. The log messages will be prepended with a timestamp. By default NSLog writes to the global console log, but that behavior can be modified by setting STDERR_FILENO.

mipadi
Thanks @mipadi. But I'm assuming that the information at /Logs/CrashReporter will also have info from other apps on the machine which I don't want. Ideally the errors from my app will be on a unique file that my app creates and which I can have the user send to me. Do you know what I mean?
Eric Brotto
Yes. There is a separate crash log for each app (actually, each crash for each app) in `~/Library/Logs/CrashReporter`. By default `NSLog` prints to the global console log, but that can be modified by setting `STDERR_FILENO`.
mipadi
You can't *set* `STDERR_FILENO`, because it's a constant. You can, however, dupe another file descriptor onto it, which is how you redirect your own stderr.
Peter Hosey