log.h 6.2 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__;
W
wangliu 已提交
36

W
wangliu 已提交
37
#define ANDROIDLOGI(...)                                               \
W
wangliu 已提交
38
  __android_log_print(ANDROID_LOG_INFO, ANDROID_LOG_TAG, __VA_ARGS__); \
39
  printf("%s\n", __VA_ARGS__);
W
wangliu 已提交
40
#define ANDROIDLOGW(...)                                                  \
W
wangliu 已提交
41
  __android_log_print(ANDROID_LOG_WARNING, ANDROID_LOG_TAG, __VA_ARGS__); \
42
  printf("%s\n", __VA_ARGS__);
W
wangliu 已提交
43
#define ANDROIDLOGD(...)                                                \
W
wangliu 已提交
44
  __android_log_print(ANDROID_LOG_DEBUG, ANDROID_LOG_TAG, __VA_ARGS__); \
45
  printf("%s\n", __VA_ARGS__)
W
wangliu 已提交
46
#define ANDROIDLOGE(...)                                                \
W
wangliu 已提交
47
  __android_log_print(ANDROID_LOG_ERROR, ANDROID_LOG_TAG, __VA_ARGS__); \
48
  printf("%s\n", __VA_ARGS__)
W
wangliu 已提交
49 50 51 52 53
#else
#define ANDROIDLOGI(...)
#define ANDROIDLOGW(...)
#define ANDROIDLOGD(...)
#define ANDROIDLOGE(...)
W
wangliu 已提交
54 55 56

#endif

朔-望's avatar
朔-望 已提交
57
enum LogLevel {
58 59 60 61 62 63 64 65 66
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
67 68 69 70 71 72 73 74 75 76 77 78
};

// log level
static LogLevel log_level = kLOG_DEBUG4;

static std::vector<std::string> logs{"NO",      "ERROR ",  "WARNING",
                                     "INFO   ", "DEBUG  ", "DEBUG1 ",
                                     "DEBUG2 ", "DEBUG3 ", "DEBUG4 "};
struct ToLog;
struct Print;

struct Print {
79 80
  friend struct ToLog;

朔-望's avatar
朔-望 已提交
81 82
  template <typename T>
  Print &operator<<(T const &value) {
83 84 85 86
    buffer_ << value;
    return *this;
  }

朔-望's avatar
朔-望 已提交
87
 private:
88
  void print(LogLevel level) {
89
    // buffer_ << std::endl;
90
    if (level == kLOG_ERROR) {
91 92 93
#ifdef ANDROID
      ANDROIDLOGE(buffer_.str().c_str());
#else
94
      std::cerr << buffer_.str() << std::endl;
95
#endif
96
    } else {
97 98 99
#ifdef ANDROID
      ANDROIDLOGI(buffer_.str().c_str());
#else
100
      std::cout << buffer_.str() << std::endl;
101
#endif
朔-望's avatar
朔-望 已提交
102
    }
103 104
  }
  std::ostringstream buffer_;
朔-望's avatar
朔-望 已提交
105 106 107
};

struct ToLog {
108
  explicit ToLog(LogLevel level = kLOG_DEBUG, const std::string &info = "")
109 110 111 112 113 114
      : level_(level) {
    unsigned blanks =
        (unsigned)(level > kLOG_DEBUG ? (level - kLOG_DEBUG) * 4 : 1);
    printer_ << logs[level] << " " << info << ":" << std::string(blanks, ' ');
  }

朔-望's avatar
朔-望 已提交
115 116
  template <typename T>
  ToLog &operator<<(T const &value) {
117 118 119 120 121 122
    printer_ << value;
    return *this;
  }

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

朔-望's avatar
朔-望 已提交
123
 private:
124 125
  LogLevel level_;
  Print printer_;
朔-望's avatar
朔-望 已提交
126
};
L
liuruilong 已提交
127

