log.h 8.1 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 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. */

L
liuruilong 已提交
15 16
#pragma once

L
liuruilong 已提交
17
#include <vector>
L
liuruilong 已提交
18
#ifdef PADDLE_MOBILE_DEBUG
D
dolphin8 已提交
19
#include <cstring>
L
liuruilong 已提交
20 21 22
#include <iostream>
#include <sstream>
#include <string>
L
liuruilong 已提交
23
#endif
W
wangliu 已提交
24 25 26
#ifdef ANDROID
#include <android/log.h>
#endif
L
liuruilong 已提交
27 28 29

namespace paddle_mobile {

L
liuruilong 已提交
30 31
#ifdef PADDLE_MOBILE_DEBUG

W
wangliu 已提交
32 33
#ifdef ANDROID

H
update  
hjchen2 已提交
34 35
static const char *ANDROID_LOG_TAG =
    "paddle_mobile LOG built on " __DATE__ " " __TIME__;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#ifdef PADDLE_ENABLE_COLORABLE_LOG
#define PADDLE_RED "\033[1;31;40m"
#define PADDLE_GREEN "\033[1;32;40m"
#define PADDLE_YELLOW "\033[1;33;40m"
#define PADDLE_LIGHT_RED "\033[1;35;40m"
#define PADDLE_BLUE "\033[1;34;40m"
#define PADDLE_WHITE "\033[1;37;40m"
#define PADDLE_CONON "\033[0m"
#else
#define PADDLE_RED ""
#define PADDLE_GREEN ""
#define PADDLE_YELLOW ""
#define PADDLE_LIGHT_RED ""
#define PADDLE_BLUE ""
#define PADDLE_WHITE ""
#define PADDLE_CONON ""
#endif
W
wangliu 已提交
53
#define ANDROIDLOGI(...)                                               \
W
wangliu 已提交
54
  __android_log_print(ANDROID_LOG_INFO, ANDROID_LOG_TAG, __VA_ARGS__); \
55
  fprintf(stderr, PADDLE_YELLOW "%s\n" PADDLE_CONON, __VA_ARGS__);     \
56
  fflush(stderr)
57 58 59
#define ANDROIDLOGW(...)                                               \
  __android_log_print(ANDROID_LOG_WARN, ANDROID_LOG_TAG, __VA_ARGS__); \
  fprintf(stderr, PADDLE_LIGHT_RED "%s\n" PADDLE_CONON, __VA_ARGS__);  \
60
  fflush(stderr)
W
wangliu 已提交
61
#define ANDROIDLOGD(...)                                                \
W
wangliu 已提交
62
  __android_log_print(ANDROID_LOG_DEBUG, ANDROID_LOG_TAG, __VA_ARGS__); \
63
  fprintf(stderr, PADDLE_WHITE "%s\n" PADDLE_CONON, __VA_ARGS__);       \
64
  fflush(stderr)
W
wangliu 已提交
65
#define ANDROIDLOGE(...)                                                \
W
wangliu 已提交
66
  __android_log_print(ANDROID_LOG_ERROR, ANDROID_LOG_TAG, __VA_ARGS__); \
67 68 69 70 71
  fprintf(stderr, PADDLE_RED "%s\n" PADDLE_CONON, __VA_ARGS__);         \
  fflush(stderr)
#define ANDROIDLOGV(...)                                                  \
  __android_log_print(ANDROID_LOG_VERBOSE, ANDROID_LOG_TAG, __VA_ARGS__); \
  fprintf(stderr, PADDLE_GREEN "%s\n" PADDLE_CONON, __VA_ARGS__);         \
72
  fflush(stderr)
W
wangliu 已提交
73 74 75 76 77
#else
#define ANDROIDLOGI(...)
#define ANDROIDLOGW(...)
#define ANDROIDLOGD(...)
#define ANDROIDLOGE(...)
78
#define ANDROIDLOGV(...)
W
wangliu 已提交
79 80 81

#endif

朔-望's avatar
朔-望 已提交
82
enum LogLevel {
83 84 85 86
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
87
  kLOG_VERBOSE,
88 89 90 91 92
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
93 94 95 96 97
};

// log level
static LogLevel log_level = kLOG_DEBUG4;

98 99 100
static std::vector<std::string> logs{"NO     ", "ERROR  ", "WARNING", "INFO   ",
                                     "VERBOSE", "DEBUG  ", "DEBUG1 ", "DEBUG2 ",
                                     "DEBUG3 ", "DEBUG4 "};
朔-望's avatar
朔-望 已提交
101 102 103 104
struct ToLog;
struct Print;

struct Print {
105 106
  friend struct ToLog;

朔-望's avatar
朔-望 已提交
107 108
  template <typename T>
  Print &operator<<(T const &value) {
109 110 111 112
    buffer_ << value;
    return *this;
  }

朔-望's avatar
朔-望 已提交
113
 private:
114
  void print(LogLevel level) {
115
    // buffer_ << std::endl;
116
    if (level == kLOG_ERROR) {
117 118 119
#ifdef ANDROID
      ANDROIDLOGE(buffer_.str().c_str());
#else
120
      std::cerr << buffer_.str() << std::endl;
121
#endif
122
    } else if (level == kLOG_INFO) {
123 124
#ifdef ANDROID
      ANDROIDLOGI(buffer_.str().c_str());
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#else
      std::cerr << buffer_.str() << std::endl;
#endif
    } else if (level == kLOG_VERBOSE) {
#ifdef ANDROID
      ANDROIDLOGV(buffer_.str().c_str());
#else
      std::cerr << buffer_.str() << std::endl;
#endif
    } else if (level == kLOG_WARNING) {
#ifdef ANDROID
      ANDROIDLOGW(buffer_.str().c_str());
#else
      std::cerr << buffer_.str() << std::endl;
#endif
    } else {
#ifdef ANDROID
      ANDROIDLOGD(buffer_.str().c_str());
143
#else
144
      std::cout << buffer_.str() << std::endl;
145
#endif
朔-望's avatar
朔-望 已提交
146
    }
147 148
  }
  std::ostringstream buffer_;
朔-望's avatar
朔-望 已提交
149 150 151
};

struct ToLog {
152
  explicit ToLog(LogLevel level = kLOG_DEBUG, const std::string &info = "")
153 154 155 156 157 158
      : level_(level) {
    unsigned blanks =
        (unsigned)(level > kLOG_DEBUG ? (level - kLOG_DEBUG) * 4 : 1);
    printer_ << logs[level] << " " << info << ":" << std::string(blanks, ' ');
  }

朔-望's avatar
朔-望 已提交
159 160
  template <typename T>
  ToLog &operator<<(T const &value) {
161 162 163 164 165 166
    printer_ << value;
    return *this;
  }

