15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file defines useful logging macros/methods for CDM adapter.
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef MEDIA_CDM_PPAPI_CDM_LOGGING_H_
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define MEDIA_CDM_PPAPI_CDM_LOGGING_H_
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <iostream>
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sstream>
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace media {
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The following classes/macros are adapted from base/logging.h.
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This class is used to explicitly ignore values in the conditional
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// logging macros.  This avoids compiler warnings like "value computed
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// is not used" and "statement has no effect".
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class LogMessageVoidify {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LogMessageVoidify() {}
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This has to be an operator with a precedence lower than << but
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // higher than ?:
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void operator&(std::ostream&) {}
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This class serves two purposes:
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// (1) It adds common headers to the log message, e.g. timestamp, process ID.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// (2) It adds a line break at the end of the log message.
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This class is copied and modified from base/logging.* but is quite different
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// in terms of how things work. This class is designed to work only with the
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// CDM_DLOG() defined below and should not be used for other purposes.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class CdmLogMessage {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CdmLogMessage(const char* file, int line);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~CdmLogMessage();
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string message() { return stream_.str(); }
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::ostringstream stream_;
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Helper macro which avoids evaluating the arguments to a stream if
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the condition doesn't hold.
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CDM_LAZY_STREAM(stream, condition) \
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  !(condition) ? (void) 0 : LogMessageVoidify() & (stream)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CDM_DLOG() CDM_LAZY_STREAM(std::cout, CDM_DLOG_IS_ON()) \
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  << CdmLogMessage(__FILE__, __LINE__).message()
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(NDEBUG)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CDM_DLOG_IS_ON() false
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CDM_DLOG_IS_ON() true
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace media
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // MEDIA_CDM_PPAPI_CDM_LOGGING_H_
67