log.h 5.1 KB
Newer Older
L
liuruilong 已提交
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
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/

#pragma once

#ifdef PADDLE_MOBILE_DEBUG

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

namespace paddle_mobile {

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

// 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 {
52 53
  friend struct ToLog;

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

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

struct ToLog {
73 74 75 76 77 78 79
  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
朔-望 已提交
80 81
  template <typename T>
  ToLog &operator<<(T const &value) {
82 83 84 85 86 87
    printer_ << value;
    return *this;
  }

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

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

#define LOG(level)                                                             \
94 95 96 97 98 99 100 101 102
  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 已提交
103 104

#define DLOG                                                                   \
105 106 107 108 109 110 111 112 113
  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())
朔-望's avatar
朔-望 已提交
114
}  // namespace paddle_mobile
L
liuruilong 已提交
115

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

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

L
liuruilong 已提交
126 127 128 129
#else

namespace paddle_mobile {

朔-望's avatar
朔-望 已提交
130
enum LogLevel {
131 132 133 134 135 136 137 138 139
  kNO_LOG,
  kLOG_ERROR,
  kLOG_WARNING,
  kLOG_INFO,
  kLOG_DEBUG,
  kLOG_DEBUG1,
  kLOG_DEBUG2,
  kLOG_DEBUG3,
  kLOG_DEBUG4
朔-望's avatar
朔-望 已提交
140 141 142 143
};

struct ToLog;
struct Print {
144
  friend struct ToLog;
朔-望's avatar
朔-望 已提交
145 146
  template <typename T>
  Print &operator<<(T const &value) {}
朔-望's avatar
朔-望 已提交
147

朔-望's avatar
朔-望 已提交
148
 private:
朔-望's avatar
朔-望 已提交
149 150 151
};

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

朔-望's avatar
朔-望 已提交
154 155 156 157
  template <typename T>
  ToLog &operator<<(T const &value) {
    return *this;
  }
朔-望's avatar
朔-望 已提交
158
};
L
liuruilong 已提交
159

朔-望's avatar
朔-望 已提交
160 161 162
#define LOG(level) \
  if (true) {      \
  } else           \
163
    paddle_mobile::ToLog(level)
L
liuruilong 已提交
164

朔-望's avatar
朔-望 已提交
165 166 167
#define DLOG  \
  if (true) { \
  } else      \
168
    paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG)
L
liuruilong 已提交
169 170 171 172

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

#define DLOGF(format, ...)
朔-望's avatar
朔-望 已提交
173
}  // namespace paddle_mobile
L
liuruilong 已提交
174

L
liuruilong 已提交
175
#endif