Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / media_log.h
blob1e4b9464dd761a6deecf1616eaeabcec3ed6d38b
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_
8 #include <sstream>
9 #include <string>
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"
19 namespace media {
21 class MEDIA_EXPORT MediaLog : public base::RefCountedThreadSafe<MediaLog> {
22 public:
23 enum MediaLogLevel {
24 MEDIALOG_ERROR,
25 MEDIALOG_INFO,
26 MEDIALOG_DEBUG,
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);
37 MediaLog();
39 // Add an event to this log. Overriden by inheritors to actually do something
40 // with it.
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);
73 protected:
74 friend class base::RefCountedThreadSafe<MediaLog>;
75 virtual ~MediaLog();
77 private:
78 // A unique (to this process) id for this MediaLog.
79 int32 id_;
81 DISALLOW_COPY_AND_ASSIGN(MediaLog);
84 // Helper class to make it easier to use MediaLog like DVLOG().
85 class MEDIA_EXPORT LogHelper {
86 public:
87 LogHelper(MediaLog::MediaLogLevel level,
88 const scoped_refptr<MediaLog>& media_log);
89 ~LogHelper();
91 std::ostream& stream() { return stream_; }
93 private:
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): " \
122 : "")
124 } // namespace media
126 #endif // MEDIA_BASE_MEDIA_LOG_H_