D
dolphin8 已提交
128 129 130 131
#define LOG(level)                                                           \
  if (level > paddle_mobile::log_level) {                                    \
  } else                                                                     \
    paddle_mobile::ToLog(                                                    \
W
wangliu 已提交
132
        level, static_cast<const std::stringstream &>(                       \
D
dolphin8 已提交
133 134 135 136 137 138 139 140 141 142 143 144
                   std::stringstream()                                       \
                   << "[file: "                                              \
                   << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) \
                                              : __FILE__)                    \
                   << "] [line: " << __LINE__ << "] ")                       \
                   .str())

#define DLOG                                                          \
  if (paddle_mobile::kLOG_DEBUG > paddle_mobile::log_level) {         \
  } else                                                              \
    paddle_mobile::ToLog(                                             \
        paddle_mobile::kLOG_DEBUG,                                    \
W
wangliu 已提交
145
        static_cast<const std::stringstream &>(                       \
D
dolphin8 已提交
146 147 148 149 150
            std::stringstream()                                       \
            << "[file: "                                              \
            << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) \
                                       : __FILE__)                    \
            << "] [line: " << __LINE__ << "] ")                       \
151
            .str())
L
liuruilong 已提交
152

朔-望's avatar
朔-望 已提交
153 154 155
#define LOGF(level, format, ...)          \
  if (level > paddle_mobile::log_level) { \
  } else                                  \
156
    printf(format, ##__VA_ARGS__)
L
liuruilong 已提交
157

朔-望's avatar
朔-望 已提交
158 159 160
#define DLOGF(format, ...)                                    \
  if (paddle_mobile::kLOG_DEBUG > paddle_mobile::log_level) { \
  } else                                                      \
161
    printf(format, ##__VA_ARGS__)
L
liuruilong 已提交
162

L
liuruilong 已提交
163 164
#else

W
wangliu 已提交
165 166 167 168 169
#define ANDROIDLOGI(...)
#define ANDROIDLOGW(...)
#define ANDROIDLOGD(...)
#define ANDROIDLOGE(...)

朔-望's avatar
朔-望 已提交
170
enum LogLevel {
171 172 173 174 175 176 177 178 179
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
180 181 182 183
};

struct ToLog;
struct Print {
184
  friend struct ToLog;
朔-望's avatar
朔-望 已提交
185
  template <typename T>
186
  Print &operator<<(T const &value) {
187
    return *this;
188
  }
朔-望's avatar
朔-望 已提交
189 190 191
};

struct ToLog {
192
  ToLog(LogLevel level) {}
朔-望's avatar
朔-望 已提交
193

朔-望's avatar
朔-望 已提交
194 195 196 197
  template <typename T>
  ToLog &operator<<(T const &value) {
    return *this;
  }
朔-望's avatar
朔-望 已提交
198
};
L
liuruilong 已提交
199

朔-望's avatar
朔-望 已提交
200 201 202
#define LOG(level) \
  if (true) {      \
  } else           \
203
    paddle_mobile::ToLog(level)
L
liuruilong 已提交
204

朔-望's avatar
朔-望 已提交
205 206 207
#define DLOG  \
  if (true) { \
  } else      \
208
    paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG)
L
liuruilong 已提交
209 210 211 212 213

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

#define DLOGF(format, ...)

L
liuruilong 已提交
214
#endif
L
liuruilong 已提交
215 216 217

template <typename T>
Print &operator<<(Print &printer, const std::vector<T> &v) {
L
liuruilong 已提交
218
  printer << "[ ";
L
liuruilong 已提交
219 220 221

  for (int i = 0; i < v.size(); ++i) {
    const auto &value = v[i];
L
liuruilong 已提交
222
    printer << value << " ";
L
liuruilong 已提交
223 224 225
    if (i % 10 == 9) {
      printer << "\n";
    }
L
liuruilong 已提交
226
  }
L
liuruilong 已提交
227
  printer << " ]";
L
liuruilong 已提交
228 229 230
  return printer;
}

L
liuruilong 已提交
231
}  // namespace paddle_mobile