proxygen
LogMessage.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <sys/types.h>
19 #include <chrono>
20 #include <string>
21 
22 #include <folly/Range.h>
23 #include <folly/logging/LogLevel.h>
24 
25 namespace folly {
26 
27 class LogCategory;
28 
38 class LogMessage {
39  public:
40  LogMessage(
41  const LogCategory* category,
42  LogLevel level,
43  folly::StringPiece filename,
44  unsigned int lineNumber,
45  folly::StringPiece functionName,
46  std::string&& msg);
48  const LogCategory* category,
49  LogLevel level,
50  folly::StringPiece filename,
51  unsigned int lineNumber,
52  folly::StringPiece functionName,
54  : LogMessage(
55  category,
56  level,
57  filename,
58  lineNumber,
59  functionName,
60  msg.str()) {}
61 
67  LogMessage(
68  const LogCategory* category,
69  LogLevel level,
70  std::chrono::system_clock::time_point timestamp,
71  folly::StringPiece filename,
72  unsigned int lineNumber,
73  folly::StringPiece functionName,
74  std::string&& msg);
75 
76  const LogCategory* getCategory() const {
77  return category_;
78  }
79 
80  LogLevel getLevel() const {
81  return level_;
82  }
83 
85  return filename_;
86  }
88 
89  unsigned int getLineNumber() const {
90  return lineNumber_;
91  }
92 
94  return functionName_;
95  }
96 
97  std::chrono::system_clock::time_point getTimestamp() const {
98  return timestamp_;
99  }
100 
102  return threadID_;
103  }
104 
105  const std::string& getMessage() const {
106  // If no characters needed to be sanitized, message_ will be empty.
107  if (message_.empty()) {
108  return rawMessage_;
109  }
110  return message_;
111  }
112 
113  const std::string& getRawMessage() const {
114  return rawMessage_;
115  }
116 
117  bool containsNewlines() const {
118  return containsNewlines_;
119  }
120 
121  private:
122  void sanitizeMessage();
123 
124  const LogCategory* const category_{nullptr};
125  LogLevel const level_{static_cast<LogLevel>(0)};
126  uint64_t const threadID_{0};
127  std::chrono::system_clock::time_point const timestamp_;
128 
133 
137  unsigned int const lineNumber_{0};
138 
143 
150  bool containsNewlines_{false};
151 
159 
169 };
170 } // namespace folly
const std::string & getRawMessage() const
Definition: LogMessage.h:113
LogMessage(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, folly::StringPiece msg)
Definition: LogMessage.h:47
folly::StringPiece getFunctionName() const
Definition: LogMessage.h:93
LogLevel const level_
Definition: LogMessage.h:125
unsigned int const lineNumber_
Definition: LogMessage.h:137
std::string const rawMessage_
Definition: LogMessage.h:158
folly::StringPiece const filename_
Definition: LogMessage.h:132
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
const LogCategory *const category_
Definition: LogMessage.h:124
const std::string & getMessage() const
Definition: LogMessage.h:105
void sanitizeMessage()
Definition: LogMessage.cpp:83
uint64_t getThreadID() const
Definition: LogMessage.h:101
LogLevel getLevel() const
Definition: LogMessage.h:80
folly::StringPiece getFileName() const
Definition: LogMessage.h:84
std::chrono::system_clock::time_point const timestamp_
Definition: LogMessage.h:127
std::string message_
Definition: LogMessage.h:168
LogLevel
Definition: LogLevel.h:38
unsigned int getLineNumber() const
Definition: LogMessage.h:89
const char * string
Definition: Conv.cpp:212
const LogCategory * getCategory() const
Definition: LogMessage.h:76
LogMessage(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, std::string &&msg)
Definition: LogMessage.cpp:24
folly::StringPiece getFileBaseName() const
Definition: LogMessage.cpp:61
std::chrono::system_clock::time_point getTimestamp() const
Definition: LogMessage.h:97
folly::StringPiece const functionName_
Definition: LogMessage.h:142
uint64_t const threadID_
Definition: LogMessage.h:126
bool containsNewlines() const
Definition: LogMessage.h:117