  ~ToLog() { printer_.print(level_); }

朔-望's avatar
朔-望 已提交
167
 private:
168 169
  LogLevel level_;
  Print printer_;
朔-望's avatar
朔-望 已提交
170
};
L
liuruilong 已提交
171

D
dolphin8 已提交
172 173
#define LOG(level)                                                           \
  if (level > paddle_mobile::log_level) {                                    \
174
    /* NOLINTNEXTLINE */                                                     \
D
dolphin8 已提交
175 176
  } else                                                                     \
    paddle_mobile::ToLog(                                                    \
W
wangliu 已提交
177
        level, static_cast<const std::stringstream &>(                       \
D
dolphin8 已提交
178 179 180 181 182 183 184 185 186
                   std::stringstream()                                       \
                   << "[file: "                                              \
                   << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) \
                                              : __FILE__)                    \
                   << "] [line: " << __LINE__ << "] ")                       \
                   .str())

#define DLOG                                                          \
  if (paddle_mobile::kLOG_DEBUG > paddle_mobile::log_level) {         \
187
    /* NOLINTNEXTLINE */                                              \
D
dolphin8 已提交
188 189 190
  } else                                                              \
    paddle_mobile::ToLog(                                             \
        paddle_mobile::kLOG_DEBUG,                                    \
W
wangliu 已提交
191
        static_cast<const std::stringstream &>(                       \
D
dolphin8 已提交
192 193 194 195 196
            std::stringstream()                                       \
            << "[file: "                                              \
            << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) \
                                       : __FILE__)                    \
            << "] [line: " << __LINE__ << "] ")                       \
197
            .str())
L
liuruilong 已提交
198

朔-望's avatar
朔-望 已提交
199 200
#define LOGF(level, format, ...)          \
  if (level > paddle_mobile::log_level) { \
201
    /* NOLINTNEXTLINE */                  \
朔-望's avatar
朔-望 已提交
202
  } else                                  \
203
    printf(format, ##__VA_ARGS__)
L
liuruilong 已提交
204

朔-望's avatar
朔-望 已提交
205 206
#define DLOGF(format, ...)                                    \
  if (paddle_mobile::kLOG_DEBUG > paddle_mobile::log_level) { \
207
    /* NOLINTNEXTLINE */                                      \
朔-望's avatar
朔-望 已提交
208
  } else                                                      \
209
    printf(format, ##__VA_ARGS__)
L
liuruilong 已提交
210

L
liuruilong 已提交
211 212
#else

W
wangliu 已提交
213 214 215 216
#define ANDROIDLOGI(...)
#define ANDROIDLOGW(...)
#define ANDROIDLOGD(...)
#define ANDROIDLOGE(...)
217
#define ANDROIDLOGV(...)
W
wangliu 已提交
218

朔-望's avatar
朔-望 已提交
219
enum LogLevel {
220 221 222 223
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
224
  kLOG_VERBOSE,
225 226 227 228 229
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
230 231 232 233
};

struct ToLog;
struct Print {
234
  friend struct ToLog;
朔-望's avatar
朔-望 已提交
235
  template <typename T>
236
  Print &operator<<(T const &value) {
237
    return *this;
238
  }
朔-望's avatar
朔-望 已提交
239 240 241
};

struct ToLog {
242
  explicit ToLog(LogLevel level) {}
朔-望's avatar
朔-望 已提交
243

朔-望's avatar
朔-望 已提交
244 245 246 247
  template <typename T>
  ToLog &operator<<(T const &value) {
    return *this;
  }
朔-望's avatar
朔-望 已提交
248
};
L
liuruilong 已提交
249

250 251 252 253
#define LOG(level)       \
  if (true) {            \
    /* NOLINTNEXTLINE */ \
  } else                 \
254
    paddle_mobile::ToLog(level)
L
liuruilong 已提交
255

256 257 258 259
#define DLOG             \
  if (true) {            \
    /* NOLINTNEXTLINE */ \
  } else                 \
260
    paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG)
L
liuruilong 已提交
261 262 263 264 265

#define LOGF(level, format, ...)

#define DLOGF(format, ...)

L
liuruilong 已提交
266
#endif
L
liuruilong 已提交
267 268 269

template <typename T>
Print &operator<<(Print &printer, const std::vector<T> &v) {
L
liuruilong 已提交
270
  printer << "[ ";
L
liuruilong 已提交
271 272 273

  for (int i = 0; i < v.size(); ++i) {
    const auto &value = v[i];
L
liuruilong 已提交
274
    printer << value << " ";
L
liuruilong 已提交
275 276 277
    if (i % 10 == 9) {
      printer << "\n";
    }
L
liuruilong 已提交
278
  }
L
liuruilong 已提交
279
  printer << " ]";
L
liuruilong 已提交
280 281 282
  return printer;
}

L
liuruilong 已提交
283
}  // namespace paddle_mobile