C++ streambuf用法


class LogStreamBuf : public std::streambuf {
  public:
  // REQUIREMENTS: "len" must be >= 2 to account for the '\n' and '\n'.
  LogStreamBuf(char *buf, int len) {
    setp(buf, buf + len - 2);
  }
  // This effectively ignores overflow.
  virtual int_type overflow(int_type ch) {
    return ch;
  }

  // Legacy public ostrstream method.
  size_t pcount() const { return pptr() - pbase(); }
  char* pbase() const { return std::streambuf::pbase(); }
};

 

class LogStream : public std::ostream {
   public:
      LogStream(char *buf, int len, int ctr)
          : std::ostream(NULL),
            streambuf_(buf, len),
            ctr_(ctr),
            self_(this) { rdbuf(&streambuf_);}

      int ctr() const { return ctr_; }
      void set_ctr(int ctr) { ctr_ = ctr; }
      LogStream* self() const { return self_; }

      // Legacy std::streambuf methods.
      size_t pcount() const { return streambuf_.pcount(); }
      char* pbase() const { return streambuf_.pbase(); }
      char* str() const { return pbase(); }

  private:
      base_logging::LogStreamBuf streambuf_;
      int ctr_;  // Counter hack (for the LOG_EVERY_X() macro)
      LogStream *self_;  // Consistency check hack
  };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM