log.h 4.9 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 19 20 21
#ifdef PADDLE_MOBILE_DEBUG
#include <iostream>
#include <sstream>
#include <string>
L
liuruilong 已提交
22
#endif
L
liuruilong 已提交
23 24 25

namespace paddle_mobile {

L
liuruilong 已提交
26 27
#ifdef PADDLE_MOBILE_DEBUG

朔-望's avatar
朔-望 已提交
28
enum LogLevel {
29 30 31 32 33 34 35 36 37
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
38 39 40 41 42 43 44 45 46 47 48 49
};

// 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 {
50 51
  friend struct ToLog;

朔-望's avatar
朔-望 已提交
52 53
  template <typename T>
  Print &operator<<(T const &value) {
54 55 56 57
    buffer_ << value;
    return *this;
  }

朔-望's avatar
朔-望 已提交
58
 private:
59 60 61 62 63 64
  void print(LogLevel level) {
    buffer_ << std::endl;
    if (level == kLOG_ERROR) {
      std::cerr << buffer_.str();
    } else {
      std::cout << buffer_.str();
朔-望's avatar
朔-望 已提交
65
    }
66 67
  }
  std::ostringstream buffer_;
朔-望's avatar
朔-望 已提交
68 69 70
};

struct ToLog {
71 72 73 74 75 76 77
  ToLog(LogLevel level = kLOG_DEBUG, const std::string &info = "")
      : level_(level) {
    unsigned blanks =
        (unsigned)(level > kLOG_DEBUG ? (level - kLOG_DEBUG) * 4 : 1);
    printer_ << logs[level] << " " << info << ":" << std::string(blanks, ' ');
  }

朔-望's avatar
朔-望 已提交
78 79
  template <typename T>
  ToLog &operator<<(T const &value) {
80 81 82 83 84 85
    printer_ << value;
    return *this;
  }

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

朔-望's avatar
朔-望 已提交
86
 private:
87 88
  LogLevel level_;
  Print printer_;
朔-望's avatar
朔-望 已提交
89
};
L
liuruilong 已提交
90 91

#define LOG(level)                                                             \
92 93 94 95 96 97 98 99 100
  if (level > paddle_mobile::log_level) {                                      \
  } else                                                                       \
    paddle_mobile::ToLog(                                                      \
        level,                                                                 \
        (std::stringstream()                                                   \
         << "[file: "                                                          \
         << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \
         << "] [line: " << __LINE__ << "] ")                                   \
            .str())
L
liuruilong 已提交
101 102

#define DLOG                                                                   \
103 104 105 106 107 108 109 110 111
  if (paddle_mobile::kLOG_DEBUG > paddle_mobile::log_level) {                  \
  } else                                                                       \
    paddle_mobile::ToLog(                                                      \
        paddle_mobile::kLOG_DEBUG,                                             \
        (std::stringstream()                                                   \
         << "[file: "                                                          \
         << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \
         << "] [line: " << __LINE__ << "] ")                                   \
            .str())
L
liuruilong 已提交
112

朔-望's avatar
朔-望 已提交
113 114 115
#define LOGF(level, format, ...)          \
  if (level > paddle_mobile::log_level) { \
  } else                                  \
116
    printf(format, ##__VA_ARGS__)
L
liuruilong 已提交
117

朔-望's avatar
朔-望 已提交
118 119 120
#define DLOGF(format, ...)                                    \
  if (paddle_mobile::kLOG_DEBUG > paddle_mobile::log_level) { \
  } else                                                      \
121
    printf(format, ##__VA_ARGS__)
L
liuruilong 已提交
122

L
liuruilong 已提交
123 124
#else

朔-望's avatar
朔-望 已提交
125
enum LogLevel {
126 127 128 129 130 131 132 133 134
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
135 136 137 138
};

struct ToLog;
struct Print {
139
  friend struct ToLog;
朔-望's avatar
朔-望 已提交
140 141
  template <typename T>
  Print &operator<<(T const &value) {}
朔-望's avatar
朔-望 已提交
142

朔-望's avatar
朔-望 已提交
143
 private:
朔-望's avatar
朔-望 已提交
144 145 146
};

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

朔-望's avatar
朔-望 已提交
149 150 151 152
  template <typename T>
  ToLog &operator<<(T const &value) {
    return *this;
  }
朔-望's avatar
朔-望 已提交
153
};
L
liuruilong 已提交
154

朔-望's avatar
朔-望 已提交
155 156 157
#define LOG(level) \
  if (true) {      \
  } else           \
158
    paddle_mobile::ToLog(level)
L
liuruilong 已提交
159

朔-望's avatar
朔-望 已提交
160 161 162
#define DLOG  \
  if (true) { \
  } else      \
163
    paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG)
L
liuruilong 已提交
164 165 166 167 168

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

#define DLOGF(format, ...)

L
liuruilong 已提交
169
#endif
L
liuruilong 已提交
170 171 172

template <typename T>
Print &operator<<(Print &printer, const std::vector<T> &v) {
L
liuruilong 已提交
173 174 175 176
  printer << "[\n ";

  for (int i = 0; i < v.size(); ++i) {
    const auto &value = v[i];
L
liuruilong 已提交
177
    printer << value << " ";
L
liuruilong 已提交
178 179 180
    if (i % 10 == 9) {
      printer << "\n";
    }
L
liuruilong 已提交
181
  }
L
liuruilong 已提交
182
  printer << " \n]";
L
liuruilong 已提交
183 184 185
  return printer;
}

L
liuruilong 已提交
186
}  // namespace paddle_mobile