OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_MEDIA_LOG_H_ | 5 #ifndef MEDIA_BASE_MEDIA_LOG_H_ |
6 #define MEDIA_BASE_MEDIA_LOG_H_ | 6 #define MEDIA_BASE_MEDIA_LOG_H_ |
7 | 7 |
8 #include <sstream> | 8 #include <sstream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
15 #include "media/base/media_log_event.h" | 15 #include "media/base/media_log_event.h" |
16 #include "media/base/pipeline.h" | 16 #include "media/base/pipeline.h" |
17 #include "media/base/pipeline_status.h" | 17 #include "media/base/pipeline_status.h" |
18 | 18 |
19 namespace media { | 19 namespace media { |
20 | 20 |
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; | |
24 | |
25 // Helper class to make it easier to use log_cb like DVLOG(). | |
26 class LogHelper { | |
27 public: | |
28 LogHelper(const LogCB& Log_cb); | |
29 ~LogHelper(); | |
30 | |
31 std::ostream& stream() { return stream_; } | |
32 | |
33 private: | |
34 LogCB log_cb_; | |
35 std::stringstream stream_; | |
36 }; | |
37 | |
38 #define MEDIA_LOG(log_cb) LogHelper(log_cb).stream() | |
39 | |
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)) | |
44 | 21 |
45 class MEDIA_EXPORT MediaLog : public base::RefCountedThreadSafe<MediaLog> { | 22 class MEDIA_EXPORT MediaLog : public base::RefCountedThreadSafe<MediaLog> { |
46 public: | 23 public: |
| 24 enum MediaLogLevel { |
| 25 MEDIALOG_ERROR, |
| 26 MEDIALOG_INFO, |
| 27 MEDIALOG_DEBUG, |
| 28 }; |
| 29 |
47 // Convert various enums to strings. | 30 // Convert various enums to strings. |
| 31 static std::string MediaLogLevelToString(MediaLogLevel level); |
| 32 static MediaLogEvent::Type MediaLogLevelToEventType(MediaLogLevel level); |
48 static std::string EventTypeToString(MediaLogEvent::Type type); | 33 static std::string EventTypeToString(MediaLogEvent::Type type); |
49 static std::string PipelineStatusToString(PipelineStatus status); | 34 static std::string PipelineStatusToString(PipelineStatus status); |
50 | 35 |
51 static std::string MediaEventToLogString(const MediaLogEvent& event); | 36 static std::string MediaEventToLogString(const MediaLogEvent& event); |
52 | 37 |
53 MediaLog(); | 38 MediaLog(); |
54 | 39 |
55 // Add an event to this log. Overriden by inheritors to actually do something | 40 // Add an event to this log. Overriden by inheritors to actually do something |
56 // with it. | 41 // with it. |
57 virtual void AddEvent(scoped_ptr<MediaLogEvent> event); | 42 virtual void AddEvent(scoped_ptr<MediaLogEvent> event); |
(...skipping 10 matching lines...) Expand all Loading... |
68 base::TimeDelta value); | 53 base::TimeDelta value); |
69 scoped_ptr<MediaLogEvent> CreateLoadEvent(const std::string& url); | 54 scoped_ptr<MediaLogEvent> CreateLoadEvent(const std::string& url); |
70 scoped_ptr<MediaLogEvent> CreateSeekEvent(float seconds); | 55 scoped_ptr<MediaLogEvent> CreateSeekEvent(float seconds); |
71 scoped_ptr<MediaLogEvent> CreatePipelineStateChangedEvent( | 56 scoped_ptr<MediaLogEvent> CreatePipelineStateChangedEvent( |
72 Pipeline::State state); | 57 Pipeline::State state); |
73 scoped_ptr<MediaLogEvent> CreatePipelineErrorEvent(PipelineStatus error); | 58 scoped_ptr<MediaLogEvent> CreatePipelineErrorEvent(PipelineStatus error); |
74 scoped_ptr<MediaLogEvent> CreateVideoSizeSetEvent( | 59 scoped_ptr<MediaLogEvent> CreateVideoSizeSetEvent( |
75 size_t width, size_t height); | 60 size_t width, size_t height); |
76 scoped_ptr<MediaLogEvent> CreateBufferedExtentsChangedEvent( | 61 scoped_ptr<MediaLogEvent> CreateBufferedExtentsChangedEvent( |
77 int64 start, int64 current, int64 end); | 62 int64 start, int64 current, int64 end); |
78 scoped_ptr<MediaLogEvent> CreateMediaSourceErrorEvent( | 63 scoped_ptr<MediaLogEvent> CreateLogEvent(MediaLogLevel level, |
79 const std::string& error); | 64 const std::string& message); |
80 | 65 |
81 // Report a property change without an accompanying event. | 66 // Report a property change without an accompanying event. |
82 void SetStringProperty(const std::string& key, const std::string& value); | 67 void SetStringProperty(const std::string& key, const std::string& value); |
83 void SetIntegerProperty(const std::string& key, int value); | 68 void SetIntegerProperty(const std::string& key, int value); |
84 void SetDoubleProperty(const std::string& key, double value); | 69 void SetDoubleProperty(const std::string& key, double value); |
85 void SetBooleanProperty(const std::string& key, bool value); | 70 void SetBooleanProperty(const std::string& key, bool value); |
86 void SetTimeProperty(const std::string& key, base::TimeDelta value); | 71 void SetTimeProperty(const std::string& key, base::TimeDelta value); |
87 | 72 |
88 protected: | 73 protected: |
89 friend class base::RefCountedThreadSafe<MediaLog>; | 74 friend class base::RefCountedThreadSafe<MediaLog>; |
90 virtual ~MediaLog(); | 75 virtual ~MediaLog(); |
91 | 76 |
92 private: | 77 private: |
93 // A unique (to this process) id for this MediaLog. | 78 // A unique (to this process) id for this MediaLog. |
94 int32 id_; | 79 int32 id_; |
95 | 80 |
96 DISALLOW_COPY_AND_ASSIGN(MediaLog); | 81 DISALLOW_COPY_AND_ASSIGN(MediaLog); |
97 }; | 82 }; |
98 | 83 |
| 84 // Indicates a string should be added to the log. |
| 85 // First parameter - The log level for the string. |
| 86 // Second parameter - The string to add to the log. |
| 87 typedef base::Callback<void(MediaLog::MediaLogLevel, const std::string&)> LogCB; |
| 88 |
| 89 // Helper class to make it easier to use log_cb like DVLOG(). |
| 90 class LogHelper { |
| 91 public: |
| 92 LogHelper(MediaLog::MediaLogLevel level, const LogCB& log_cb); |
| 93 ~LogHelper(); |
| 94 |
| 95 std::ostream& stream() { return stream_; } |
| 96 |
| 97 private: |
| 98 MediaLog::MediaLogLevel level_; |
| 99 LogCB log_cb_; |
| 100 std::stringstream stream_; |
| 101 }; |
| 102 |
| 103 // Provides a stringstream to collect a log entry to pass to the provided |
| 104 // LogCB at the requested level. |
| 105 #define MEDIA_LOG(level, log_cb) \ |
| 106 LogHelper((MediaLog::MEDIALOG_##level), (log_cb)).stream() |
| 107 |
| 108 // Logs only while count < max. Increments count for each log. Use LAZY_STREAM |
| 109 // to avoid wasteful evaluation of subsequent stream arguments. |
| 110 #define LIMITED_MEDIA_LOG(level, log_cb, count, max) \ |
| 111 LAZY_STREAM(MEDIA_LOG(level, log_cb), (count) < (max) && ((count)++ || true)) |
| 112 |
99 } // namespace media | 113 } // namespace media |
100 | 114 |
101 #endif // MEDIA_BASE_MEDIA_LOG_H_ | 115 #endif // MEDIA_BASE_MEDIA_LOG_H_ |
OLD | NEW |