简体   繁体   English

从控制台 C++ 中获取打印的语句

[英]Grabbing printed statements from console C++

I have two loggers in my program.我的程序中有两个记录器。 One that I made inside a gui, and one that is super complicated but very well designed and prints to the console.我在 gui 中制作的一个,一个超级复杂但设计得很好并打印到控制台的。 I am trying to get the output from the nice console logger to the rendered one.我试图从漂亮的控制台记录器获取输出到渲染的记录器。 I have tried everything under the sun to get this to work but I can't seem to figure it out (due to my lack of understanding of the code from the other logger(spdlog).) My conclusion is that taking the logs directly from what is printed is the best way to do this but I can't find online anyone asking how to do this.我已经在阳光下尝试了一切来让它工作,但我似乎无法弄清楚(由于我缺乏对另一个记录器(spdlog)的代码的理解。)我的结论是直接从打印的内容是最好的方法,但我在网上找不到任何人询问如何执行此操作。 I have seen a few questions but they just post code as an answer and don't really explain what is going on.我看到了一些问题,但他们只是发布代码作为答案,并没有真正解释发生了什么。 My question: Is there a way to grab printed statements from the console and what are the performance issues/complications that come with doing something like this.我的问题:有没有办法从控制台获取打印的语句,以及执行此类操作会带来哪些性能问题/并发症。

For example, if i do std::cout << "hello!" << std::endl;例如,如果我执行std::cout << "hello!" << std::endl; std::cout << "hello!" << std::endl; or some printf statement, I want to be able to further down in the code be able to grab "hello!"或者一些 printf 语句,我希望能够在代码中进一步向下能够抓取“你好!”

My conclusion is that taking the logs directly from what is printed is the best way to do this but I can't find online anyone asking how to do this.我的结论是,直接从打印的日志中获取日志是最好的方法,但我在网上找不到任何人询问如何执行此操作。

Consoles nowadays are terminal emulators.现在的控制台是终端模拟器。 The original terminals' output went to printers and couldn't be (easily) read back.原始终端的输出进入打印机,无法(轻松)读回。

Application's stdout and stderr (console) streams are write-only.应用程序的stdoutstderr (控制台)流是只写的。 Moreover, in Windows and Unix/Linux you can pipe your program's (console) output (either or both stderr and stdout ) into another application with |此外,在 Windows 和 Unix/Linux 中,您可以通过管道将程序的(控制台)输出( stderrstdout或两者)通过| (pipe) that creates a pipe IPC between stdout of your application and stdin of another one. (pipe) 在您的应用程序的stdout和另一个应用程序的stdin之间创建管道 IPC。 That IPC pipe is write-only, your application cannot possibly read back from it.该 IPC 管道是只写的,您的应用程序不可能从中读回。

You may be able to get access to the contents of the frame buffer of Windows cmd.exe that controls its Windows console window, but that won't be the verbatim byte-exact copy of data you wrote into stdout because of the escape sequences interpreted by Windows console .您可能能够访问控制其 Windows 控制台窗口的 Windows cmd.exe帧缓冲区的内容,但这不会是您写入stdout的数据的逐字精确副本,因为转义序列被解释通过 Windows 控制台

If stdout is redirected into a file you can re-open that file for reading, but there is no portable way to re-open that file.如果stdout被重定向到一个文件中,您可以重新打开该文件进行读取,但没有可移植的方式来重新打开该文件。

In other words, there is no portable way to read console output back.换句话说,没有可移植的方式来读回控制台输出。


I have tried everything under the sun to get this to work but I can't seem to figure it out (due to my lack of understanding of the code from the other logger(spdlog).我已经在阳光下尝试了一切来让它工作,但我似乎无法弄清楚(由于我缺乏对其他记录器(spdlog)的代码的理解)。

I bet you haven't tried reading spdlog documentation, in particular logger with multi sinks .我敢打赌,您还没有尝试阅读spdlog文档,尤其是logger with multi sinks A sink is an output abstraction, which implementation can write into a file, memory or both.接收器是一个输出抽象,它的实现可以写入文件、内存或两者。 What you need is attach your own sink to spdlog that prints into your UI.您需要的是将您自己的接收器附加到打印到您的 UI 中的spdlog

Derive your sink from base_sink and implement abstract member functions:base_sink派生接收器并实现抽象成员函数:

  • virtual void sink_it_(const details::log_msg &msg) = 0; to print into the UI , and,打印到UI ,并且,
  • virtual void flush_() = 0; to do nothing.什么都不做。

Then attach one object of your sink class to that spdlog .然后将接收器类的一个对象附加到该spdlog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM