简体   繁体   English

std :: cout,ostream和其他类型的获取输出流

[英]std::cout, ostream and other kinds of getting output stream

In my project (Unreal Engine 4) I don't have an output stream - instead of this I can communicate via UE_LOG function, which works pretty much similar to printf() . 在我的项目(虚幻引擎4)中,我没有输出流,而是可以通过UE_LOG函数进行通信,该函数的工作原理与printf()非常相似。 The problem is that I just made a .dll library (without Unreal includes) which I want to communicate through the iostream . 问题是我只是制作了一个.dll库(不包含Unreal包含),我想通过iostream进行通信。 My idea is - inside .dll library I use standard cout to write messages into ostream, I use all of it in Unreal Engine functions, where I grab ostream in form of string and output it into UE_LOG function. 我的想法是-在.dll库中,我使用标准cout将消息写入ostream,我在Unreal Engine函数中使用所有消息,在其中我以字符串形式获取ostream并将其输出到UE_LOG函数中。

Problem is I always treated std::cout as a part of magic, without thinking what is really inside (I am pretty sure most of us did). 问题是我一直把std::cout当作魔术的一部分,而不考虑里面到底是什么(我敢肯定我们大多数人都做过)。 How I can handle this? 我该如何处理? Easy ways won't work (like grabbing stringstream and outputing it into UE_LOG). 简单的方法不起作用(例如获取stringstream并将其输出到UE_LOG中)。

My idea is - inside .dll library I use standard cout to write messages into ostream 我的想法是-在.dll库中,我使用标准cout将消息写入ostream

You actually can replace the output buffer used with std::cout with your own implementation. 实际上,您可以使用自己的实现替换std::cout使用的输出缓冲区。 Use the std::ostream::rdbuf() function to do so (example from the reference docs): 使用std::ostream::rdbuf()函数执行此操作(参考文档中的示例):

#include <iostream>
#include <sstream>

int main()
{
    std::ostringstream local;
    auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer

    std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
        // buffer of 'local' object

    // now std::cout work with 'local' buffer
    // you don't see this message
    std::cout << "some message";

    // go back to old buffer
    std::cout.rdbuf(cout_buff);

    // you will see this message
    std::cout << "back to default buffer\n";

    // print 'local' content
    std::cout << "local content: " << local.str() << "\n";
}

(in case my edit won't be positively reviewed) (以防我的编辑未被正面评价)

From OP: Thanks to your hints I finally found how to solve my problem. 来自OP:感谢您的提示,我终于找到了解决问题的方法。 Suppose I want to get stream from cout and send it to printf (because I think stdio library is superior to iostream). 假设我想从cout获取流并将其发送到printf(因为我认为stdio库优于iostream)。 Here how I can do this: 在这里,我该怎么做:

#include <iostream>
#include <sstream>
#include <cstdio>

using namespace std;

class ssbuf : public stringbuf{
protected:
    int sync(){
        printf("My buffer: %s",this->str().c_str());
        str("");
        return this->stringbuf::sync();
    }
};


int main(){
    ssbuf *buf = new ssbuf();
    cout.rdbuf(buf);
    cout<<"This is out stream "<<"and you cant do anything about it"<<endl;
    cout<<"(don't) "<<"Vote Trump"<<endl;
}

Code is very raw, but it does it's job. 代码是很原始的,但是确实可以。 I made child class of buffer which has method sync() downcasting original virtual method sync(). 我使缓冲区的子类具有方法sync()向下转换原始虚拟方法sync()。 Except this it works like usual buffer, just grabs all console-out stream - exactly what we wanted. 除此之外,它的工作方式与通常的缓冲区一样,只是抓住了所有控制台输出流-正是我们想要的。 The str("") inside is to clean the buffer - probably not outputted stream doesn't clean itself. 里面的str(“”)用来清理缓冲区-可能没有输出的流不会清理自身。

Great thanks for help! 非常感谢您的帮助! Big GRIN for you! 大GRIN为您服务! :D :d

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

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