~fsx/protobluff

5e6b2d22394b3d71059dd7c67c54ed584e6ec98d — Frank Smit 10 months ago 26c24b8
Replace protobuf's logging. It's not available anymore.
M meson.build => meson.build +1 -0
@@ 17,6 17,7 @@ libprotoc = cpp.find_library(
  dirs: libprotobuf.get_pkgconfig_variable('libdir'),
)
libcheck = dependency('check')
libfmt = dependency('fmt')

public_includes = include_directories('include')
internal_includes = include_directories('src')

A src/generator/logging.hh => src/generator/logging.hh +117 -0
@@ 0,0 1,117 @@
#ifndef PB_GENERATOR_LOGGER_HH
#define PB_GENERATOR_LOGGER_HH

#include <exception>
#include <cassert>

#include <fmt/core.h>
#include <fmt/ostream.h>

#define GOOGLE_LOG(LEVEL) \
  protobluff::LogFinisher() = \
      protobluff::LogMessage( \
          protobluff::LogLevel::LEVEL, __FILE__, __LINE__)

#define GOOGLE_LOG_IF(LEVEL, CONDITION) \
  !(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL)

#define GOOGLE_CHECK(EXPRESSION) \
  GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": "

#define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B))
#define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B))
#define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) <  (B))
#define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B))
#define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) >  (B))
#define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B))

#define GOOGLE_DLOG GOOGLE_LOG

#define GOOGLE_DCHECK    GOOGLE_CHECK
#define GOOGLE_DCHECK_OK GOOGLE_CHECK_OK
#define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ
#define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE
#define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT
#define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE
#define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT
#define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE

namespace protobluff { enum class LogLevel; }

template <> struct fmt::formatter<protobluff::LogLevel> : ostream_formatter {};

namespace protobluff {
  enum class LogLevel {
    INFO,
    WARNING,
    ERROR,
    FATAL,
  };

  std::ostream& operator<<(std::ostream& os, LogLevel level)
  {
    if (level == LogLevel::INFO) {
      os << "INFO";
    } else if (level == LogLevel::WARNING) {
      os << "WARNING";
    } else if (level == LogLevel::ERROR) {
      os << "ERROR";
    } else if (level == LogLevel::FATAL) {
      os << "FATAL";
    } else {
      assert(false);
    }

    return os;
  }

  class LogFinisher;

  class LogMessage {
   public:
    LogMessage(LogLevel level, const char* filename, int line)
      : level_(level), filename_(filename), line_(line) {}
    ~LogMessage() {}

    LogMessage& operator<<(const std::string& value) {
      message_ += value;
      return *this;
    }

    LogMessage& operator<<(const char* value) {
      message_ += value;
      return *this;
    }

    LogMessage& operator<<(char value) {
      return *this << std::string(1, value);
    }

    LogMessage& operator<<(int value) {
      return *this << std::to_string(value);
    }

   private:
    friend class LogFinisher;
    void Finish() {
      fmt::print(stderr, "{}: {}:{}: {}", level_, filename_, line_, message_);
      if (level_ == LogLevel::FATAL) {
        std::terminate();
      }
    }

    LogLevel level_;
    const char* filename_;
    int line_;
    std::string message_;
  };

  class LogFinisher {
   public:
    void operator=(LogMessage& other) {
      other.Finish();
    }
  };
}

#endif  // PB_GENERATOR_LOGGER_HH

M src/generator/meson.build => src/generator/meson.build +1 -1
@@ 11,5 11,5 @@ protoc_gen_protobluff = executable(
  'protoc-gen-protobluff.cc',
  'strutil.cc',
  include_directories: [public_includes, internal_includes],
  dependencies: [libprotobuf, libprotoc],
  dependencies: [libprotobuf, libprotoc, libfmt],
)

M src/generator/strutil.cc => src/generator/strutil.cc +1 -0
@@ 38,6 38,7 @@
#include <iterator>

#include "strutil.hh"
#include "logging.hh"

#ifdef _WIN32
// MSVC has only _snprintf, not snprintf.

M src/generator/strutil.hh => src/generator/strutil.hh +0 -1
@@ 36,7 36,6 @@
#include <stdlib.h>
#include <vector>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/logging.h>

namespace google {
namespace protobuf {