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.
4
5// This file defines useful logging macros/methods for CDM adapter.
6
7#ifndef MEDIA_CDM_PPAPI_CDM_LOGGING_H_
8#define MEDIA_CDM_PPAPI_CDM_LOGGING_H_
9
10#include <iostream>
11#include <sstream>
12#include <string>
13
14namespace media {
15
16namespace {
17
18// The following classes/macros are adapted from base/logging.h.
19
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".
23class 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&) {}
29};
30
31}  // namespace
32
33// This class serves two purposes:
34// (1) It adds common headers to the log message, e.g. timestamp, process ID.
35// (2) It adds a line break at the end of the log message.
36// This class is copied and modified from base/logging.* but is quite different
37// in terms of how things work. This class is designed to work only with the
38// CDM_DLOG() defined below and should not be used for other purposes.
39class CdmLogMessage {
40 public:
41  CdmLogMessage(const char* file, int line);
42  ~CdmLogMessage();
43
44  std::string message() { return stream_.str(); }
45
46 private:
47  std::ostringstream stream_;
48};
49
50// Helper macro which avoids evaluating the arguments to a stream if
51// the condition doesn't hold.
52#define CDM_LAZY_STREAM(stream, condition) \
53  !(condition) ? (void) 0 : LogMessageVoidify() & (stream)
54
55#define CDM_DLOG() CDM_LAZY_STREAM(std::cout, CDM_DLOG_IS_ON()) \
56  << CdmLogMessage(__FILE__, __LINE__).message()
57
58#if defined(NDEBUG)
59#define CDM_DLOG_IS_ON() false
60#else
61#define CDM_DLOG_IS_ON() true
62#endif
63
64}  // namespace media
65
66#endif  // MEDIA_CDM_PPAPI_CDM_LOGGING_H_
67