github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/log.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #pragma once
    22  
    23  #include <glog/logging.h>
    24  #include <iostream>
    25  #include <sstream>
    26  
    27  namespace neb {
    28  
    29  enum log_level_t {
    30    log_DEBUG = 0,
    31    log_INFO,
    32    log_WARNING,
    33    log_ERROR,
    34  };
    35  
    36  class log_flush_base {
    37  public:
    38    log_flush_base() = default;
    39    virtual ~log_flush_base() = default;
    40  
    41    template <typename T> log_flush_base &operator<<(T const &msg) {
    42      m_oss << msg;
    43      return *this;
    44    }
    45  
    46  protected:
    47    std::ostringstream m_oss;
    48  };
    49  
    50  template <log_level_t TL> class log_flush : public log_flush_base {};
    51  template <> class log_flush<log_INFO> : public log_flush_base {
    52  public:
    53    virtual ~log_flush() {
    54      LOG(INFO) << m_oss.str();
    55      google::FlushLogFiles(google::INFO);
    56    }
    57  };
    58  template <> class log_flush<log_WARNING> : public log_flush_base {
    59  public:
    60    virtual ~log_flush() {
    61      LOG(WARNING) << m_oss.str();
    62      google::FlushLogFiles(google::WARNING);
    63    }
    64  };
    65  template <> class log_flush<log_ERROR> : public log_flush_base {
    66  public:
    67    virtual ~log_flush() {
    68      LOG(ERROR) << m_oss.str();
    69      google::FlushLogFiles(google::ERROR);
    70    }
    71  };
    72  
    73  #define LOG_FLUSH(LEVEL) log_flush<log_##LEVEL>()
    74  
    75  } // namespace neb