如何在NSTextView中轻松重定向控制台输出? [英] How can I easily redirect the console output in a NSTextView?

查看:223
本文介绍了如何在NSTextView中轻松重定向控制台输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个Cocoa应用程序的代码,它通过NSlogs和printfs将自己的消息记录到控制台输出。我的目标是在NSView中将所有这些输出重定向到一个单独的NSWindow。



如何以




  • 最小化要重写的代码量

  • 可以还原

  • >解决方案

    这里是我想出的解决方案:


    1. 创建一个NSWindowController有一个NSTextView作为(我们称之为 A

    2. 创建一个封装A对象的Singleton类(让我们称之为 B 使用 readInBackgroundAndNotify NSFileHandle中读取一个文件(包含所有记录)将字符串发送到A(将其附加到它的NSTextView) code>。通知它时调用追加方法。它还有一个方法用于启动日志记录文件,它使用 freopen(3)将一些流(stderr和stdout atm)重定向到一个附加模式的文件。 / li>
    3. 在项目中只需调用B的开始日志记录方法(无需实例化,但我想它真的没有关系)。



      1. 这个解决方案是考虑到Joshua Nozzi的答案和tlindner的答案,并结合它们。我有和封装的解决方案,尊重三个请求的问题(我只需要添加一行代码,我可以轻松地回复,我可以在其他应用程序中使用此解决方案)。我注意到,有时可能是错误的有一个NSWindowController封装这种方式(而所有其他的是由一些超级控制器管理)。



        我终于选择了文件解决方案,因为它是非常容易实现和更像Cocoa的比tlindner的一个。它还提供了一个日志文件持久存储在磁盘上的机会。但是当然我可能错过了一些,指出在我的评论请^^


        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

        • minimizes the amount of code to rewrite
        • makes it possible to revert back
        • maximizes the reuse of written code

        (the usual software engineering guide lines)?

        解决方案

        Here is the solution I came up with:

        1. Create an NSWindowController that has a NSTextView as an outlet (let's call this A)
        2. Create a Singleton class (let's call this B) that encapsulates an A object and provides some methods for sending strings to A (which appends it to its NSTextView) by reading a file (which contains all the loggings) using 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.
        3. In the project just call the starting logging method of B (no needs of instantiation, but I guess it really does not matter) after importing it.

        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 ^^

        这篇关于如何在NSTextView中轻松重定向控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