Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logging.h File Reference

Copyright 2016 Google Inc. More...

#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include "ion/port/logging.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  ion::base::logging_internal::Logger
 This class is used for regular logging. More...
 
class  ion::base::logging_internal::NullLogger
 This class is used to disable logging, while still allowing for log messages to contain '<<' expressions. More...
 
class  ion::base::logging_internal::SingleLogger
 This class prints a message only the first time it is created for the passed file_name and line_number. More...
 
class  ion::base::logging_internal::ThrottledLogger
 This class prints a message only if the passed file_name and line_number has not printed a message in a certain amount of time. More...
 

Namespaces

 ion
 Copyright 2016 Google Inc.
 
 ion::base
 EnumHelper instantiations. These must be in the ion::base namespace.
 
 ion::base::logging_internal
 Internal logging implementation.
 

Macros

Internal LOG macro implementation

These macros implement Ion's LOG statement.

Use the non-prefixed LOG macros in your code instead.

#define ION_LOG(severity)
 Completely disable logging in production builds. More...
 
#define ION_IGNORE_LOG(severity)   ::ion::base::logging_internal::NullLogger().GetStream()
 Macro that ignores logging. More...
 
#define ION_LOG_ONCE(severity)
 Macro that logs a message only once. More...
 
#define ION_LOG_EVERY_N_SEC(severity, time)
 Macro that logs a message at most every N seconds. More...
 
Main logging macros

These macros log a message to the console in a platform-specific way.

The severity argument determines the severity of the logging message. DFATAL and FATAL severities will also trigger a breakpoint and abort, either in debug mode (DFATAL) or unconditionally (FATAL).

All versions of these macros use streaming syntax, like std::cerr. That is, you invoke them like:

LOG(WARNING) << "Something mildly concerning happened.";
LOG(FATAL) << "The " << device << " is literally on fire.";
#define LOG(severity)   ION_LOG(severity)
 Logs the streamed message unconditionally with a severity of severity. More...
 
#define LOG_ONCE(severity)   ION_LOG_ONCE(severity)
 Logs the streamed message once per process run with a severity of severity. More...
 
#define LOG_EVERY_N_SEC(severity, time)   ION_LOG_EVERY_N_SEC(severity, time)
 Logs the streamed message at most once every time seconds with a severity of severity. More...
 
#define DLOG(severity)   ION_IGNORE_LOG(severity)
 Same as LOG(severity), but only logs in debug mode. More...
 
#define DLOG_ONCE(severity)   ION_IGNORE_LOG(severity)
 Same as LOG_ONCE(severity), but only logs in debug mode. More...
 
#define DLOG_EVERY_N_SEC(severity, time)   ION_IGNORE_LOG(severity)
 Same as LOG_EVERY_N_SEC(severity, time), but only logs in debug mode. More...
 
Internal CHECK macro implementation

These macros implement Ion's CHECK and DCHECK statements.

Use the non- prefixed CHECK and DCHECK macros in your code instead.

#define ION_LOG_CHECK_MESSAGE(severity, check_type, expr)
 
#define ION_CHECK(expr)
 
#define ION_DCHECK(expr)
 ION_DCHECK is defined normally in debug builds. More...
 
#define ION_CHECK_OP(op, val1, val2)
 
#define ION_DCHECK_OP(op, val1, val2)
 
CHECK and DCHECK macros

These macros will assert that the value they are given satisfies some predicate (for CHECK, the expression must evaluate to true).

If it does not, the process will be forcibly killed. In the DCHECK variants, the check will only be performed in debug mode.

Warning
Do not place code with side effects inside a DCHECK, or the behavior of your program will differ between debug and release mode!
#define CHECK(expr)   ION_CHECK(expr)
 
#define CHECK_EQ(val1, val2)   ION_CHECK_OP(==, val1, val2)
 
#define CHECK_NE(val1, val2)   ION_CHECK_OP(!=, val1, val2)
 
#define CHECK_LE(val1, val2)   ION_CHECK_OP(<=, val1, val2)
 
#define CHECK_LT(val1, val2)   ION_CHECK_OP(<, val1, val2)
 
#define CHECK_GE(val1, val2)   ION_CHECK_OP(>=, val1, val2)
 
