Suppose you have the code of a Cocoa app which logs its own messages through NSlogs and printfs to the Console output. My goal is to redirect all this output into a separate NSWindow in a NSView.
How can I achieve this in a way that
(the usual software engineering guide lines)?
Here is the solution I came up with:
readInBackgroundAndNotify
from NSFileHandle
. When notified it calls the appending method. It has a method for starting the logging on file as well, which uses freopen(3)
to redirect some stream (stderr and stdout atm) to a file in append mode.This solution was created considering both Joshua Nozzi's answer and tlindner's one, and combines them. I have and encapsulated solution that respects the three requests in the question (I have to add only a line of code, I can revert back easily and I can use this solution in other apps too). I noticed that maybe sometimes it can be wrong to have an NSWindowController encapsulated this way (whereas all the other ones are managed by some super-controller).
I finally opted for the file solution since it is very easy to implement and more Cocoa-like than tlindner's one. Also it gives the opportunity to have a logging file that persists on the disk. But of course I may have missed something, point that to me in the comments please ^^