Home | History | Annotate | Download | only in base
      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.
      4 
      5 #ifndef MEDIA_BASE_MEDIA_LOG_H_
      6 #define MEDIA_BASE_MEDIA_LOG_H_
      7 
      8 #include <sstream>
      9 #include <string>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "media/base/media_export.h"
     14 #include "media/base/media_log_event.h"
     15 #include "media/base/pipeline.h"
     16 #include "media/base/pipeline_status.h"
     17 
     18 namespace media {
     19 
     20 // Indicates a string should be added to the log.
     21 // First parameter - The string to add to the log.
     22 typedef base::Callback<void(const std::string&)> LogCB;
     23 
     24 // Helper class to make it easier to use log_cb like DVLOG().
     25 class LogHelper {
     26  public:
     27   LogHelper(const LogCB& Log_cb);
     28   ~LogHelper();
     29 
     30   std::ostream& stream() { return stream_; }
     31 
     32  private:
     33   LogCB log_cb_;
     34   std::stringstream stream_;
     35 };
     36 
     37 #define MEDIA_LOG(log_cb) LogHelper(log_cb).stream()
     38 
     39 class MEDIA_EXPORT MediaLog : public base::RefCountedThreadSafe<MediaLog> {
     40  public:
     41   // Convert various enums to strings.
     42   static const char* EventTypeToString(MediaLogEvent::Type type);
     43   static const char* PipelineStatusToString(PipelineStatus);
     44 
     45   MediaLog();
     46 
     47   // Add an event to this log. Overriden by inheritors to actually do something
     48   // with it.
     49   virtual void AddEvent(scoped_ptr<MediaLogEvent> event);
     50 
     51   // Helper methods to create events and their parameters.
     52   scoped_ptr<MediaLogEvent> CreateEvent(MediaLogEvent::Type type);
     53   scoped_ptr<MediaLogEvent> CreateBooleanEvent(
     54       MediaLogEvent::Type type, const char* property, bool value);
     55   scoped_ptr<MediaLogEvent> CreateStringEvent(
     56       MediaLogEvent::Type type, const char* property, const std::string& value);
     57   scoped_ptr<MediaLogEvent> CreateTimeEvent(
     58       MediaLogEvent::Type type, const char* property, base::TimeDelta value);
     59   scoped_ptr<MediaLogEvent> CreateLoadEvent(const std::string& url);
     60   scoped_ptr<MediaLogEvent> CreateSeekEvent(float seconds);
     61   scoped_ptr<MediaLogEvent> CreatePipelineStateChangedEvent(
     62       Pipeline::State state);
     63   scoped_ptr<MediaLogEvent> CreatePipelineErrorEvent(PipelineStatus error);
     64   scoped_ptr<MediaLogEvent> CreateVideoSizeSetEvent(
     65       size_t width, size_t height);
     66   scoped_ptr<MediaLogEvent> CreateBufferedExtentsChangedEvent(
     67       int64 start, int64 current, int64 end);
     68   scoped_ptr<MediaLogEvent> CreateMediaSourceErrorEvent(
     69       const std::string& error);
     70 
     71   // Report a property change without an accompanying event.
     72   void SetStringProperty(const char* key, const std::string& value);
     73   void SetIntegerProperty(const char* key, int value);
     74   void SetDoubleProperty(const char* key, double value);
     75   void SetBooleanProperty(const char* key, bool value);
     76   void SetTimeProperty(const char* key, base::TimeDelta value);
     77 
     78  protected:
     79   friend class base::RefCountedThreadSafe<MediaLog>;
     80   virtual ~MediaLog();
     81 
     82  private:
     83   // A unique (to this process) id for this MediaLog.
     84   int32 id_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(MediaLog);
     87 };
     88 
     89 }  // namespace media
     90 
     91 #endif  // MEDIA_BASE_MEDIA_LOG_H_
     92