Asylo
Classes | Namespaces | Macros | Enumerations | Functions
logging.h File Reference
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <sstream>
#include <string>
#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
Include dependency graph for logging.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  asylo::CheckOpMessageBuilder
 A helper class for formatting "expr (V1 vs. V2)" in a CHECK_XX statement. More...
 
class  asylo::LogMessage
 Class representing a log message created by a log macro. More...
 
class  asylo::LogMessageVoidify
 This class is used just to take an ostream type and make it a void type to satisify the ternary operator in LOG_IF. More...
 
class  asylo::LogMessageFatal
 A LogSeverity FATAL (or QFATAL) version of LogMessage that the compiler can interpret as noreturn. More...
 

Namespaces

 asylo
 

Macros

#define LOG(severity)   COMPACT_ASYLO_LOG_##severity.stream()
 Creates a message and logs it to file. More...
 
#define LOG_IF(severity, condition)   !(condition) ? (void)0 : asylo::LogMessageVoidify() & LOG(severity)
 A command to LOG only if a condition is true. More...
 
#define VLOG(level)   LOG_IF(INFO, (level) <= get_vlog_level())
 A LOG command with an associated verbosity level. More...
 
#define CHECK(condition)   LOG_IF(FATAL, !(condition)) << "Check failed: " #condition " "
 Ends the program with a fatal error if the specified condition is false. More...
 
#define CHECK_OP_LOG(name, op, val1, val2, log)
 Compares val1 and val2 with op, and does log if false. More...
 
#define CHECK_OP(name, op, val1, val2)   CHECK_OP_LOG(name, op, val1, val2, asylo::LogMessageFatal)
 Compares val1 and val2 with op, and produces a LOG(FATAL) if false. More...
 
#define CHECK_EQ(val1, val2)   CHECK_OP(Check_EQ, ==, val1, val2)
 Produces a LOG(FATAL) unless val1 equals val2. More...
 
#define CHECK_NE(val1, val2)   CHECK_OP(Check_NE, !=, val1, val2)
 Produces a LOG(FATAL) unless val1 does not equal to val2. More...
 
#define CHECK_LE(val1, val2)   CHECK_OP(Check_LE, <=, val1, val2)
 Produces a LOG(FATAL) unless val1 is less than or equal to val2. More...
 
#define CHECK_LT(val1, val2)   CHECK_OP(Check_LT, <, val1, val2)
 Produces a LOG(FATAL) unless val1 is less than val2. More...
 
#define CHECK_GE(val1, val2)   CHECK_OP(Check_GE, >=, val1, val2)
 Produces a LOG(FATAL) unless val1 is greater than or equal to val2. More...
 
#define CHECK_GT(val1, val2)   CHECK_OP(Check_GT, >, val1, val2)
 Produces a LOG(FATAL) unless val1 is greater than val2. More...
 
#define CHECK_NOTNULL(val)   asylo::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
 Checks that the argument is not null, and returns it. More...
 

Enumerations

enum  LogSeverity {
  INFO, WARNING, ERROR, FATAL,
  QFATAL
}
 Severity level definitions. More...
 

Functions

template<>
void asylo::MakeCheckOpValueString (std::ostream *os, const std::nullptr_t &p)
 
void asylo::set_vlog_level (int level)
 Sets the verbosity threshold for VLOG. More...
 
int asylo::get_vlog_level ()
 Gets the verbosity threshold for VLOG. More...
 
bool asylo::set_log_directory (const std::string &log_directory)
 Sets the log directory, as specified when this enclave is initialized. More...
 
const std::string asylo::get_log_directory ()
 Gets the log directory that was specified when this enclave is initialized. More...
 
bool asylo::EnsureDirectory (const char *path)
 Checks the log directory to make sure it's accessible, and creates it if it does not exist. More...
 
bool asylo::InitLogging (const char *directory, const char *file_name, int level)
 Initializes minimal logging library. More...
 
template<typename T >
asylo::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. More...
 

Macro Definition Documentation

◆ CHECK

#define CHECK (   condition)    LOG_IF(FATAL, !(condition)) << "Check failed: " #condition " "

Ends the program with a fatal error if the specified condition is false.

Example:

CHECK(!cheese.empty()) << "Out of Cheese";

Might produce a message like:

Check_failed: !cheese.empty() Out of Cheese

◆ CHECK_EQ

#define CHECK_EQ (   val1,
  val2 
)    CHECK_OP(Check_EQ, ==, val1, val2)

Produces a LOG(FATAL) unless val1 equals val2.