#define CHECK_GT(val1, val2)   ION_CHECK_OP(>, val1, val2)
 
#define DCHECK(expr)   ION_DCHECK(expr)
 
#define DCHECK_EQ(val1, val2)   ION_DCHECK_OP(==, val1, val2)
 
#define DCHECK_NE(val1, val2)   ION_DCHECK_OP(!=, val1, val2)
 
#define DCHECK_LE(val1, val2)   ION_DCHECK_OP(<=, val1, val2)
 
#define DCHECK_LT(val1, val2)   ION_DCHECK_OP(<, val1, val2)
 
#define DCHECK_GE(val1, val2)   ION_DCHECK_OP(>=, val1, val2)
 
#define DCHECK_GT(val1, val2)   ION_DCHECK_OP(>, val1, val2)
 
#define CHECK_NOTNULL(val)
 Check that the input is not NULL. More...
 

Functions

void ion::base::SetLogEntryWriter (port::LogEntryWriter *w)
 Public functions. More...
 
port::LogEntryWriter * ion::base::GetLogEntryWriter ()
 Returns the log-writer that messages are currently logged to. More...
 
port::LogEntryWriter * ion::base::GetDefaultLogEntryWriter ()
 Returns the log-writer that messages will be logged to if if another is not explicitly specified via SetLogEntryWriter(). More...
 
void ion::base::SetBreakHandler (const std::function< void()> &break_handler)
 Sets a custom break handler that gets invoked by Logger::CheckMessage. More...
 
void ion::base::RestoreDefaultBreakHandler ()
 Restores the default break handler (port::BreakAndAbort). More...
 
template<typename T >
Tion::base::logging_internal::CheckNotNullCommon (const char *expr_string, T &t)
 Helpers for CHECK_NOTNULL(). More...
 
template<typename T >
Tion::base::logging_internal::CheckNotNull (const char *expr_string, T *t)
 
template<typename T >
Tion::base::logging_internal::CheckNotNull (const char *expr_string, T &t)
 
template<typename T >
const Tion::base::logging_internal::CheckNotNull (const char *expr_string, const T &t)
 This extra overload is needed to support non-const rvalues of non-pointer type. More...
 

Detailed Description

Copyright 2016 Google Inc.

All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Macros for displaying logging messages and checking assertions. Logging and checking facilities are each available in fatal and non-fatal variants, and variants that log only in debug mode vs. always.

Definition in file logging.h.

Macro Definition Documentation

#define CHECK_EQ (   val1,
  val2 
)    ION_CHECK_OP(==, val1, val2)

Definition at line 324 of file logging.h.

#define CHECK_GE (   val1,
  val2 
)    ION_CHECK_OP(>=, val1, val2)

Definition at line 328 of file logging.h.

#define CHECK_GT (   val1,
  val2 
)    ION_CHECK_OP(>, val1, val2)

Definition at line 329 of file logging.h.

Referenced by ion::analytics::SampleMapping::SampleMapping().

#define CHECK_LE (   val1,
  val2 
)    ION_CHECK_OP(<=, val1, val2)

Definition at line 326 of file logging.h.

#define CHECK_LT (   val1,
  val2 
)    ION_CHECK_OP(<, val1, val2)

Definition at line 327 of file logging.h.

Referenced by ion::analytics::SampleMapping::SampleMapping().

#define CHECK_NE (   val1,
  val2 
)    ION_CHECK_OP(!=, val1, val2)

Definition at line 325 of file logging.h.

#define CHECK_NOTNULL (   val)
Value:
"'" #val "' Must be non NULL", (val))
const T & CheckNotNull(const char *expr_string, const T &t)
This extra overload is needed to support non-const rvalues of non-pointer type.
Definition: logging.h:371

Check that the input is not NULL.

Unlike other CHECK macros, this one returns val, so it can be used in initializer lists. Outside initializers, prefer CHECK.

Definition at line 382 of file logging.h.

#define DCHECK (   expr)    ION_DCHECK(expr)

Definition at line 331 of file logging.h.

