【问题标题】:How to redefine clog to tee to original clog and a log file?如何将 clog 重新定义为原始 clog 和日志文件?
【发布时间】:2023-04-07 06:23:01
【问题描述】:

我在这里看到了一个有用的开始:

http://www.cs.technion.ac.il/~imaman/programs/teestream.html

创建一个新的流到阻塞和日志文件的流效果很好。

但是,如果我尝试将 clog 重新定义为新流,则它不起作用,因为新流具有与 clog 相同的 rdbuf(),因此以下内容无效:

clog.rdbuf(myTee.rdbuf());

那么我怎样才能修改 tee 类以拥有自己的 rdbuf() ,然后它可以成为 clog 的目标?

谢谢。

-威廉

【问题讨论】:

    标签: c++ stream tee clog


    【解决方案1】:

    如果你真的想继续对 tee 使用 std::clog 而不是将输出发送到不同的流,你需要工作低一级:而不是从 ostream 派生,从 streambuf 派生。然后你可以这样做:

    fstream logFile(...);
    TeeBuf tbuf(logFile.rdbuf(), clog.rdbuf());
    clog.rdbuf(&tbuf);
    

    有关如何派生您自己的 streambuf 类的更多信息,请参阅here。

    【讨论】:

    • 您好,感谢您的建议。对此,我想知道我的 TeeBuf 类是否会覆盖虚拟 xsputn() 以放入两个流?那就足够了吗?换句话说,我正在寻找与原始解决方案中的 operator
    • 好的,我已经编写了一个覆盖 xsputn() 并发送到两个底层 streambuf(s) 的版本。但是,只有第一行有效并被发送到 clog 和日志文件,一旦看到“endl”,就不再有输出到 clog 或文件。那么我需要用新的 tee 缓冲区在 clog 上正确处理“endl”吗?
    • 这里是建议的 xsputn():streamsize xsputn(const char_type *charSequence, streamsize numChar) { _clogBu​​f->sputn(charSequence, numChar); return _fileBuf->sputn(charSequence, numChar); }
    • 我也尝试为 seekoff()、seekpos() 和 sync() 添加定义,它们只使用调用 pubseekoff()、pubseekpos() 和 pubsync() 的底层两个缓冲区,但我得到了与“endl”工作的第一行相同的症状。
    • 你不需要xsputn,那只是为了优化,你需要重载sync()和overflow(),每次内部缓冲区应该同步到底层表示时都会调用sync()(例如屏幕),所以在每个 std::flush、std::endl 上,所以要确保它这样做。当内部缓冲区填满时调用 overflow(),因此它应该同步以释放缓冲区,检查 eof() 等。
    【解决方案2】:

    您不想做您想做的事情,因为“tee”不在 rdbuf 级别上工作。因此将 rdbuf 设置为其他内容是行不通的,输出只会转到一个流。

    你需要按照那里的例子:

    例如

    fstream clog_file(...);
    xstream clog_x(...);
    TeeStream clog(clog_file, clog_x);
    

    然后在任何地方都使用 clog 而不是原来的 clog。

    【讨论】:

    • 你好,有没有人有一个如何重新定义 C++ 内置 clog 的例子,而不是有一个新的关联 rdbuf(),它被处理为原始 clog.rdbuf() 和ofstream 对象的 rdbuf() 到磁盘上的日志文件。目的是让代码始终使用 std::clog ,但让它同时进入默认的 clog 目标以及磁盘上的日志文件。谢谢。 -威廉
    • 见内森的回答。这是更多的工作,但它会工作。如果您想走这条路,我建议您购买《C++ 标准库:教程和参考》一书。我仍然建议使用你自己的 typedef,这样你就不会被阻塞。 amazon.com/C-Standard-Library-Tutorial-Reference/dp/0201379260/…
    【解决方案3】:

    这是我创建的似乎可以完成这项工作的课程,感谢所有提供帮助的人!

    -威廉

    class TeeStream : public std::basic_filebuf<char, std::char_traits<char> >
    {
    private:
      class FileStream : public std::ofstream {
      public:
        FileStream()
          : logFileName("/my/log/file/location.log") {
          open(logFileName.c_str(), ios::out | ios::trunc);
    
          if (fail()) {
            cerr << "Error: failed to open log file: " << logFileName << endl;
            exit(1);
          }
        }
        ~FileStream() {
          close();
        }
    
        const char *getLogFileName() const {
          return logFileName.c_str();
        }
    
      private:
        const string logFileName;
    
      };
    
    public:
      typedef std::char_traits<char> traits;
      typedef std::basic_filebuf<char, traits> baseClass;
    
      TeeStream()
        :  baseClass(),
           _logOutputStream(),
           _clogBuf(clog.rdbuf()),
           _fileBuf(_logOutputStream.rdbuf()) {
        clog.rdbuf(this);
        _logOutputStream << "Log file starts here:" << endl;
      }
      ~TeeStream() {
        clog.rdbuf(_clogBuf);
      }
    
      int_type overflow(char_type additionalChar =traits::eof()) {
        const int_type eof = traits::eof();
        const char_type additionalCharacter = traits::to_char_type(additionalChar);
        const int_type result1 = _clogBuf->sputc(additionalCharacter);
        const int_type result2 = _fileBuf->sputc(additionalCharacter);
    
        if (traits::eq_int_type(eof, result1)) {
          return eof;
        } else {
          return result2;
        }
      }
    
      int sync() {
        const int result1 = _clogBuf->pubsync();
        const int result2 = _fileBuf->pubsync();
    
        if (result1 == -1) {
          return -1;
        } else {
          return result2;
        }
      }
    
    private:
      FileStream _logOutputStream;
      streambuf * const _clogBuf;
      streambuf * const _fileBuf;
    
    };
    

    【讨论】:

    • 虽然总体思路看起来不错,但您有一些潜在的问题。 putc 需要一个 char_type,当您传递 int_type 时,这些转换有一个 traits::to_char_type() 函数(并进行适当的检查以查看转换是否成功)
    • 其次,最好使用特征比较函数 i.s.o。依靠operator== 比较内部int_type:traits::eq_int_type(additionalChar, traits::eof())(虽然这是相当学术的,但实际上大多数时候它不会产生影响)
    • 根据 Pieter 的建议进行更改。
    【解决方案4】:

    我只会使用 Boost iostreams 的东西来做。

    #include &lt;iostream&gt;
    #include &lt;fstream&gt;
    #include &lt;boost/iostreams/tee.hpp&gt;
    #include &lt;boost/iostreams/stream.hpp&gt;

    int main(const int a_argc, const char *a_args[])
    {
        namespace io = boost::iostreams;
        typedef io::tee_device<std::ofstream, std::ostream> TeeDevice;
        typedef io::stream<TeeDevice> TeeStream;
    
        std::ofstream flog("logFile.txt");
        //We need to copy clog, otherwise we get infinite recursion
        //later on when we reassign clog's rdbuf.
        std::ostream clogCopy(std::clog.rdbuf());
    
        TeeDevice logTee(flog, clogCopy);
        TeeStream logTeeStream(logTee);
    
        logTeeStream << "This text gets clogged and flogged." << std::endl;
    
        //Modify clog to automatically go through the tee.
        std::streambuf *originalRdBuf = std::clog.rdbuf(logTeeStream.rdbuf());  
    
        std::clog << "This text doesn't only get clogged, it's flogged too." << std::endl;
    
        std::clog.rdbuf(originalRdBuf);
        std::clog << "This text avoids flogging." << std::endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-30
      • 2011-05-30
      • 1970-01-01
      • 2011-02-07
      • 2011-01-25
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2014-09-05
      相关资源
      最近更新 更多