<bdo id='AjF1b'></bdo><ul id='AjF1b'></ul>
    <i id='AjF1b'><tr id='AjF1b'><dt id='AjF1b'><q id='AjF1b'><span id='AjF1b'><b id='AjF1b'><form id='AjF1b'><ins id='AjF1b'></ins><ul id='AjF1b'></ul><sub id='AjF1b'></sub></form><legend id='AjF1b'></legend><bdo id='AjF1b'><pre id='AjF1b'><center id='AjF1b'></center></pre></bdo></b><th id='AjF1b'></th></span></q></dt></tr></i><div id='AjF1b'><tfoot id='AjF1b'></tfoot><dl id='AjF1b'><fieldset id='AjF1b'></fieldset></dl></div>
    1. <small id='AjF1b'></small><noframes id='AjF1b'>

      <legend id='AjF1b'><style id='AjF1b'><dir id='AjF1b'><q id='AjF1b'></q></dir></style></legend>

      <tfoot id='AjF1b'></tfoot>
    2. 自定义流到 C++ 中的方法?

      Custom stream to method in C++?(自定义流到 C++ 中的方法?)
    3. <legend id='keBpa'><style id='keBpa'><dir id='keBpa'><q id='keBpa'></q></dir></style></legend>
        <tbody id='keBpa'></tbody>

      <tfoot id='keBpa'></tfoot>

          <bdo id='keBpa'></bdo><ul id='keBpa'></ul>
          • <i id='keBpa'><tr id='keBpa'><dt id='keBpa'><q id='keBpa'><span id='keBpa'><b id='keBpa'><form id='keBpa'><ins id='keBpa'></ins><ul id='keBpa'></ul><sub id='keBpa'></sub></form><legend id='keBpa'></legend><bdo id='keBpa'><pre id='keBpa'><center id='keBpa'></center></pre></bdo></b><th id='keBpa'></th></span></q></dt></tr></i><div id='keBpa'><tfoot id='keBpa'></tfoot><dl id='keBpa'><fieldset id='keBpa'></fieldset></dl></div>

            <small id='keBpa'></small><noframes id='keBpa'>

              • 本文介绍了自定义流到 C++ 中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在制作一个记录器,我希望有一些类似流的事情发生,理想情况下做 CLogger <<测试",<<1<<",2,3 "; 而不是 CLogger->log("Testing, %i,2,3", 1);

                I'm making a logger and I wish to have some kind of stream-like happenings going on, ideally doing CLogger << "Testing, " << 1 << ",2,3 "; instead of CLogger->log("Testing, %i,2,3", 1);

                我的问题是我该怎么做?我不想直接创建一个到 stdout 的流,因为我想使用我自己的方法,包括写入文件等.我已经考虑过重载一个特定的结构,它将当前的流缓冲区刷新到一个方法,但我必须做 CLogger <<冲洗<<"Test! "; 这有点奇怪.

                My question is how would I do this? I don't want to directly create a stream to stdout as I want to use my own method which includes writing files and such. I've considered overloading with a certain struct that'd flush the current stream buffer to a method, but I'd have to do CLogger << flush << "Test! "; which is kind of odd.

                有人知道怎么做吗?

                推荐答案

                如果您需要的只是将某些日志消息定向到文件,您是否考虑过 std::ofstream?

                If all that you need is directing certain log messages to files, have you considered std::ofstream?

                否则,我喜欢从 std::ostream 派生我的日志记录类,所以我得到了流的所有优点.诀窍是将所有特定于应用程序的代码放在关联的 streambuf 类中.考虑:

                Otherwise, I like to derive my logging class from std::ostream, so I get all of the stream goodness. The trick is to put all of your application-specific code in the associated streambuf class. Consider:

                #include <iostream>
                #include <sstream>
                
                class CLogger : public std::ostream {
                private:
                    class CLogBuf : public std::stringbuf {
                    private:
                        // or whatever you need for your application
                        std::string m_marker;
                    public:
                        CLogBuf(const std::string& marker) : m_marker(marker) { }
                        ~CLogBuf() {  pubsync(); }
                        int sync() {
                            std::cout << m_marker << ": " << str();
                            str("");
                            return std::cout?0:-1;
                        }
                
                    };
                
                public:
                    // Other constructors could specify filename, etc
                    // just remember to pass whatever you need to CLogBuf
                    CLogger(const std::string& marker) : std::ostream(new CLogBuf(marker)) {}
                    ~CLogger() { delete rdbuf(); }
                };
                
                int main()
                {
                    CLogger hi("hello");
                    CLogger bye("goodbye");
                
                    hi << "hello, world" << std::endl;
                    hi << "Oops, forgot to flush.
                ";
                    bye << "goodbye, cruel world
                " << std::flush;
                    bye << "Cough, cough.
                ";
                }
                

                注意事项:

                • CLogger 构造函数可以采用您需要使用的任何参数——文件名、输出语言、指向底层日志数据的指针,等等.只需将数据传递到 CLogBuf 类即可.
                • CLogBuf 的 sync() 在响应 std::flush 期间被自动调用.

                这篇关于自定义流到 C++ 中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

              • <small id='SfWDc'></small><noframes id='SfWDc'>

                  <tbody id='SfWDc'></tbody>
              • <legend id='SfWDc'><style id='SfWDc'><dir id='SfWDc'><q id='SfWDc'></q></dir></style></legend>

                    <bdo id='SfWDc'></bdo><ul id='SfWDc'></ul>

                      1. <i id='SfWDc'><tr id='SfWDc'><dt id='SfWDc'><q id='SfWDc'><span id='SfWDc'><b id='SfWDc'><form id='SfWDc'><ins id='SfWDc'></ins><ul id='SfWDc'></ul><sub id='SfWDc'></sub></form><legend id='SfWDc'></legend><bdo id='SfWDc'><pre id='SfWDc'><center id='SfWDc'></center></pre></bdo></b><th id='SfWDc'></th></span></q></dt></tr></i><div id='SfWDc'><tfoot id='SfWDc'></tfoot><dl id='SfWDc'><fieldset id='SfWDc'></fieldset></dl></div>
                        <tfoot id='SfWDc'></tfoot>