base::ManualConstructor improvements
[chromium-blink-merge.git] / media / cdm / ppapi / cdm_logging.h
blob63961f2b78a6265bfe40f3373b0a2cb274bd07a0
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_
10 #include <ostream>
11 #include <sstream>
12 #include <string>
14 namespace media {
16 namespace {
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 {
24 public:
25 LogMessageVoidify() {}
26 // This has to be an operator with a precedence lower than << but
27 // higher than ?:
28 void operator&(std::ostream&) {}
31 } // namespace
33 // This class is used to avoid having to include <iostream> in this file.
34 class CdmLogStream {
35 public:
36 CdmLogStream() {}
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.
50 class CdmLogMessage {
51 public:
52 CdmLogMessage(const char* file, int line);
53 ~CdmLogMessage();
55 std::string message() { return stream_.str(); }
57 private:
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)
66 #if defined(NDEBUG)
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())
74 #else
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()
78 #endif
80 } // namespace media
82 #endif // MEDIA_CDM_PPAPI_CDM_LOGGING_H_