1 // Copyright (c) 2012 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 #ifndef MEDIA_BASE_MEDIA_LOG_H_
6 #define MEDIA_BASE_MEDIA_LOG_H_
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "media/base/media_export.h"
15 #include "media/base/media_log_event.h"
16 #include "media/base/pipeline.h"
17 #include "media/base/pipeline_status.h"
21 class MEDIA_EXPORT MediaLog
: public base::RefCountedThreadSafe
<MediaLog
> {
29 // Convert various enums to strings.
30 static std::string
MediaLogLevelToString(MediaLogLevel level
);
31 static MediaLogEvent::Type
MediaLogLevelToEventType(MediaLogLevel level
);
32 static std::string
EventTypeToString(MediaLogEvent::Type type
);
33 static std::string
PipelineStatusToString(PipelineStatus status
);
35 static std::string
MediaEventToLogString(const MediaLogEvent
& event
);
39 // Add an event to this log. Overriden by inheritors to actually do something
41 virtual void AddEvent(scoped_ptr
<MediaLogEvent
> event
);
43 // Helper methods to create events and their parameters.
44 scoped_ptr
<MediaLogEvent
> CreateEvent(MediaLogEvent::Type type
);
45 scoped_ptr
<MediaLogEvent
> CreateBooleanEvent(
46 MediaLogEvent::Type type
, const std::string
& property
, bool value
);
47 scoped_ptr
<MediaLogEvent
> CreateStringEvent(MediaLogEvent::Type type
,
48 const std::string
& property
,
49 const std::string
& value
);
50 scoped_ptr
<MediaLogEvent
> CreateTimeEvent(MediaLogEvent::Type type
,
51 const std::string
& property
,
52 base::TimeDelta value
);
53 scoped_ptr
<MediaLogEvent
> CreateLoadEvent(const std::string
& url
);
54 scoped_ptr
<MediaLogEvent
> CreateSeekEvent(float seconds
);
55 scoped_ptr
<MediaLogEvent
> CreatePipelineStateChangedEvent(
56 Pipeline::State state
);
57 scoped_ptr
<MediaLogEvent
> CreatePipelineErrorEvent(PipelineStatus error
);
58 scoped_ptr
<MediaLogEvent
> CreateVideoSizeSetEvent(
59 size_t width
, size_t height
);
60 scoped_ptr
<MediaLogEvent
> CreateBufferedExtentsChangedEvent(
61 int64 start
, int64 current
, int64 end
);
63 // Report a log message at the specified log level.
64 void AddLogEvent(MediaLogLevel level
, const std::string
& message
);
66 // Report a property change without an accompanying event.
67 void SetStringProperty(const std::string
& key
, const std::string
& value
);
68 void SetIntegerProperty(const std::string
& key
, int value
);
69 void SetDoubleProperty(const std::string
& key
, double value
);
70 void SetBooleanProperty(const std::string
& key
, bool value
);
71 void SetTimeProperty(const std::string
& key
, base::TimeDelta value
);
74 friend class base::RefCountedThreadSafe
<MediaLog
>;
78 // A unique (to this process) id for this MediaLog.
81 DISALLOW_COPY_AND_ASSIGN(MediaLog
);
84 // Helper class to make it easier to use MediaLog like DVLOG().
85 class MEDIA_EXPORT LogHelper
{
87 LogHelper(MediaLog::MediaLogLevel level
,
88 const scoped_refptr
<MediaLog
>& media_log
);
91 std::ostream
& stream() { return stream_
; }
94 MediaLog::MediaLogLevel level_
;
95 const scoped_refptr
<MediaLog
> media_log_
;
96 std::stringstream stream_
;
99 // Provides a stringstream to collect a log entry to pass to the provided
100 // MediaLog at the requested level.
101 #define MEDIA_LOG(level, media_log) \
102 LogHelper((MediaLog::MEDIALOG_##level), (media_log)).stream()
104 // Logs only while |count| < |max|, increments |count| for each log, and warns
105 // in the log if |count| has just reached |max|.
106 // Multiple short-circuit evaluations are involved in this macro:
107 // 1) LAZY_STREAM avoids wasteful MEDIA_LOG and evaluation of subsequent stream
108 // arguments if |count| is >= |max|, and
109 // 2) the |condition| given to LAZY_STREAM itself short-circuits to prevent
110 // incrementing |count| beyond |max|.
111 // Note that LAZY_STREAM guarantees exactly one evaluation of |condition|, so
112 // |count| will be incremented at most once each time this macro runs.
113 // The "|| true" portion of |condition| lets logging occur correctly when
114 // |count| < |max| and |count|++ is 0.
115 // TODO(wolenetz,chcunningham): Consider using a helper class instead of a macro
116 // to improve readability.
117 #define LIMITED_MEDIA_LOG(level, media_log, count, max) \
118 LAZY_STREAM(MEDIA_LOG(level, media_log), \
119 (count) < (max) && ((count)++ || true)) \
120 << (((count) == (max)) ? "(Log limit reached. Further similar entries " \
121 "may be suppressed): " \
126 #endif // MEDIA_BASE_MEDIA_LOG_H_