Referenced by ion::profile::TraceRecorder::AddTraceToTimelineNode(), ion::math::AdjugateWithDeterminant(), ion::base::Allocatable::Allocatable(), ion::base::StlAllocator< T >::allocate(), ion::base::DataContainer::AllocatorDeleter(), ion::profile::TraceRecorder::AnnotateCurrentScopeAtTime(), ion::gfxutils::BufferToAttributeBinder< T >::Apply(), ion::gfx::Renderer::BindFramebuffer(), ion::gfx::BufferObject::BufferSubData::BufferSubData(), ion::gfxutils::BufferToAttributeBinder< T >::BufferToAttributeBinder(), ion::text::Builder::Build(), ion::text::OutlineBuilder::BuildVertexData(), ion::gfxutils::BuildWireframeIndexBuffer(), ion::text::Font::CacheSdfGrids(), ion::math::CofactorMatrix(), ion::gfx::Image::ComputeDataSize(), ion::base::DateTime::ComputeDateString(), ion::text::ComputeTextSize(), ion::base::DateTime::ComputeTimeString(), ion::profile::TraceRecorder::CreateTimeStampAtTime(), ion::base::internal_variant_utils::ManualConstructor< T15 >::Destroy(), ion::math::Determinant(), ion::profile::TraceRecorder::DumpTrace(), ion::profile::TraceRecorder::EnterTimeRange(), ion::math::Rotation< T >::GetAxisAndAngle(), ion::portgfx::Visual::GetCurrent(), ion::base::DateTime::GetDateTimeField(), ion::math::Rotation< T >::GetEulerAngles(), ion::text::Builder::GetFontImageTexture(), ion::base::CircularBuffer< uint32 >::GetItem(), ion::text::Font::GetMutableGlyphGridLocked(), ion::profile::CallTraceManager::GetNamedTraceRecorder(), ion::gfx::Image::GetNumComponentsForFormat(), ion::gfx::UniformHolder::GetUniformIndex(), ion::base::internal_variant_utils::ManualConstructor< T15 >::Init(), ion::base::internal_variant_utils::ManualConstructor< T15 >::InitArray(), ion::analytics::GpuPerformanceTester::InstanceCopy(), ion::math::LookAtMatrixFromDir(), ion::analytics::GpuPerformanceTester::MeasureBaseline(), ion::analytics::GpuPerformanceTester::MeasureMinViewportSpeed(), ion::analytics::GpuPerformanceTester::MeasurePerformance(), ion::analytics::GpuPerformanceTester::MeasureStateChanges(), ion::math::Normalize(), ion::base::SharedPtr< AllocationTracker >::operator*(), ion::base::SharedPtr< AllocationTracker >::operator->(), ion::math::VectorBase< Dimension, T >::operator[](), ion::math::OrthoInverseH(), ion::base::ZipAssetManager::RegisterAssetData(), ion::remote::HttpServer::RegisterHandler(), ion::gfx::Renderer::Renderer(), ion::gfx::StateTable::ResetValue(), ion::profile::ScopedFrameTracer::ScopedFrameTracer(), ion::profile::ScopedTracer::ScopedTracer(), ion::remote::HttpClient::Url::Set(), ion::gfx::ResourceHolder::SetResource(), ion::gfx::ShaderProgram::ShaderProgram(), ion::text::Builder::StoreGlyphVertices(), ion::portgfx::Visual::TeardownVisual(), ion::base::SpinMutex::Unlock(), ion::gfx::UpdateFromStateTable(), ion::gfx::UpdateSettingsInStateTable(), ion::gfx::UpdateStateTable(), ion::text::OutlineBuilder::UpdateUniforms(), ion::gfx::ResourceHolder::VectorField< T >::VectorField(), ion::profile::VSyncProfiler::VSyncProfiler(), ion::gfxutils::ResourceCallback< T >::~ResourceCallback(), ion::profile::ScopedFrameTracer::~ScopedFrameTracer(), ion::profile::ScopedTracer::~ScopedTracer(), ion::base::SpinMutex::~SpinMutex(), and ion::base::WeakReferent::~WeakReferent().

#define DCHECK_NE (   val1,
  val2 
)    ION_DCHECK_OP(!=, val1, val2)
#define DLOG (   severity)    ION_IGNORE_LOG(severity)
#define DLOG_EVERY_N_SEC (   severity,
  time 
)    ION_IGNORE_LOG(severity)

Same as LOG_EVERY_N_SEC(severity, time), but only logs in debug mode.

Definition at line 236 of file logging.h.

#define DLOG_ONCE (   severity)    ION_IGNORE_LOG(severity)

Same as LOG_ONCE(severity), but only logs in debug mode.

Definition at line 233 of file logging.h.

#define ION_CHECK (   expr)
Value:
if (expr) \
; \
ION_LOG_CHECK_MESSAGE(FATAL, "CHECK", #expr)
#define ION_LOG_CHECK_MESSAGE(severity, check_type, expr)
Definition: logging.h:245

Definition at line 249 of file logging.h.

#define ION_CHECK_OP (   op,
  val1,
  val2 
)
Value:
ION_CHECK((val1) op (val2)) << "(" << (val1) << " " << #op /* NOLINT */ \
<< " " << (val2) << ")\n"
#define ION_CHECK(expr)
Definition: logging.h:249

Definition at line 305 of file logging.h.

#define ION_DCHECK (   expr)
Value:
if (true || (false && (expr))) \
; \
else \
::ion::base::logging_internal::NullLogger().GetStream()

ION_DCHECK is defined normally in debug builds.

In optimized and production builds, we still want to compile expr in order to avoid unused variable warnings and to ensure that expr is valid code.

We do not evaluate expr at runtime, though (the compiler optimizes it away), hence we also never use any strings that might get passed to ION_DCHECK using operator<< and we can discard them by passing them into a NullLogger. Using a NullLogger instead of a regular Logger results in fewer instructions after preprocessing, and increases the likelihood for small functions to be inlined in compilers that are sensitive to that (such as Visual C++).

An efficient way to achieve this is to let a call such as DCHECK(expr) << "foo"

to be expanded to (true) ? (void)(expr) : ::ion::base::logging_internal::NullLogger().GetStream() << "foo"

This ensures three things: (1) expr gets compiled, but its generated code gets thrown away. (2) Callers can still pass in a debug string that then gets thrown away. (3) The entire expression is a no-op.

Some toolchains do not support the ternary operator syntax since they require the second and third operand to be of the same type. Hence by default we use an if/else clause instead. Visual Studio seems to have a higher likelihood of inlining for small functions that use ION_DCHECKs if they expand to the ternary operator, so we use the ternary operator on Windows.

Definition at line 297 of file logging.h.

#define ION_DCHECK_OP (   op,
  val1,
  val2 
)
Value:
ION_DCHECK((val1) op (val2)) << "(" << (val1) << " " << #op /* NOLINT */ \
<< " " << (val2) << ")\n"
#define ION_DCHECK(expr)
ION_DCHECK is defined normally in debug builds.
Definition: logging.h:297

Definition at line 308 of file logging.h.

#define ION_IGNORE_LOG (   severity)    ::ion::base::logging_internal::NullLogger().GetStream()

Macro that ignores logging.

Definition at line 183 of file logging.h.

#define ION_LOG (   severity)
Value:
::ion::port::severity).GetStream()
std::ostream & GetStream()
Return the stream to which output is sent (or accumulated).
Definition: logging.cc:132
This class is used for regular logging.
Definition: logging.h:73

Completely disable logging in production builds.

Macro that actually causes logging to happen.

Definition at line 178 of file logging.h.

Referenced by ion::base::logging_internal::CheckNotNullCommon().

#define ION_LOG_CHECK_MESSAGE (   severity,
  check_type,
  expr 
)
Value:
check_type, #expr)
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
static const std::string CheckMessage(const char *check_string, const char *expr_string)
Returns a message that can be used in CHECK or DCHECK output.
Definition: logging.cc:134

Definition at line 245 of file logging.h.

#define ION_LOG_EVERY_N_SEC (   severity,
  time 
)
Value:
__FILE__, __LINE__, ::ion::port::severity, time).GetStream()
This class prints a message only if the passed file_name and line_number has not printed a message in...
Definition: logging.h:133
std::ostream & GetStream()
Return the stream to which output is sent (or accumulated).
Definition: logging.cc:178

Macro that logs a message at most every N seconds.

