在 Cocoa 中显示日志输出

发布于 2024-07-13 09:40:54 字数 193 浏览 5 评论 0原文

使用 Cocoa 组件显示不断更新的日志输出的最佳方式是什么? 输出是基于行的,由应用程序生成。 附加行应该很快,并且视图应该自动滚动到底部。 总而言之,它应该可以工作并且看起来像终端中的基本标准。

我的代码是用 Python 编写的,使用 PyObjC 桥。 我正在寻找我可以适应的方法/示例,欢迎使用 Objective-C 代码。

What is the best way to display constantly updated logging output using Cocoa Components? The output is line-based and generated by the application. Appending lines should be fast, and the view should automatically scroll to the bottom. All in all, it should work and look like basic standard out in a terminal.

My code is written in Python, using the PyObjC bridge. I'm looking for approaches/examples I can adapt, Objective-C code is welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-07-20 09:40:54

似乎有两个选择:(1)使用 NSTableView 和(2)使用 NSTextView。 Console.app 似乎使用 NSTableView,但 NSTextView 的代码可能更少。

对于 (1),我将编写一个 NSTableDataSource,它返回 tableView_objectValueForTableColumn_rowIndex_ 的相应行(从内存或从磁盘)以及 numberOfRowsInTableView_ 的日志行总数。 将此数据源设置为 NSTableView 的数据源。 当新的日志行进入时,您可能需要调用 tableView.reloadData() 来重新显示数据。假设表视图嵌入在 NSScrollView 中(如果您在 Interface Builder 中创建表视图,则默认情况下) ),你可以使用Apple Scroll View编程指南(很容易翻译成Python)中的这个方法滚动到底部。

- (void)scrollToBottom:sender;
{
    NSPoint newScrollOrigin;

    // assume that the scrollview is an existing variable
    if ([[scrollview documentView] isFlipped]) {
        newScrollOrigin=NSMakePoint(0.0,NSMaxY([[scrollview documentView] frame])
                                       -NSHeight([[scrollview contentView] bounds]));
    } else {
        newScrollOrigin=NSMakePoint(0.0,0.0);
    }

    [[scrollview documentView] scrollPoint:newScrollOrigin];

} 

显然,这段代码假设你有一个滚动视图的IBOutlet。

对于 (2),您可以使用 textView.textStorage().appendString_(new_log + '\n') 在文本视图的末尾添加一行(假设上面还没有换行符)结束)。 您可以通过调用 textView.setSelectedRange_(NSMakeRange(textView.textStorage().length(),0)) 来强制封闭的滚动视图滚动到末尾(间接)。

It seems that there are two options: (1) use an NSTableView and (2) use an NSTextView. Console.app appears to use an NSTableView but an NSTextView is probably less code.

For (1), I would write an NSTableDataSource that returns the appropriate line (either from memory or from disk) for tableView_objectValueForTableColumn_rowIndex_ and the total number of log lines for numberOfRowsInTableView_. Set this data source as the dataSource for an NSTableView. You may need to call tableView.reloadData() to redisplay the data when a new log line comes in. Assuming the table view is embedded in an NSScrollView (the default if you create the table view in Interface Builder), you can scroll to the bottom with this method from the Apple Scroll View Programming Guide (easily translated to Python)

- (void)scrollToBottom:sender;
{
    NSPoint newScrollOrigin;

    // assume that the scrollview is an existing variable
    if ([[scrollview documentView] isFlipped]) {
        newScrollOrigin=NSMakePoint(0.0,NSMaxY([[scrollview documentView] frame])
                                       -NSHeight([[scrollview contentView] bounds]));
    } else {
        newScrollOrigin=NSMakePoint(0.0,0.0);
    }

    [[scrollview documentView] scrollPoint:newScrollOrigin];

} 

Obviously, this code assumes that you've got an IBOutlet to the scroll view.

For (2), you can add a line to the end of the text view with textView.textStorage().appendString_(new_log + '\n') (assuming there isn't already a newline on the end). You can force the enclosing scroll view to scroll to the end (indirectoy) by calling textView.setSelectedRange_(NSMakeRange(textView.textStorage().length(),0))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文