michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Original author: bcampen@mozilla.com */ michael@0: michael@0: #include michael@0: michael@0: #include "rlogringbuffer.h" michael@0: michael@0: #include michael@0: #include michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Move.h" // Pinch hitting for and std::move michael@0: #include "mozilla/Mutex.h" michael@0: #include "mozilla/NullPtr.h" michael@0: #include michael@0: michael@0: extern "C" { michael@0: #include michael@0: #include "r_log.h" michael@0: } michael@0: michael@0: /* Matches r_dest_vlog type defined in r_log.h */ michael@0: static int ringbuffer_vlog(int facility, michael@0: int level, michael@0: const char *format, michael@0: va_list ap) { michael@0: MOZ_ASSERT(mozilla::RLogRingBuffer::GetInstance()); michael@0: // I could be evil and printf right into a std::string, but unless this michael@0: // shows up in profiling, it is not worth doing. michael@0: char temp[4096]; michael@0: vsnprintf(temp, sizeof(temp), format, ap); michael@0: michael@0: mozilla::RLogRingBuffer::GetInstance()->Log(std::string(temp)); michael@0: return 0; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: michael@0: RLogRingBuffer* RLogRingBuffer::instance; michael@0: michael@0: RLogRingBuffer::RLogRingBuffer() michael@0: : log_limit_(4096), michael@0: mutex_("RLogRingBuffer::mutex_") { michael@0: } michael@0: michael@0: RLogRingBuffer::~RLogRingBuffer() { michael@0: } michael@0: michael@0: void RLogRingBuffer::SetLogLimit(uint32_t new_limit) { michael@0: OffTheBooksMutexAutoLock lock(mutex_); michael@0: log_limit_ = new_limit; michael@0: RemoveOld(); michael@0: } michael@0: michael@0: void RLogRingBuffer::Log(std::string&& log) { michael@0: OffTheBooksMutexAutoLock lock(mutex_); michael@0: log_messages_.push_front(Move(log)); michael@0: RemoveOld(); michael@0: } michael@0: michael@0: inline void RLogRingBuffer::RemoveOld() { michael@0: if (log_messages_.size() > log_limit_) { michael@0: log_messages_.resize(log_limit_); michael@0: } michael@0: } michael@0: michael@0: michael@0: RLogRingBuffer* RLogRingBuffer::CreateInstance() { michael@0: if (!instance) { michael@0: instance = new RLogRingBuffer; michael@0: r_log_set_extra_destination(LOG_INFO, &ringbuffer_vlog); michael@0: } michael@0: return instance; michael@0: } michael@0: michael@0: RLogRingBuffer* RLogRingBuffer::GetInstance() { michael@0: return instance; michael@0: } michael@0: michael@0: void RLogRingBuffer::DestroyInstance() { michael@0: // First param is ignored when passing null michael@0: r_log_set_extra_destination(LOG_INFO, nullptr); michael@0: delete instance; michael@0: instance = nullptr; michael@0: } michael@0: michael@0: void RLogRingBuffer::Filter(const std::string& substring, michael@0: uint32_t limit, michael@0: std::deque* matching_logs) { michael@0: std::vector substrings; michael@0: substrings.push_back(substring); michael@0: FilterAny(substrings, limit, matching_logs); michael@0: } michael@0: michael@0: inline bool AnySubstringMatches(const std::vector& substrings, michael@0: const std::string& string) { michael@0: for (auto sub = substrings.begin(); sub != substrings.end(); ++sub) { michael@0: if (string.find(*sub) != std::string::npos) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void RLogRingBuffer::FilterAny(const std::vector& substrings, michael@0: uint32_t limit, michael@0: std::deque* matching_logs) { michael@0: OffTheBooksMutexAutoLock lock(mutex_); michael@0: if (limit == 0) { michael@0: // At a max, all of the log messages. michael@0: limit = log_limit_; michael@0: } michael@0: michael@0: for (auto log = log_messages_.begin(); michael@0: log != log_messages_.end() && matching_logs->size() < limit; michael@0: ++log) { michael@0: if (AnySubstringMatches(substrings, *log)) { michael@0: matching_logs->push_front(*log); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } // namespace mozilla michael@0: