1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file defines useful logging macros/methods for CDM adapter.
7 #ifndef MEDIA_CDM_PPAPI_CDM_LOGGING_H_
8 #define MEDIA_CDM_PPAPI_CDM_LOGGING_H_
18 // The following classes/macros are adapted from base/logging.h.
20 // This class is used to explicitly ignore values in the conditional
21 // logging macros. This avoids compiler warnings like "value computed
22 // is not used" and "statement has no effect".
23 class LogMessageVoidify
{
25 LogMessageVoidify() {}
26 // This has to be an operator with a precedence lower than << but
28 void operator&(std::ostream
&) {}
33 // This class is used to avoid having to include <iostream> in this file.
38 // Retrieves the stream that we write to. This header cannot depend on
39 // <iostream> because that will add static initializers to all files that
40 // include this header. See crbug.com/94794.
41 std::ostream
& stream();
44 // This class serves two purposes:
45 // (1) It adds common headers to the log message, e.g. timestamp, process ID.
46 // (2) It adds a line break at the end of the log message.
47 // This class is copied and modified from base/logging.* but is quite different
48 // in terms of how things work. This class is designed to work only with the
49 // CDM_DLOG() defined below and should not be used for other purposes.
52 CdmLogMessage(const char* file
, int line
);
55 std::string
message() { return stream_
.str(); }
58 std::ostringstream stream_
;
61 // Helper macro which avoids evaluating the arguments to a stream if
62 // the condition doesn't hold.
63 #define CDM_LAZY_STREAM(stream, condition) \
64 !(condition) ? (void) 0 : LogMessageVoidify() & (stream)
67 // Logging is disabled for the release builds, theoretically the compiler should
68 // take care of removing the references to CdmLogMessage but it's not always the
69 // case when some specific optimizations are turned on (like PGO). Update the
70 // macro to make sure that we don't try to do any logging or to refer to
71 // CdmLogMessage in release.
72 #define CDM_DLOG_IS_ON() false
73 #define CDM_DLOG() CDM_LAZY_STREAM(CdmLogStream().stream(), CDM_DLOG_IS_ON())
75 #define CDM_DLOG_IS_ON() true
76 #define CDM_DLOG() CDM_LAZY_STREAM(CdmLogStream().stream(), CDM_DLOG_IS_ON()) \
77 << CdmLogMessage(__FILE__, __LINE__).message()
82 #endif // MEDIA_CDM_PPAPI_CDM_LOGGING_H_