media/mtransport/rlogringbuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 6 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 /* Original author: bcampen@mozilla.com */
michael@0 9
michael@0 10 #include <cstdarg>
michael@0 11
michael@0 12 #include "rlogringbuffer.h"
michael@0 13
michael@0 14 #include <deque>
michael@0 15 #include <string>
michael@0 16 #include "mozilla/Assertions.h"
michael@0 17 #include "mozilla/Move.h" // Pinch hitting for <utility> and std::move
michael@0 18 #include "mozilla/Mutex.h"
michael@0 19 #include "mozilla/NullPtr.h"
michael@0 20 #include <vector>
michael@0 21
michael@0 22 extern "C" {
michael@0 23 #include <csi_platform.h>
michael@0 24 #include "r_log.h"
michael@0 25 }
michael@0 26
michael@0 27 /* Matches r_dest_vlog type defined in r_log.h */
michael@0 28 static int ringbuffer_vlog(int facility,
michael@0 29 int level,
michael@0 30 const char *format,
michael@0 31 va_list ap) {
michael@0 32 MOZ_ASSERT(mozilla::RLogRingBuffer::GetInstance());
michael@0 33 // I could be evil and printf right into a std::string, but unless this
michael@0 34 // shows up in profiling, it is not worth doing.
michael@0 35 char temp[4096];
michael@0 36 vsnprintf(temp, sizeof(temp), format, ap);
michael@0 37
michael@0 38 mozilla::RLogRingBuffer::GetInstance()->Log(std::string(temp));
michael@0 39 return 0;
michael@0 40 }
michael@0 41
michael@0 42 namespace mozilla {
michael@0 43
michael@0 44 RLogRingBuffer* RLogRingBuffer::instance;
michael@0 45
michael@0 46 RLogRingBuffer::RLogRingBuffer()
michael@0 47 : log_limit_(4096),
michael@0 48 mutex_("RLogRingBuffer::mutex_") {
michael@0 49 }
michael@0 50
michael@0 51 RLogRingBuffer::~RLogRingBuffer() {
michael@0 52 }
michael@0 53
michael@0 54 void RLogRingBuffer::SetLogLimit(uint32_t new_limit) {
michael@0 55 OffTheBooksMutexAutoLock lock(mutex_);
michael@0 56 log_limit_ = new_limit;
michael@0 57 RemoveOld();
michael@0 58 }
michael@0 59
michael@0 60 void RLogRingBuffer::Log(std::string&& log) {
michael@0 61 OffTheBooksMutexAutoLock lock(mutex_);
michael@0 62 log_messages_.push_front(Move(log));
michael@0 63 RemoveOld();
michael@0 64 }
michael@0 65
michael@0 66 inline void RLogRingBuffer::RemoveOld() {
michael@0 67 if (log_messages_.size() > log_limit_) {
michael@0 68 log_messages_.resize(log_limit_);
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72
michael@0 73 RLogRingBuffer* RLogRingBuffer::CreateInstance() {
michael@0 74 if (!instance) {
michael@0 75 instance = new RLogRingBuffer;
michael@0 76 r_log_set_extra_destination(LOG_INFO, &ringbuffer_vlog);
michael@0 77 }
michael@0 78 return instance;
michael@0 79 }
michael@0 80
michael@0 81 RLogRingBuffer* RLogRingBuffer::GetInstance() {
michael@0 82 return instance;
michael@0 83 }
michael@0 84
michael@0 85 void RLogRingBuffer::DestroyInstance() {
michael@0 86 // First param is ignored when passing null
michael@0 87 r_log_set_extra_destination(LOG_INFO, nullptr);
michael@0 88 delete instance;
michael@0 89 instance = nullptr;
michael@0 90 }
michael@0 91
michael@0 92 void RLogRingBuffer::Filter(const std::string& substring,
michael@0 93 uint32_t limit,
michael@0 94 std::deque<std::string>* matching_logs) {
michael@0 95 std::vector<std::string> substrings;
michael@0 96 substrings.push_back(substring);
michael@0 97 FilterAny(substrings, limit, matching_logs);
michael@0 98 }
michael@0 99
michael@0 100 inline bool AnySubstringMatches(const std::vector<std::string>& substrings,
michael@0 101 const std::string& string) {
michael@0 102 for (auto sub = substrings.begin(); sub != substrings.end(); ++sub) {
michael@0 103 if (string.find(*sub) != std::string::npos) {
michael@0 104 return true;
michael@0 105 }
michael@0 106 }
michael@0 107 return false;
michael@0 108 }
michael@0 109
michael@0 110 void RLogRingBuffer::FilterAny(const std::vector<std::string>& substrings,
michael@0 111 uint32_t limit,
michael@0 112 std::deque<std::string>* matching_logs) {
michael@0 113 OffTheBooksMutexAutoLock lock(mutex_);
michael@0 114 if (limit == 0) {
michael@0 115 // At a max, all of the log messages.
michael@0 116 limit = log_limit_;
michael@0 117 }
michael@0 118
michael@0 119 for (auto log = log_messages_.begin();
michael@0 120 log != log_messages_.end() && matching_logs->size() < limit;
michael@0 121 ++log) {
michael@0 122 if (AnySubstringMatches(substrings, *log)) {
michael@0 123 matching_logs->push_front(*log);
michael@0 124 }
michael@0 125 }
michael@0 126 }
michael@0 127
michael@0 128 } // namespace mozilla
michael@0 129

mercurial