logging.h 6.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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

21 22 23
#ifndef _LOGGING_H_
#define _LOGGING_H_

24
#include <assert.h>
25 26
#include <time.h>
#if !defined(_WIN32)
Y
Yan Chunwei 已提交
27 28
#include <sys/time.h>
#include <sys/types.h>
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
#else
#define NOMINMAX  // msvc max/min macro conflict with std::min/max
#include <windows.h>
extern struct timeval;
static int gettimeofday(struct timeval* tp, void* tzp) {
  time_t clock;
  struct tm tm;
  SYSTEMTIME wtm;

  GetLocalTime(&wtm);
  tm.tm_year = wtm.wYear - 1900;
  tm.tm_mon = wtm.wMonth - 1;
  tm.tm_mday = wtm.wDay;
  tm.tm_hour = wtm.wHour;
  tm.tm_min = wtm.wMinute;
  tm.tm_sec = wtm.wSecond;
  tm.tm_isdst = -1;
  clock = mktime(&tm);
  tp->tv_sec = clock;
  tp->tv_usec = wtm.wMilliseconds * 1000;

  return (0);
}
#endif

Y
Yan Chunwei 已提交
54 55 56 57
#include <cstdlib>
#include <cstring>
#include <string>
#include "lite/utils/replace_stl/stream.h"
58
#include "lite/utils/string.h"
Y
Yan Chunwei 已提交
59

60
#if defined(LITE_WITH_LOG) && defined(LITE_WITH_ANDROID)
61 62 63 64
#include <android/log.h>
// Android log macors
#define ANDROID_LOG_TAG "Paddle-Lite"
#define ANDROID_LOG_I(msg) \
65
  __android_log_print(ANDROID_LOG_INFO, ANDROID_LOG_TAG, "%s", msg)
66
#define ANDROID_LOG_W(msg) \
67
  __android_log_print(ANDROID_LOG_WARN, ANDROID_LOG_TAG, "%s", msg)
68
#define ANDROID_LOG_F(msg) \
69
  __android_log_print(ANDROID_LOG_FATAL, ANDROID_LOG_TAG, "%s", msg)
70 71
#endif

Y
Yan Chunwei 已提交
72 73 74
// NOLINTFILE()

// LOG()
75
#ifndef LITE_WITH_LOG
Y
Yan Chunwei 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#define LOG(status) LOG_##status
#define LOG_INFO paddle::lite::Voidify()
#define LOG_ERROR LOG_INFO
#define LOG_WARNING LOG_INFO
#define LOG_FATAL paddle::lite::VoidifyFatal()
#else
#define LOG(status) LOG_##status.stream()
#define LOG_INFO paddle::lite::LogMessage(__FILE__, __FUNCTION__, __LINE__, "I")
#define LOG_ERROR LOG_INFO
#define LOG_WARNING \
  paddle::lite::LogMessage(__FILE__, __FUNCTION__, __LINE__, "W")
#define LOG_FATAL \
  paddle::lite::LogMessageFatal(__FILE__, __FUNCTION__, __LINE__)
#endif

91
#ifndef LITE_WITH_LOG
Y
Yan Chunwei 已提交
92 93 94 95 96 97 98 99 100
#define VLOG(level) paddle::lite::Voidify()
#else
// VLOG()
#define VLOG(level) \
  paddle::lite::VLogMessage(__FILE__, __FUNCTION__, __LINE__, level).stream()
#endif

// CHECK()
// clang-format off
101
#ifndef LITE_WITH_LOG
Y
Yan Chunwei 已提交
102 103 104 105
#define CHECK(x) if (!(x)) paddle::lite::VoidifyFatal()
#define _CHECK_BINARY(x, cmp, y) CHECK(x cmp y)
#else
#define CHECK(x) if (!(x)) paddle::lite::LogMessageFatal(__FILE__, __FUNCTION__, __LINE__).stream() << "Check failed: " #x << ": " // NOLINT(*)
106
#define _CHECK_BINARY(x, cmp, y) CHECK((x cmp y)) << (x) << "!" #cmp << (y) << " " // NOLINT(*)
Y
Yan Chunwei 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
#endif

// clang-format on
#define CHECK_EQ(x, y) _CHECK_BINARY(x, ==, y)
#define CHECK_NE(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)

