logging.h 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
 * This file implements an lightweight alternative for glog, which is more
 * friendly for mobile.
 */
#pragma once
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <sstream>

// LOG()
#define LOG(status) LOG_##status.stream()
#define LOG_ERROR LOG_INFO
#define LOG_INFO paddle::lite::LogMessage(__FILE__, __LINE__)
#define LOG_WARNING paddle::lite::LogMessage(__FILE__, __LINE__)
#define LOG_FATAL paddle::lite::LogMessageFatal(__FILE__, __LINE__)
// Not supported yet.
#define VLOG(level) LOG_INFO.stream()

// CHECK()
#define CHECK(x)                                             \
  if (!(x))                                                  \
  paddle::lite::LogMessageFatal(__FILE__, __LINE__).stream() \
      << "Check failed: " #x << ": "
#define CHECK_EQ(x, y) _CHECK_BINARY(x, ==, y)
#define CHECK_LT(x, y) _CHECK_BINARY(x, <, y)
#define CHECK_LE(x, y) _CHECK_BINARY(x, <=, y)
#define CHECK_GT(x, y) _CHECK_BINARY(x, >, y)
#define CHECK_GE(x, y) _CHECK_BINARY(x, >=, y)
#define _CHECK_BINARY(x, cmp, y) CHECK(x cmp y) << x << "!" #cmp << y << " "

namespace paddle {
namespace lite {

class LogMessage {
 public:
  LogMessage(const char* file, int lineno) {
    const int kMaxLen = 20;
    const int len = strlen(file);
    if (len > kMaxLen) {
      log_stream_ << '[' << "..." << file + len - kMaxLen << ":" << lineno
                  << "] ";
    } else {
      log_stream_ << '[' << file << ":" << lineno << "] ";
    }
  }

  ~LogMessage() {
    log_stream_ << '\n';
    std::cerr << log_stream_.str();
  }

  std::ostream& stream() { return log_stream_; }

 protected:
  std::stringstream log_stream_;

  LogMessage(const LogMessage&) = delete;
  void operator=(const LogMessage&) = delete;
};

class LogMessageFatal : public LogMessage {
 public:
  LogMessageFatal(const char* file, int lineno) : LogMessage(file, lineno) {}

  ~LogMessageFatal() {
    log_stream_ << '\n';
    std::cerr << log_stream_.str();
    abort();
  }
};

}  // namespace lite
}  // namespace paddle