◆ CHECK_GE

#define CHECK_GE (   val1,
  val2 
)    CHECK_OP(Check_GE, >=, val1, val2)

Produces a LOG(FATAL) unless val1 is greater than or equal to val2.

◆ CHECK_GT

#define CHECK_GT (   val1,
  val2 
)    CHECK_OP(Check_GT, >, val1, val2)

Produces a LOG(FATAL) unless val1 is greater than val2.

◆ CHECK_LE

#define CHECK_LE (   val1,
  val2 
)    CHECK_OP(Check_LE, <=, val1, val2)

Produces a LOG(FATAL) unless val1 is less than or equal to val2.

◆ CHECK_LT

#define CHECK_LT (   val1,
  val2 
)    CHECK_OP(Check_LT, <, val1, val2)

Produces a LOG(FATAL) unless val1 is less than val2.

◆ CHECK_NE

#define CHECK_NE (   val1,
  val2 
)    CHECK_OP(Check_NE, !=, val1, val2)

Produces a LOG(FATAL) unless val1 does not equal to val2.

◆ CHECK_NOTNULL

#define CHECK_NOTNULL (   val)    asylo::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))

Checks that the argument is not null, and returns it.

Unlike other CHECK macros, this one returns its input, so it can be used in initializers. Outside initializers, prefer CHECK.

CHECK_NOTNULL works for both raw pointers and (compatible) smart pointers including std::unique_ptr and std::shared_ptr.

For smart pointers CHECK_NOTNULL returns a reference to its argument, preserving the value category (i.e., an rvalue reference for an rvalue argument, and an lvalue reference otherwise). For pre-C++11 compilers that's not possible, so as a best available approximation a reference-to-const will be returned if the argument is an rvalue.

Parameters
valThe value being compared.

◆ CHECK_OP

#define CHECK_OP (   name,
  op,
  val1,
  val2 
)    CHECK_OP_LOG(name, op, val1, val2, asylo::LogMessageFatal)

Compares val1 and val2 with op, and produces a LOG(FATAL) if false.

Parameters
nameAn identifier that is the name of the comparison, such as Check_EQ or Check_NE.
opThe comparison operator, such as == or !=.
val1The first variable to be compared.
val2The second variable to be compared.

◆ CHECK_OP_LOG

#define CHECK_OP_LOG (   name,
  op,
  val1,
  val2,
  log 
)
Value:
while (std::unique_ptr<std::string> _result = std::unique_ptr<std::string>( \
asylo::name##Impl(asylo::GetReferenceableValue(val1), \
asylo::GetReferenceableValue(val2), \
#val1 " " #op " " #val2))) \
log(__FILE__, __LINE__, *_result).stream()

Compares val1 and val2 with op, and does log if false.

Parameters
nameAn identifier that is the name of the comparison, such as Check_EQ or Check_NE.
opThe comparison operator, such as == or !=.
val1The first variable to be compared.
val2The second variable to be compared.
logThe log action to be performed if the comparison returns false.

◆ LOG

#define LOG (   severity)    COMPACT_ASYLO_LOG_##severity.stream()

Creates a message and logs it to file.

LOG(severity) returns a stream object that can be written to with the << operator. Log messages are emitted with terminating newlines. Example:

LOG(INFO) << "Found" << num_cookies << " cookies";
Parameters
severityThe severity of the log message, one of LogSeverity. The FATAL severity will end the program after the log is emitted.

◆ LOG_IF

#define LOG_IF (   severity,
  condition 
)    !(condition) ? (void)0 : asylo::LogMessageVoidify() & LOG(severity)

A command to LOG only if a condition is true.

If the condition is false, nothing is logged. Example:

LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
Parameters
severityThe severity of the log message, one of LogSeverity. The FATAL severity will end the program after the log is emitted.
conditionThe condition that determines whether to log the message.

◆ VLOG

#define VLOG (   level)    LOG_IF(INFO, (level) <= get_vlog_level())

A LOG command with an associated verbosity level.

The verbosity threshold may be configured at runtime with set_vlog_level and InitLogging.

VLOG statements are logged at INFO severity if they are logged at all. The numeric levels are on a different scale than the severity levels. Example:

VLOG(1) << "Print when VLOG level is set to be 1 or higher";
Parameters
levelThe numeric level that determines whether to log the message.

Enumeration Type Documentation

◆ LogSeverity

Severity level definitions.

These represent the four severity levels INFO through FATAL.

Enumerator
INFO 
WARNING 
ERROR 
FATAL 
QFATAL