How to create functions like std::cout?

后端 未结 6 637
盖世英雄少女心
盖世英雄少女心 2021-01-03 16:40

I\'m creating my own logging utility for my project, I want to create a function like iostream\'s std::cout, to log to a file and print to the console as well.

Here\

6条回答
  •  暖寄归人
    2021-01-03 17:01

    There's only one decent solution, and it's not simple, but it gets LOG(log_info) << "line 1\nline 2" << std::endl; correct.

    You must implement a custòm std::ostreambuf. It's the only class that can reformat output after all the usual operator<< functions have been applied.

    In particular, your stream buffer method overflow is called when you've got a bunch of characters to work on. You can now add your loglevel filtering, and reliably check for newlines in the formatted character stream.

提交回复
热议问题