media/mtransport/rlogringbuffer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/rlogringbuffer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.9 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +/* Original author: bcampen@mozilla.com */
    1.12 +
    1.13 +#include <cstdarg>
    1.14 +
    1.15 +#include "rlogringbuffer.h"
    1.16 +
    1.17 +#include <deque>
    1.18 +#include <string>
    1.19 +#include "mozilla/Assertions.h"
    1.20 +#include "mozilla/Move.h" // Pinch hitting for <utility> and std::move
    1.21 +#include "mozilla/Mutex.h"
    1.22 +#include "mozilla/NullPtr.h"
    1.23 +#include <vector>
    1.24 +
    1.25 +extern "C" {
    1.26 +#include <csi_platform.h>
    1.27 +#include "r_log.h"
    1.28 +}
    1.29 +
    1.30 +/* Matches r_dest_vlog type defined in r_log.h */
    1.31 +static int ringbuffer_vlog(int facility,
    1.32 +                           int level,
    1.33 +                           const char *format,
    1.34 +                           va_list ap) {
    1.35 +  MOZ_ASSERT(mozilla::RLogRingBuffer::GetInstance());
    1.36 +  // I could be evil and printf right into a std::string, but unless this
    1.37 +  // shows up in profiling, it is not worth doing.
    1.38 +  char temp[4096];
    1.39 +  vsnprintf(temp, sizeof(temp), format, ap);
    1.40 +
    1.41 +  mozilla::RLogRingBuffer::GetInstance()->Log(std::string(temp));
    1.42 +  return 0;
    1.43 +}
    1.44 +
    1.45 +namespace mozilla {
    1.46 +
    1.47 +RLogRingBuffer* RLogRingBuffer::instance;
    1.48 +
    1.49 +RLogRingBuffer::RLogRingBuffer()
    1.50 +  : log_limit_(4096),
    1.51 +    mutex_("RLogRingBuffer::mutex_") {
    1.52 +}
    1.53 +
    1.54 +RLogRingBuffer::~RLogRingBuffer() {
    1.55 +}
    1.56 +
    1.57 +void RLogRingBuffer::SetLogLimit(uint32_t new_limit) {
    1.58 +  OffTheBooksMutexAutoLock lock(mutex_);
    1.59 +  log_limit_ = new_limit;
    1.60 +  RemoveOld();
    1.61 +}
    1.62 +
    1.63 +void RLogRingBuffer::Log(std::string&& log) {
    1.64 +  OffTheBooksMutexAutoLock lock(mutex_);
    1.65 +  log_messages_.push_front(Move(log));
    1.66 +  RemoveOld();
    1.67 +}
    1.68 +
    1.69 +inline void RLogRingBuffer::RemoveOld() {
    1.70 +  if (log_messages_.size() > log_limit_) {
    1.71 +    log_messages_.resize(log_limit_);
    1.72 +  }
    1.73 +}
    1.74 +
    1.75 +
    1.76 +RLogRingBuffer* RLogRingBuffer::CreateInstance() {
    1.77 +  if (!instance) {
    1.78 +    instance = new RLogRingBuffer;
    1.79 +    r_log_set_extra_destination(LOG_INFO, &ringbuffer_vlog);
    1.80 +  }
    1.81 +  return instance;
    1.82 +}
    1.83 +
    1.84 +RLogRingBuffer* RLogRingBuffer::GetInstance() {
    1.85 +  return instance;
    1.86 +}
    1.87 +
    1.88 +void RLogRingBuffer::DestroyInstance() {
    1.89 +  // First param is ignored when passing null
    1.90 +  r_log_set_extra_destination(LOG_INFO, nullptr);
    1.91 +  delete instance;
    1.92 +  instance = nullptr;
    1.93 +}
    1.94 +
    1.95 +void RLogRingBuffer::Filter(const std::string& substring,
    1.96 +                            uint32_t limit,
    1.97 +                            std::deque<std::string>* matching_logs) {
    1.98 +  std::vector<std::string> substrings;
    1.99 +  substrings.push_back(substring);
   1.100 +  FilterAny(substrings, limit, matching_logs);
   1.101 +}
   1.102 +
   1.103 +inline bool AnySubstringMatches(const std::vector<std::string>& substrings,
   1.104 +                                const std::string& string) {
   1.105 +  for (auto sub = substrings.begin(); sub != substrings.end(); ++sub) {
   1.106 +    if (string.find(*sub) != std::string::npos) {
   1.107 +      return true;
   1.108 +    }
   1.109 +  }
   1.110 +  return false;
   1.111 +}
   1.112 +
   1.113 +void RLogRingBuffer::FilterAny(const std::vector<std::string>& substrings,
   1.114 +                               uint32_t limit,
   1.115 +                               std::deque<std::string>* matching_logs) {
   1.116 +  OffTheBooksMutexAutoLock lock(mutex_);
   1.117 +  if (limit == 0) {
   1.118 +    // At a max, all of the log messages.
   1.119 +    limit = log_limit_;
   1.120 +  }
   1.121 +
   1.122 +  for (auto log = log_messages_.begin();
   1.123 +       log != log_messages_.end() && matching_logs->size() < limit;
   1.124 +       ++log) {
   1.125 +    if (AnySubstringMatches(substrings, *log)) {
   1.126 +      matching_logs->push_front(*log);
   1.127 +    }
   1.128 +  }
   1.129 +}
   1.130 +
   1.131 +} // namespace mozilla
   1.132 +

mercurial