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 // Indicates a string should be added to the log.
22 // First parameter - The string to add to the log.
23 typedef base::Callback
<void(const std::string
&)> LogCB
;
25 // Helper class to make it easier to use log_cb like DVLOG().
28 LogHelper(const LogCB
& Log_cb
);
31 std::ostream
& stream() { return stream_
; }
35 std::stringstream stream_
;
38 #define MEDIA_LOG(log_cb) LogHelper(log_cb).stream()
40 // Logs only while count < max. Increments count for each log. Use LAZY_STREAM
41 // to avoid wasteful evaluation of subsequent stream arguments.
42 #define LIMITED_MEDIA_LOG(log_cb, count, max) \
43 LAZY_STREAM(MEDIA_LOG(log_cb), (count) < (max) && ((count)++ || true))
45 class MEDIA_EXPORT MediaLog
: public base::RefCountedThreadSafe
<MediaLog
> {
47 // Convert various enums to strings.
48 static std::string
EventTypeToString(MediaLogEvent::Type type
);
49 static std::string
PipelineStatusToString(PipelineStatus status
);
53 // Add an event to this log. Overriden by inheritors to actually do something
55 virtual void AddEvent(scoped_ptr
<MediaLogEvent
> event
);
57 // Helper methods to create events and their parameters.
58 scoped_ptr
<MediaLogEvent
> CreateEvent(MediaLogEvent::Type type
);
59 scoped_ptr
<MediaLogEvent
> CreateBooleanEvent(
60 MediaLogEvent::Type type
, const std::string
& property
, bool value
);
61 scoped_ptr
<MediaLogEvent
> CreateStringEvent(MediaLogEvent::Type type
,
62 const std::string
& property
,
63 const std::string
& value
);
64 scoped_ptr
<MediaLogEvent
> CreateTimeEvent(MediaLogEvent::Type type
,
65 const std::string
& property
,
66 base::TimeDelta value
);
67 scoped_ptr
<MediaLogEvent
> CreateLoadEvent(const std::string
& url
);
68 scoped_ptr
<MediaLogEvent
> CreateSeekEvent(float seconds
);
69 scoped_ptr
<MediaLogEvent
> CreatePipelineStateChangedEvent(
70 Pipeline::State state
);
71 scoped_ptr
<MediaLogEvent
> CreatePipelineErrorEvent(PipelineStatus error
);
72 scoped_ptr
<MediaLogEvent
> CreateVideoSizeSetEvent(
73 size_t width
, size_t height
);
74 scoped_ptr
<MediaLogEvent
> CreateBufferedExtentsChangedEvent(
75 int64 start
, int64 current
, int64 end
);
76 scoped_ptr
<MediaLogEvent
> CreateMediaSourceErrorEvent(
77 const std::string
& error
);
79 // Report a property change without an accompanying event.
80 void SetStringProperty(const std::string
& key
, const std::string
& value
);
81 void SetIntegerProperty(const std::string
& key
, int value
);
82 void SetDoubleProperty(const std::string
& key
, double value
);
83 void SetBooleanProperty(const std::string
& key
, bool value
);
84 void SetTimeProperty(const std::string
& key
, base::TimeDelta value
);
87 friend class base::RefCountedThreadSafe
<MediaLog
>;
91 // A unique (to this process) id for this MediaLog.
94 DISALLOW_COPY_AND_ASSIGN(MediaLog
);
99 #endif // MEDIA_BASE_MEDIA_LOG_H_