proxygen
Logging.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <folly/Optional.h>
13 #include <folly/io/IOBuf.h>
14 #include <sstream>
15 #include <string>
16 
17 namespace proxygen {
18 
19 namespace logging_details {
21 }
22 
23 class NullStream final : public std::ostream {
24  public:
25  NullStream() = default;
26  NullStream(const NullStream&) = default;
28  return *this;
29  }
30 };
31 
32 template <typename T>
34  private:
35  using StringStreamPair = std::pair<std::string, std::ostringstream>;
36  public:
38  const bool checkPassed,
39  const char* checkString,
40  const char* file,
41  const int line,
42  const int logLevel)
43  : file_(file),
44  line_(line),
45  logLevel_(logLevel) {
46  if (checkPassed) {
47  nullStream_.emplace();
48  } else {
49  traceAndLogStreamPair_ = std::make_unique<StringStreamPair>();
50  traceAndLogStreamPair_->first = logging_details::getStackTrace();
51  traceAndLogStreamPair_->second << checkString;
52  }
53  }
54  std::ostream& stream() {
55  if (nullStream_) {
56  return nullStream_.value();
57  }
58  return traceAndLogStreamPair_->second;
59  }
61  if (!nullStream_) {
62  google::LogMessage(file_, line_, logLevel_).stream()
63  << traceAndLogStreamPair_->second.str()
64  << "\nCall stack:\n"
65  << traceAndLogStreamPair_->first;
66  throw T(traceAndLogStreamPair_->second.str());
67  }
68  }
69  private:
70  const char* file_;
71  int line_;
72  int logLevel_;
73  std::unique_ptr<StringStreamPair> traceAndLogStreamPair_;
75 };
76 
77 template<class T>
78 inline NullStream& operator<<(NullStream &ns, const T & /* ignored */) {
79  return ns;
80 }
81 
82 #define CHECK_LOG_AND_THROW(CONDITION, LOG_LEVEL, EXCEPTION) \
83  (StackTracePrinterWithException<EXCEPTION>( \
84  (CONDITION), \
85  "Check failed \"" #CONDITION "\": ", \
86  __FILE__, \
87  __LINE__, \
88  google::GLOG_##LOG_LEVEL)).stream()
89 
90 #define CHECK_LOG_AND_THROW_LT(X, Y, LOG_LEVEL, EXCEPTION) \
91  CHECK_LOG_AND_THROW((X) < (Y), LOG_LEVEL, EXCEPTION)
92 
93 #define CHECK_LOG_AND_THROW_LE(X, Y, LOG_LEVEL, EXCEPTION) \
94  CHECK_LOG_AND_THROW((X) <= (Y), LOG_LEVEL, EXCEPTION)
95 
96 #define CHECK_LOG_AND_THROW_GT(X, Y, LOG_LEVEL, EXCEPTION) \
97  CHECK_LOG_AND_THROW((X) > (Y), LOG_LEVEL, EXCEPTION)
98 
99 #define CHECK_LOG_AND_THROW_GE(X, Y, LOG_LEVEL, EXCEPTION) \
100  CHECK_LOG_AND_THROW((X) >= (Y), LOG_LEVEL, EXCEPTION)
101 
102 #define CHECK_LOG_AND_THROW_EQ(X, Y, LOG_LEVEL, EXCEPTION) \
103  CHECK_LOG_AND_THROW((X) == (Y), LOG_LEVEL, EXCEPTION)
104 
105 #define CHECK_LOG_AND_THROW_NE(X, Y, LOG_LEVEL, EXCEPTION) \
106  CHECK_LOG_AND_THROW((X) != (Y), LOG_LEVEL, EXCEPTION)
107 
108 #define CHECK_LOG_AND_THROW_NOT_NULL(X, LOG_LEVEL, EXCEPTION) \
109  CHECK_LOG_AND_THROW((X) != nullptr, LOG_LEVEL, EXCEPTION)
110 
111 #define CHECK_LOG_AND_THROW_NULL(X, LOG_LEVEL, EXCEPTION) \
112  CHECK_LOG_AND_THROW((X) == nullptr, LOG_LEVEL, EXCEPTION)
113 
115  public:
116  enum class Format : uint8_t {
117  HEX_FOLLY = 0,
118  HEX_16 = 1,
119  CHAIN_INFO = 2,
120  BIN = 3,
121  };
122 
123  static std::string printChain(const folly::IOBuf* buf,
124  Format format,
125  bool coalesce);
126 
128  bool coalesce=false) {
129  return printChain(buf, Format::HEX_FOLLY, coalesce);
130  }
131 
132  static std::string printHex16(const folly::IOBuf* buf, bool coalesce=false) {
133  return printChain(buf, Format::HEX_16, coalesce);
134  }
135 
137  return printChain(buf, Format::CHAIN_INFO, false);
138  }
139 
140  static std::string printBin(const folly::IOBuf* buf, bool coalesce=false) {
141  return printChain(buf, Format::BIN, coalesce);
142  }
143 
145  virtual ~IOBufPrinter() {}
146 
147  virtual std::string print(const folly::IOBuf* buf) = 0;
148 };
149 
150 class Hex16Printer : public IOBufPrinter {
151  public:
152  std::string print(const folly::IOBuf* buf) override;
153 };
154 
156  public:
157  std::string print(const folly::IOBuf* buf) override;
158 };
159 
161  public:
162  std::string print(const folly::IOBuf* buf) override;
163 };
164 
165 class BinPrinter : public IOBufPrinter {
166  public:
167  std::string print(const folly::IOBuf* buf) override;
168 };
169 
173 void dumpBinToFile(const std::string& filename, const folly::IOBuf* buf);
174 
180 
181 }
folly::Optional< NullStream > nullStream_
Definition: Logging.h:74
#define T(v)
Definition: http_parser.c:233
~StackTracePrinterWithException() noexcept(false)
Definition: Logging.h:60
void dumpBinToFile(const string &filename, const IOBuf *buf)
Definition: Logging.cpp:132
static std::string printHexFolly(const folly::IOBuf *buf, bool coalesce=false)
Definition: Logging.h:127
std::ostream & operator<<(std::ostream &os, const HeaderTable &table)
string hexStr(StringPiece sp)
Definition: Logging.cpp:47
static std::string printHex16(const folly::IOBuf *buf, bool coalesce=false)
Definition: Logging.h:132
std::string getStackTrace()
Definition: Logging.cpp:159
static std::string printBin(const folly::IOBuf *buf, bool coalesce=false)
Definition: Logging.h:140
requires E e noexcept(noexcept(s.error(std::move(e))))
virtual ~IOBufPrinter()
Definition: Logging.h:145
std::unique_ptr< StringStreamPair > traceAndLogStreamPair_
Definition: Logging.h:73
StackTracePrinterWithException(const bool checkPassed, const char *checkString, const char *file, const int line, const int logLevel)
Definition: Logging.h:37
NullStream & operator=(NullStream &&)
Definition: Logging.h:27
static std::string printChainInfo(const folly::IOBuf *buf)
Definition: Logging.h:136
const char * string
Definition: Conv.cpp:212
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
std::pair< std::string, std::ostringstream > StringStreamPair
Definition: Logging.h:35