Definition at line 192 of file logging.h.

#define ION_LOG_ONCE (   severity)
Value:
__FILE__, __LINE__, ::ion::port::severity).GetStream()
std::ostream & GetStream()
Return the stream to which output is sent (or accumulated).
Definition: logging.cc:158
This class prints a message only the first time it is created for the passed file_name and line_numbe...
Definition: logging.h:110

Macro that logs a message only once.

Definition at line 187 of file logging.h.

#define LOG (   severity)    ION_LOG(severity)

Logs the streamed message unconditionally with a severity of severity.

Definition at line 216 of file logging.h.

Referenced by ion::gfx::ShaderInputRegistry::Add(), ion::gfx::ResourceHolder::VectorField< T >::Add(), ion::text::FontManager::AddFontFromZipasset(), ion::gfx::BufferObject::AddSpec(), ion::gfx::Shape::AddVertexRange(), ion::gfxutils::BufferToAttributeBinder< T >::AreBindingsPacked(), ion::gfxutils::Frame::Begin(), ion::text::Font::CacheSdfGrid(), ion::base::OnceFlag::CallChecked(), ion::gfx::ShaderInputRegistry::CheckInputsAreUnique(), ion::portgfx::Visual::CreateVisualInCurrentShareGroup(), ion::image::DownsampleImage2x(), ion::gfxutils::Frame::End(), ion::gfx::Texture::ExpectedDimensionsForMipmap(), ion::gfx::FramebufferObject::FramebufferObject(), ion::base::DateTime::FromString(), ion::gfx::GraphicsManager::GetCapabilityValue(), ion::base::DateTime::GetDateTimeField(), ion::portgfx::Visual::GetGlVersion(), ion::base::DataContainer::GetMutableData(), ion::profile::CallTraceManager::GetNamedTraceRecorder(), ion::gfx::BufferObject::GetSpec(), ion::gfx::Renderer::GetStateTable(), ion::gfx::ShaderInputRegistry::Include(), ion::portgfx::IsExtensionSupported(), ion::portgfx::Visual::MakeCurrent(), ion::gfx::Renderer::MapBufferObjectData(), ion::gfx::Renderer::MapBufferObjectDataRange(), ion::gfxprofile::GpuProfiler::PollGlTimerQueries(), ion::image::ResizeImage(), ion::gfx::Renderer::ResolveMultisampleFramebuffer(), ion::remote::HttpClient::Url::Set(), ion::gfx::ResourceHolder::Field< std::string >::Set(), ion::gfx::ShaderProgram::SetConcurrent(), ion::base::DateTime::SetDay(), ion::analytics::GpuPerformanceTester::SetEnables(), ion::base::DateTime::SetHour(), ion::gfx::CubeMapTexture::SetImage(), ion::gfx::Texture::SetImage(), ion::gfx::TextureBase::SetImmutableImage(), ion::base::DateTime::SetMinute(), ion::base::DateTime::SetMonth(), ion::gfx::TextureBase::SetMultisampling(), ion::base::DateTime::SetNanosecond(), ion::base::DateTime::SetSecond(), ion::gfx::TextureManager::SetUnitRange(), ion::gfx::Shape::SetVertexRange(), ion::base::DateTime::SetZoneHours(), ion::base::DateTime::SetZoneMinutes(), ion::base::FullAllocationTracker::TrackDeallocation(), ion::gfx::Renderer::UnmapBufferObjectData(), ion::profile::CallTraceManager::WriteFile(), ion::base::LogChecker::~LogChecker(), and ion::gfx::Renderer::~Renderer().

#define LOG_EVERY_N_SEC (   severity,
  time 
)    ION_LOG_EVERY_N_SEC(severity, time)

Logs the streamed message at most once every time seconds with a severity of severity.

Definition at line 223 of file logging.h.

#define LOG_ONCE (   severity)    ION_LOG_ONCE(severity)

Logs the streamed message once per process run with a severity of severity.

Definition at line 219 of file logging.h.

Referenced by ion::profile::TraceRecorder::GetCurrentFrameNumber(), ion::profile::TraceRecorder::LeaveFrame(), and ion::profile::VSyncProfiler::RecordVSyncEvent().