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"
22 class MEDIA_EXPORT MediaLog
: public base::RefCountedThreadSafe
<MediaLog
> {
30 // Convert various enums to strings.
31 static std::string
MediaLogLevelToString(MediaLogLevel level
);
32 static MediaLogEvent::Type
MediaLogLevelToEventType(MediaLogLevel level
);
33 static std::string
EventTypeToString(MediaLogEvent::Type type
);
34 static std::string
PipelineStatusToString(PipelineStatus status
);
36 static std::string
MediaEventToLogString(const MediaLogEvent
& event
);
40 // Add an event to this log. Overriden by inheritors to actually do something
42 virtual void AddEvent(scoped_ptr
<MediaLogEvent
> event
);
44 // Helper methods to create events and their parameters.
45 scoped_ptr
<MediaLogEvent
> CreateEvent(MediaLogEvent::Type type
);
46 scoped_ptr
<MediaLogEvent
> CreateBooleanEvent(
47 MediaLogEvent::Type type
, const std::string
& property
, bool value
);
48 scoped_ptr
<MediaLogEvent
> CreateStringEvent(MediaLogEvent::Type type
,
49 const std::string
& property
,
50 const std::string
& value
);
51 scoped_ptr
<MediaLogEvent
> CreateTimeEvent(MediaLogEvent::Type type
,
52 const std::string
& property
,
53 base::TimeDelta value
);
54 scoped_ptr
<MediaLogEvent
> CreateLoadEvent(const std::string
& url
);
55 scoped_ptr
<MediaLogEvent
> CreateSeekEvent(float seconds
);
56 scoped_ptr
<MediaLogEvent
> CreatePipelineStateChangedEvent(
57 Pipeline::State state
);
58 scoped_ptr
<MediaLogEvent
> CreatePipelineErrorEvent(PipelineStatus error
);
59 scoped_ptr
<MediaLogEvent
> CreateVideoSizeSetEvent(
60 size_t width
, size_t height
);
61 scoped_ptr
<MediaLogEvent
> CreateBufferedExtentsChangedEvent(
62 int64 start
, int64 current
, int64 end
);
64 // Report a log message at the specified log level.
65 void AddLogEvent(MediaLogLevel level
, const std::string
& message
);
67 // Report a property change without an accompanying event.
68 void SetStringProperty(const std::string
& key
, const std::string
& value
);
69 void SetIntegerProperty(const std::string
& key
, int value
);
70 void SetDoubleProperty(const std::string
& key
, double value
);
71 void SetBooleanProperty(const std::string
& key
, bool value
);
72 void SetTimeProperty(const std::string
& key
, base::TimeDelta value
);
75 friend class base::RefCountedThreadSafe
<MediaLog
>;
79 // A unique (to this process) id for this MediaLog.
82 DISALLOW_COPY_AND_ASSIGN(MediaLog
);
85 // Indicates a string should be added to the log.
86 // First parameter - The log level for the string.
87 // Second parameter - The string to add to the log.
88 typedef base::Callback
<void(MediaLog::MediaLogLevel
, const std::string
&)> LogCB
;
90 // Helper class to make it easier to use log_cb like DVLOG().
93 LogHelper(MediaLog::MediaLogLevel level
, const LogCB
& log_cb
);
96 std::ostream
& stream() { return stream_
; }
99 MediaLog::MediaLogLevel level_
;
101 std::stringstream stream_
;
104 // Provides a stringstream to collect a log entry to pass to the provided
105 // LogCB at the requested level.
106 #define MEDIA_LOG(level, log_cb) \
107 LogHelper((MediaLog::MEDIALOG_##level), (log_cb)).stream()
109 // Logs only while count < max. Increments count for each log. Use LAZY_STREAM
110 // to avoid wasteful evaluation of subsequent stream arguments.
111 #define LIMITED_MEDIA_LOG(level, log_cb, count, max) \
112 LAZY_STREAM(MEDIA_LOG(level, log_cb), (count) < (max) && ((count)++ || true))
116 #endif // MEDIA_BASE_MEDIA_LOG_H_