proxygen
Logging.cpp
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  */
11 
12 #include <folly/Format.h>
13 #include <folly/Memory.h>
14 #include <folly/Singleton.h>
15 #include <folly/String.h>
16 #include <fstream>
17 #include <memory>
18 #include <ostream>
19 #include <sstream>
20 #include <vector>
21 #include <sys/stat.h>
22 
23 using folly::IOBuf;
24 using folly::StringPiece;
25 using std::ostream;
26 using std::string;
27 using std::stringstream;
28 using std::unique_ptr;
29 using std::vector;
30 
31 namespace {
32 proxygen::HexFollyPrinter hexFollyPrinter;
33 proxygen::Hex16Printer hex16Printer;
34 proxygen::ChainInfoPrinter chainInfoPrinter;
35 proxygen::BinPrinter binPrinter;
36 
37 vector<proxygen::IOBufPrinter*> printers = {
38  &hexFollyPrinter,
39  &hex16Printer,
40  &chainInfoPrinter,
41  &binPrinter
42 };
43 }
44 
45 namespace proxygen {
46 
47 string hexStr(StringPiece sp) {
48  string out;
49  for (auto ch : sp) {
50  out.append(folly::sformat("{:02x}", (uint8_t) ch));
51  }
52  return out;
53 }
54 
55 string HexFollyPrinter::print(const IOBuf* buf) {
56  return folly::hexDump(buf->data(), buf->length());
57 }
58 
59 string Hex16Printer::print(const IOBuf* buf) {
60  stringstream out;
61  const uint8_t* data = buf->data();
62  char tmp[24];
63  for (size_t i = 0; i < buf->length(); i++) {
64  snprintf(tmp, 3, "%02x", data[i]);
65  out << tmp;
66  if ((i + 1) % 2 == 0) {
67  out << ' ';
68  }
69  if ((i + 1) % 16 == 0) {
70  out << std::endl;
71  }
72  }
73  return out.str();
74 }
75 
76 string ChainInfoPrinter::print(const IOBuf* buf) {
77  stringstream out;
78  out << "iobuf of size " << buf->length()
79  << " tailroom " << buf->tailroom();
80  return out.str();
81 }
82 
83 string BinPrinter::print(const IOBuf* buf) {
84  static uint8_t bytesPerLine = 8;
85  string out;
86  const uint8_t* data = buf->data();
87  for (size_t i = 0; i < buf->length(); i++) {
88  for (int b = 7; b >= 0; b--) {
89  out += data[i] & 1 << b ? '1' : '0';
90  }
91  out += ' ';
92  out += isprint(data[i]) ? data[i] : ' ';
93  if ((i + 1) % bytesPerLine == 0) {
94  out += '\n';
95  } else {
96  out += ' ';
97  }
98  }
99  out += '\n';
100  return out;
101 }
102 
103 string IOBufPrinter::printChain(const IOBuf* buf,
104  Format format,
105  bool coalesce) {
106  uint8_t index = (uint8_t) format;
107  if (printers.size() <= index) {
108  LOG(ERROR) << "invalid format: " << index;
109  return "";
110  }
111  auto printer = printers[index];
112  // empty chain
113  if (!buf) {
114  return "";
115  }
116 
117  unique_ptr<IOBuf> cbuf = nullptr;
118  if (coalesce) {
119  cbuf = buf->clone();
120  cbuf->coalesce();
121  buf = cbuf.get();
122  }
123  auto b = buf;
124  string res;
125  do {
126  res += printer->print(b);
127  b = b->next();
128  } while (b != buf);
129  return res;
130 }
131 
132 void dumpBinToFile(const string& filename, const IOBuf* buf) {
133  struct stat fstat;
134  bool exists = (stat(filename.c_str(), &fstat) == 0);
135  if (exists) {
136  // don't write anything if the file exists
137  return;
138  }
139  std::ofstream file(filename, std::ofstream::binary);
140  if (!file.is_open()) {
141  LOG(ERROR) << "cannot open file " << filename;
142  return;
143  }
144  if (!buf) {
145  file.close();
146  return;
147  }
148  const IOBuf* first = buf;
149  do {
150  file.write((const char *)buf->data(), buf->length());
151  buf = buf->next();
152  } while (buf != first);
153  file.close();
154  LOG(INFO) << "wrote chain " << IOBufPrinter::printChainInfo(buf)
155  << " to " << filename;
156 }
157 
158 namespace logging_details {
160  return folly::SingletonVault::stackTraceGetter().load()();
161 }
162 }
163 
164 }
void dumpBinToFile(const string &filename, const IOBuf *buf)
Definition: Logging.cpp:132
char b
string hexStr(StringPiece sp)
Definition: Logging.cpp:47
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
ByteRange coalesce()
Definition: IOBuf.h:1095
std::string getStackTrace()
Definition: Logging.cpp:159
const uint8_t * data() const
Definition: IOBuf.h:499
std::unique_ptr< IOBuf > clone() const
Definition: IOBuf.cpp:527
std::size_t tailroom() const
Definition: IOBuf.h:551
static std::string printChain(const folly::IOBuf *buf, Format format, bool coalesce)
Definition: Logging.cpp:103
auto ch
static std::atomic< StackTraceGetterPtr > & stackTraceGetter()
Definition: Singleton.h:510
std::string print(const folly::IOBuf *buf) override
Definition: Logging.cpp:59
std::size_t length() const
Definition: IOBuf.h:533
void hexDump(const void *ptr, size_t size, OutIt out)
Definition: String-inl.h:645
IOBuf * next()
Definition: IOBuf.h:600
static std::string printChainInfo(const folly::IOBuf *buf)
Definition: Logging.h:136
const char * string
Definition: Conv.cpp:212
std::string print(const folly::IOBuf *buf) override
Definition: Logging.cpp:55
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
Range< const char * > StringPiece
std::string print(const folly::IOBuf *buf) override
Definition: Logging.cpp:83
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
constexpr detail::First first
Definition: Base-inl.h:2553
std::string print(const folly::IOBuf *buf) override
Definition: Logging.cpp:76