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