Google Cloud C++ Client  1.32.1
C++ Client Library for Google Cloud Platform
Public Types | Public Member Functions | Static Public Member Functions | List of all members
google::cloud::v1::LogSink Class Reference

A sink to receive log records. More...

#include <google/cloud/log.h>

Public Types

using BackendId = long
 

Public Member Functions

 LogSink ()
 
bool empty () const
 Return true if this object has no backends. More...
 
bool is_enabled (Severity severity) const
 Return true if severity is enabled. More...
 
void set_minimum_severity (Severity minimum)
 
Severity minimum_severity () const
 
BackendId AddBackend (std::shared_ptr< LogBackend > backend)
 
void RemoveBackend (BackendId id)
 
void ClearBackends ()
 
std::size_t BackendCount () const
 
void Log (LogRecord log_record)
 
void Flush ()
 Flush all the current backends. More...
 

Static Public Member Functions

static constexpr bool CompileTimeEnabled (Severity level)
 Return true if the severity is enabled at compile time. More...
 
static LogSinkInstance ()
 Return the singleton instance for this application. More...
 
static void EnableStdClog (Severity min_severity=Severity::GCP_LS_LOWEST_ENABLED)
 Enable std::clog on LogSink::Instance(). More...
 
static void DisableStdClog ()
 Disable std::clog on LogSink::Instance(). More...
 

Detailed Description

A sink to receive log records.

Definition at line 247 of file log.h.

Member Typedef Documentation

◆ BackendId

Definition at line 312 of file log.h.

Constructor & Destructor Documentation

◆ LogSink()

google::cloud::v1::LogSink::LogSink ( )

Definition at line 89 of file log.cc.

Member Function Documentation

◆ AddBackend()

long google::cloud::v1::LogSink::AddBackend ( std::shared_ptr< LogBackend backend)

Definition at line 103 of file log.cc.

◆ BackendCount()

std::size_t google::cloud::v1::LogSink::BackendCount ( ) const

Definition at line 121 of file log.cc.

◆ ClearBackends()

void google::cloud::v1::LogSink::ClearBackends ( )

Definition at line 114 of file log.cc.

◆ CompileTimeEnabled()

static constexpr bool google::cloud::v1::LogSink::CompileTimeEnabled ( Severity  level)
inlinestaticconstexpr

Return true if the severity is enabled at compile time.

Definition at line 252 of file log.h.

◆ DisableStdClog()

static void google::cloud::v1::LogSink::DisableStdClog ( )
inlinestatic

Disable std::clog on LogSink::Instance().

Note that this will remove the default logging backend.

Definition at line 340 of file log.h.

◆ empty()

bool google::cloud::v1::LogSink::empty ( ) const
inline

Return true if this object has no backends.

We want to avoid synchronization overhead when checking if a log message is enabled. Most of the time, most messages will be disabled, so incurring the locking overhead on each message would be too expensive and would discourage developers from creating logs. Furthermore, missing a few messages while the change of state "propagates" to other threads does not affect the correctness of the program.

Note that memory_order_relaxed does not provide a compiler barrier either, so in theory stores into the atomic could be reordered by the optimizer. We have no reason to worry about that because all the writes are done inside a critical section protected by a mutex. The compiler cannot (or should not) reorder operations around those.

Definition at line 277 of file log.h.

◆ EnableStdClog()

static void google::cloud::v1::LogSink::EnableStdClog ( Severity  min_severity = Severity::GCP_LS_LOWEST_ENABLED)
inlinestatic

Enable std::clog on LogSink::Instance().

This is also enabled if the "GOOGLE_CLOUD_CPP_ENABLE_CLOG" environment variable is set.

Definition at line 330 of file log.h.

◆ Flush()

void google::cloud::v1::LogSink::Flush ( )

Flush all the current backends.

Definition at line 142 of file log.cc.

◆ Instance()

LogSink & google::cloud::v1::LogSink::Instance ( )
static

Return the singleton instance for this application.

Definition at line 93 of file log.cc.

◆ is_enabled()

bool google::cloud::v1::LogSink::is_enabled ( Severity  severity) const
inline

Return true if severity is enabled.

We want to avoid synchronization overhead when checking if a log message is enabled. Most of the time, most messages will be disabled, so incurring the locking overhead on each message would be too expensive and would discourage developers from creating logs. Furthermore, missing a few messages while the change of state "propagates" to other threads does not affect the correctness of the program.

Note that memory_order_relaxed does not provide a compiler barrier either, so in theory stores into the atomic could be reordered by the optimizer. We have no reason to worry about that because all the writes are done inside a critical section protected by a mutex. The compiler cannot (or should not) reorder operations around those.

Definition at line 295 of file log.h.

◆ Log()

void google::cloud::v1::LogSink::Log ( LogRecord  log_record)

Definition at line 126 of file log.cc.

◆ minimum_severity()

Severity google::cloud::v1::LogSink::minimum_severity ( ) const
inline

Definition at line 303 of file log.h.

◆ RemoveBackend()

void google::cloud::v1::LogSink::RemoveBackend ( BackendId  id)

Definition at line 109 of file log.cc.

◆ set_minimum_severity()

void google::cloud::v1::LogSink::set_minimum_severity ( Severity  minimum)
inline

Definition at line 300 of file log.h.