taco-db  0.1.0
logging.h
Go to the documentation of this file.
1 
10 #include "utils/misc.h"
11 
12 #include <iostream>
13 #include <absl/base/log_severity.h>
14 #include <absl/strings/str_format.h>
15 
16 namespace taco {
17 
18 using absl::LogSeverity;
19 constexpr LogSeverity kInfo = LogSeverity::kInfo;
20 constexpr LogSeverity kWarning = LogSeverity::kWarning;
21 constexpr LogSeverity kError = LogSeverity::kError;
22 constexpr LogSeverity kFatal = LogSeverity::kFatal;
23 
24 constexpr const size_t log_msg_buf_size = 8192;
25 
26 // "[log_severity] " is at most of length 10, and there're '\n' and '\0' at the
27 // end.
28 constexpr const size_t log_msg_max_len = log_msg_buf_size - 12;
29 
30 class TDBError {
31 public:
32  TDBError(LogSeverity level, std::string &&str):
33  m_level(level),
34  m_msg(std::move(str)) {}
35 
36  constexpr LogSeverity
37  GetSeverity() const {
38  return m_level;
39  }
40 
41  const std::string&
42  GetMessage() const {
43  return m_msg;
44  }
45 
46 // If in gtest, define an implicit conversion from TDBError to
47 // absl::string_view so that we can match it against string matchers.
48 #ifdef TDB_IN_TEST
49  operator std::string() const {
50  return m_msg;
51  }
52 #endif
53 
54 private:
55  LogSeverity m_level;
56  std::string m_msg;
57 };
58 
64 void LogError(LogSeverity severity, std::string &&msg);
65 
69 void SetLogOutput(std::ostream *log_out);
70 
74 void RestoreLogOutput();
75 
79 void SetLogPrintMinSeverity(LogSeverity min_severity);
80 
86 void DisableLogPrint();
87 
92 void SetSecondaryLogOutput(std::ostream *log_out, LogSeverity min_severity);
93 
98 
105 #ifdef TDB_IN_TEST
106 #define LOG(level, ...) \
107  do { \
108  ::taco::LogError(level, absl::StrFormat("[%s] %s:%d: " \
109  CAR(__VA_ARGS__), \
110  absl::LogSeverityName(level),\
111  ::taco::StripSourcePath(__FILE__), __LINE__\
112  IF_NONEMPTY_COMMA(CADR(__VA_ARGS__), ) CDR(__VA_ARGS__))); \
113  } while (0)
114 #else
115 // don't log the file location when not in test
116 #define LOG(level, ...) \
117  do { \
118  ::taco::LogError(level, absl::StrFormat("[%s] " \
119  CAR(__VA_ARGS__), \
120  absl::LogSeverityName(level) \
121  IF_NONEMPTY_COMMA(CADR(__VA_ARGS__), ) CDR(__VA_ARGS__))); \
122  } while (0)
123 #endif // TDB_IN_TEST
124 
125 
126 
127 } // namespace taco
128 
Definition: logging.h:30
std::string m_msg
Definition: logging.h:56
TDBError(LogSeverity level, std::string &&str)
Definition: logging.h:32
LogSeverity m_level
Definition: logging.h:55
constexpr LogSeverity GetSeverity() const
Definition: logging.h:37
const std::string & GetMessage() const
Definition: logging.h:42
Definition: Record.h:148
Definition: datum.h:28
constexpr LogSeverity kFatal
Definition: logging.h:22
void SetLogOutput(std::ostream *log_out)
Sets the output stream where the log messages should be printed to.
Definition: logging.cpp:47
constexpr LogSeverity kError
Definition: logging.h:21
void DisableLogPrint()
Disable all log message printing.
Definition: logging.cpp:62
void RestoreLogOutput()
Restores the log output stream to std::cerr.
Definition: logging.cpp:52
constexpr LogSeverity kInfo
Definition: logging.h:19
void ClearSecondaryLogOutput()
Clears the secondary output stream where logs messages additionally prints.
Definition: logging.cpp:73
constexpr const size_t log_msg_max_len
Definition: logging.h:28
constexpr const size_t log_msg_buf_size
Definition: logging.h:24
void SetLogPrintMinSeverity(LogSeverity min_severity)
Sets the minimum severity level of a log message to be printed.
Definition: logging.cpp:57
void SetSecondaryLogOutput(std::ostream *log_out, LogSeverity min_severity)
Sets the secondary output stream where logs messages additionally prints with at least the specified ...
Definition: logging.cpp:67
void LogError(LogSeverity level, std::string &&str)
Logs a message (which may not be an error despite what the name suggests).
Definition: logging.cpp:29
constexpr LogSeverity kWarning
Definition: logging.h:20