19 #ifndef ASYLO_UTIL_LOGGING_H_ 20 #define ASYLO_UTIL_LOGGING_H_ 28 #include "absl/base/attributes.h" 29 #include "absl/base/optimization.h" 32 #define COMPACT_ASYLO_LOG_INFO ::asylo::LogMessage(__FILE__, __LINE__) 33 #define COMPACT_ASYLO_LOG_WARNING 34 ::asylo::LogMessage(__FILE__, __LINE__, WARNING) 35 #define COMPACT_ASYLO_LOG_ERROR ::asylo::LogMessage(__FILE__, __LINE__, ERROR) 36 #define COMPACT_ASYLO_LOG_FATAL 37 ::asylo::LogMessageFatal(__FILE__, __LINE__, FATAL) 38 #define COMPACT_ASYLO_LOG_QFATAL 39 ::asylo::LogMessageFatal(__FILE__, __LINE__, QFATAL) 42 #define COMPACT_ASYLO_LOG_DFATAL COMPACT_ASYLO_LOG_ERROR 44 #define COMPACT_ASYLO_LOG_DFATAL COMPACT_ASYLO_LOG_FATAL
60 #define LOG(severity) COMPACT_ASYLO_LOG_##severity.stream() 73 #define LOG_IF(severity, condition) 74 !(condition) ? (void)0
: asylo::LogMessageVoidify() & LOG(severity) 88 #define VLOG(level) LOG_IF(INFO, (level) <= get_vlog_level()) 102 #define CHECK(condition) 103 LOG_IF(FATAL, !(condition)) << "Check failed: " #condition " " 107 enum LogSeverity { INFO, WARNING, ERROR, FATAL, QFATAL };
114 template <
typename T>
115 inline void MakeCheckOpValueString(std::ostream *os,
const T &v) {
122 void MakeCheckOpValueString(std::ostream *os,
const char &v);
124 void MakeCheckOpValueString(std::ostream *os,
const signed char &v);
126 void MakeCheckOpValueString(std::ostream *os,
const unsigned char &v);
163 std::ostringstream *stream_;
167 template <
typename T1,
typename T2>
168 std::string *MakeCheckOpString(
const T1 &v1,
const T2 &v2,
169 const char *exprtext) {
184 #define DEFINE_CHECK_OP_IMPL(name, op) 185 template <typename T1, typename T2> 186 inline std::string *name##Impl(const T1 &v1, const T2 &v2, 187 const char *exprtext) { 188 if (ABSL_PREDICT_TRUE(v1 op v2)) return nullptr; 189 return MakeCheckOpString(v1, v2, exprtext); 191 inline std::string *name##Impl(int v1, int v2, const char *exprtext) { 192 return name##Impl<int, int>(v1, v2, exprtext); 200 DEFINE_CHECK_OP_IMPL(Check_EQ, ==)
201 DEFINE_CHECK_OP_IMPL(Check_NE, !=)
202 DEFINE_CHECK_OP_IMPL(Check_LE, <=)
203 DEFINE_CHECK_OP_IMPL(Check_LT, <)
204 DEFINE_CHECK_OP_IMPL(Check_GE, >=)
205 DEFINE_CHECK_OP_IMPL(Check_GT, >)
206 #undef DEFINE_CHECK_OP_IMPL 213 template <
typename T>
214 inline const T &GetReferenceableValue(
const T &t) {
217 inline char GetReferenceableValue(
char t) {
return t; }
218 inline uint8_t GetReferenceableValue(uint8_t t) {
return t; }
219 inline int8_t GetReferenceableValue(int8_t t) {
return t; }
220 inline int16_t GetReferenceableValue(int16_t t) {
return t; }
221 inline uint16_t GetReferenceableValue(uint16_t t) {
return t; }
222 inline int32_t GetReferenceableValue(int32_t t) {
return t; }
223 inline uint32_t GetReferenceableValue(uint32_t t) {
return t; }
224 inline int64_t GetReferenceableValue(int64_t t) {
return t; }
225 inline uint64_t GetReferenceableValue(uint64_t t) {
return t; }
236 #define CHECK_OP_LOG(name, op, val1, val2, log) 237 while (std::unique_ptr<std::string> _result = std::unique_ptr<std::string>( 238 asylo::name##Impl(asylo::GetReferenceableValue(val1), 239 asylo::GetReferenceableValue(val2), 240 #val1 " " #op " " #val2))) 241 log(__FILE__, __LINE__, *_result).stream() 250 #define CHECK_OP(name, op, val1, val2) 251 CHECK_OP_LOG(name, op, val1, val2, asylo::LogMessageFatal) 254 #define CHECK_EQ(val1, val2) CHECK_OP(Check_EQ, ==, val1, val2) 256 #define CHECK_NE(val1, val2) CHECK_OP(Check_NE, !=, val1, val2) 258 #define CHECK_LE(val1, val2) CHECK_OP(Check_LE, <=, val1, val2) 260 #define CHECK_LT(val1, val2) CHECK_OP(Check_LT, <, val1, val2) 262 #define CHECK_GE(val1, val2) CHECK_OP(Check_GE, >=, val1, val2) 264 #define CHECK_GT(val1, val2) CHECK_OP(Check_GT, >, val1, val2) 281 #define CHECK_NOTNULL(val) 282 asylo::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val)) 327 bool InitLogging(
const char *directory,
const char *file_name,
int level);
343 LogMessage(
const char *file,
int line, LogSeverity severity);
351 LogMessage(
const char *file,
int line,
const std::string &result);
363 void SendToLog(
const std::string &message_text);
372 void Init(
const char *file,
int line, LogSeverity severity);
427 template <
typename T>
428 T
CheckNotNull(
const char *file,
int line,
const char *exprtext, T &&t) {
429 if (ABSL_PREDICT_FALSE(!t)) {
430 LogMessage(file, line, std::string(exprtext));
432 return std::forward<T>(t);
std::string * NewString()
Gets the built string contents.
bool InitLogging(const char *directory, const char *file_name, int level)
Initializes minimal logging library.
const std::string get_log_directory()
Gets the log directory that was specified when this enclave is initialized.
int get_vlog_level()
Gets the verbosity threshold for VLOG.
std::ostringstream stream_
Definition: logging.h:369
LogMessage(const char *file, int line)
Constructs a new message with INFO severity.
LogMessageFatal(const char *file, int line, LogSeverity severity)
Constructs a new message with FATAL severity.
Definition: logging.h:399
Class representing a log message created by a log macro.
Definition: logging.h:330
std::ostream * ForVar1()
Gets the output stream for the first argument of the message.
Definition: logging.h:149
#define CHECK_OP(name, op, val1, val2)
Compares val1 and val2 with op, and produces a LOG(FATAL) if false.
Definition: logging.h:250
LogMessageFatal(const char *file, int line, const std::string &result)
Constructs a message with FATAL severity for use by CHECK macros.
Definition: logging.h:407
void set_vlog_level(int level)
Sets the verbosity threshold for VLOG.
CheckOpMessageBuilder(const char *exprtext)
Constructs an object to format a CheckOp message.
~CheckOpMessageBuilder()
Deletes "stream_".
#define CHECK_OP_LOG(name, op, val1, val2, log)
Compares val1 and val2 with op, and does log if false.
Definition: logging.h:236
void SendToLog(const std::string &message_text)
LogMessage(const char *file, int line, const std::string &result)
Constructs a log message with additional text that is provided by CHECK macros.
bool EnsureDirectory(const char *path)
Checks the log directory to make sure it's accessible, and creates it if it does not exist...
std::ostream * ForVar2()
Gets the output stream for writing the argument of the message.
LogSeverity severity_
Definition: logging.h:365
void MakeCheckOpValueString(std::ostream *os, const std::nullptr_t &p)
A LogSeverity FATAL (or QFATAL) version of LogMessage that the compiler can interpret as noreturn...
Definition: logging.h:389
T CheckNotNull(const char *file, int line, const char *exprtext, T &&t)
Logs a message if the given value of type T is null, and then forwards the value. ...
Definition: logging.h:428
bool set_log_directory(const std::string &log_directory)
Sets the log directory, as specified when this enclave is initialized.
ABSL_ATTRIBUTE_NORETURN ~LogMessageFatal()
The destructor flushes the message and does not return.
#define LOG(severity)
Creates a message and logs it to file.
Definition: logging.h:60
~LogMessage()
The destructor flushes the message.
std::ostringstream & stream()
Gets a reference to the underlying string stream.
Definition: logging.h:359
void operator &(const std::ostream &)
Definition: logging.h:384
A helper class for formatting "expr (V1 vs. V2)" in a CHECK_XX statement.
Definition: logging.h:135
LogMessage(const char *file, int line, LogSeverity severity)
Constructs a new message with the specified severity.
#define LOG_IF(severity, condition)
A command to LOG only if a condition is true.
Definition: logging.h:73
This class is used just to take an ostream type and make it a void type to satisify the ternary opera...
Definition: logging.h:382