namespace paddle {
namespace lite {

120
#ifdef LITE_WITH_LOG
Y
Yan Chunwei 已提交
121 122 123 124 125
void gen_log(STL::ostream& log_stream_,
             const char* file,
             const char* func,
             int lineno,
             const char* level,
126
             const int kMaxLen = 40);
Y
Yan Chunwei 已提交
127 128 129 130 131 132 133 134

// LogMessage
class LogMessage {
 public:
  LogMessage(const char* file,
             const char* func,
             int lineno,
             const char* level = "I") {
135
    level_ = level;
Y
Yan Chunwei 已提交
136 137 138 139 140
    paddle::lite::gen_log(log_stream_, file, func, lineno, level);
  }

  ~LogMessage() {
    log_stream_ << '\n';
141 142 143 144 145
#ifdef LITE_WITH_ANDROID
    if (level_ == "I") {
      ANDROID_LOG_I(log_stream_.str().c_str());
    } else if (level_ == "W") {
      ANDROID_LOG_W(log_stream_.str().c_str());
146 147
    } else if (level_ == "F") {
      ANDROID_LOG_F(log_stream_.str().c_str());
148
    } else {
149
      fprintf(stderr, "Unsupported log level: %s\n", level_.c_str());
150 151 152
      assert(false);
    }
#endif
Y
Yan Chunwei 已提交
153 154 155 156 157 158 159
    fprintf(stderr, "%s", log_stream_.str().c_str());
  }

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

 protected:
  STL::stringstream log_stream_;
160
  std::string level_;
Y
Yan Chunwei 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174

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

// LogMessageFatal
class LogMessageFatal : public LogMessage {
 public:
  LogMessageFatal(const char* file,
                  const char* func,
                  int lineno,
                  const char* level = "F")
      : LogMessage(file, func, lineno, level) {}

175 176 177 178 179
  ~LogMessageFatal()
#ifdef LITE_WITH_EXCEPTION
      noexcept(false)
#endif
  {
Y
Yan Chunwei 已提交
180
    log_stream_ << '\n';
181 182 183
#ifdef LITE_WITH_ANDROID
    ANDROID_LOG_F(log_stream_.str().c_str());
#endif
Y
Yan Chunwei 已提交
184
    fprintf(stderr, "%s", log_stream_.str().c_str());
185

186 187 188
#ifdef LITE_WITH_EXCEPTION
    throw std::exception();
#else
Y
Yan Chunwei 已提交
189 190 191 192
#ifndef LITE_ON_TINY_PUBLISH
    abort();
#else
    assert(false);
193
#endif
Y
Yan Chunwei 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
#endif
  }
};

// VLOG
class VLogMessage {
 public:
  VLogMessage(const char* file,
              const char* func,
              int lineno,
              const int32_t level_int = 0) {
    const char* GLOG_v = std::getenv("GLOG_v");
    GLOG_v_int = (GLOG_v && atoi(GLOG_v) > 0) ? atoi(GLOG_v) : 0;
    this->level_int = level_int;
    if (GLOG_v_int < level_int) {
      return;
    }
211
    const char* level = paddle::lite::to_string(level_int).c_str();
Y
Yan Chunwei 已提交
212 213 214 215 216 217 218 219
    paddle::lite::gen_log(log_stream_, file, func, lineno, level);
  }

  ~VLogMessage() {
    if (GLOG_v_int < this->level_int) {
      return;
    }
    log_stream_ << '\n';
220 221 222
#ifdef LITE_WITH_ANDROID
    ANDROID_LOG_I(log_stream_.str().c_str());
#endif
Y
Yan Chunwei 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    fprintf(stderr, "%s", log_stream_.str().c_str());
  }

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

 protected:
  STL::stringstream log_stream_;
  int32_t GLOG_v_int;
  int32_t level_int;

  VLogMessage(const VLogMessage&) = delete;
  void operator=(const VLogMessage&) = delete;
};
#else
class Voidify {
 public:
  Voidify() {}
  ~Voidify() {}

  template <typename T>
  Voidify& operator<<(const T& obj) {
    return *this;
  }
};

class VoidifyFatal : public Voidify {
 public:
250 251 252
#ifdef LITE_WITH_EXCEPTION
  ~VoidifyFatal() noexcept(false) { throw std::exception(); }
#else
Y
Yan Chunwei 已提交
253
  ~VoidifyFatal() { assert(false); }
254
#endif
Y
Yan Chunwei 已提交
255 256 257 258 259 260
};

#endif

}  // namespace lite
}  // namespace paddle
261
#endif