2013-10-29 21:45:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* Original author: bcampen@mozilla.com */
|
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
#include "rlogconnector.h"
|
2013-10-29 21:45:09 +04:00
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
2016-10-11 10:17:26 +03:00
|
|
|
#include "logging.h"
|
2013-10-29 22:53:44 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-10-29 21:45:09 +04:00
|
|
|
#include "mozilla/Move.h" // Pinch hitting for <utility> and std::move
|
2013-11-05 03:39:51 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2016-10-04 18:57:51 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2013-10-29 21:45:09 +04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <csi_platform.h>
|
|
|
|
#include "r_log.h"
|
2016-01-26 19:08:25 +03:00
|
|
|
#include "registry.h"
|
2013-10-29 21:45:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Matches r_dest_vlog type defined in r_log.h */
|
|
|
|
static int ringbuffer_vlog(int facility, int level, const char* format,
|
|
|
|
va_list ap) {
|
2019-06-22 00:09:22 +03:00
|
|
|
if (mozilla::RLogConnector::GetInstance()->ShouldLog(level)) {
|
|
|
|
// I could be evil and printf right into a std::string, but unless this
|
|
|
|
// shows up in profiling, it is not worth doing.
|
|
|
|
char temp[4096];
|
|
|
|
VsprintfLiteral(temp, format, ap);
|
2013-10-29 21:45:09 +04:00
|
|
|
|
2019-06-22 00:09:22 +03:00
|
|
|
mozilla::RLogConnector::GetInstance()->Log(level, std::string(temp));
|
|
|
|
}
|
2013-10-29 21:45:09 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
static mozilla::LogLevel rLogLvlToMozLogLvl(int level) {
|
|
|
|
switch (level) {
|
|
|
|
case LOG_EMERG:
|
|
|
|
case LOG_ALERT:
|
|
|
|
case LOG_CRIT:
|
|
|
|
case LOG_ERR:
|
|
|
|
return mozilla::LogLevel::Error;
|
|
|
|
case LOG_WARNING:
|
|
|
|
return mozilla::LogLevel::Warning;
|
|
|
|
case LOG_NOTICE:
|
|
|
|
return mozilla::LogLevel::Info;
|
|
|
|
case LOG_INFO:
|
|
|
|
return mozilla::LogLevel::Debug;
|
|
|
|
case LOG_DEBUG:
|
|
|
|
default:
|
|
|
|
return mozilla::LogLevel::Verbose;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MTLOG_MODULE("nicer");
|
|
|
|
|
2013-10-29 21:45:09 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
RLogConnector* RLogConnector::instance;
|
2013-10-29 21:45:09 +04:00
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
RLogConnector::RLogConnector()
|
|
|
|
: log_limit_(4096), mutex_("RLogConnector::mutex_"), disableCount_(0) {}
|
2013-10-29 21:45:09 +04:00
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
RLogConnector::~RLogConnector() {}
|
2013-10-29 21:45:09 +04:00
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::SetLogLimit(uint32_t new_limit) {
|
2013-11-05 03:39:51 +04:00
|
|
|
OffTheBooksMutexAutoLock lock(mutex_);
|
2013-10-29 21:45:09 +04:00
|
|
|
log_limit_ = new_limit;
|
|
|
|
RemoveOld();
|
|
|
|
}
|
|
|
|
|
2019-06-22 00:09:22 +03:00
|
|
|
bool RLogConnector::ShouldLog(int level) const {
|
|
|
|
return level <= LOG_INFO ||
|
|
|
|
MOZ_LOG_TEST(getLogModule(), rLogLvlToMozLogLvl(level));
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::Log(int level, std::string&& log) {
|
|
|
|
MOZ_MTLOG(rLogLvlToMozLogLvl(level), log);
|
2019-06-22 00:09:22 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mutex_);
|
|
|
|
if (disableCount_ == 0) {
|
|
|
|
AddMsg(std::move(log));
|
2016-01-26 19:08:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::AddMsg(std::string&& msg) {
|
2018-05-30 22:15:35 +03:00
|
|
|
log_messages_.push_front(std::move(msg));
|
2013-10-29 21:45:09 +04:00
|
|
|
RemoveOld();
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
inline void RLogConnector::RemoveOld() {
|
2013-10-29 21:45:09 +04:00
|
|
|
if (log_messages_.size() > log_limit_) {
|
|
|
|
log_messages_.resize(log_limit_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
RLogConnector* RLogConnector::CreateInstance() {
|
2013-10-29 21:45:09 +04:00
|
|
|
if (!instance) {
|
2016-10-11 10:17:26 +03:00
|
|
|
instance = new RLogConnector;
|
2016-01-26 19:08:25 +03:00
|
|
|
NR_reg_init(NR_REG_MODE_LOCAL);
|
2016-10-11 10:17:26 +03:00
|
|
|
r_log_set_extra_destination(LOG_DEBUG, &ringbuffer_vlog);
|
2013-10-29 21:45:09 +04:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
RLogConnector* RLogConnector::GetInstance() { return instance; }
|
2013-10-29 21:45:09 +04:00
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::DestroyInstance() {
|
2013-10-29 21:45:09 +04:00
|
|
|
// First param is ignored when passing null
|
2016-10-11 10:17:26 +03:00
|
|
|
r_log_set_extra_destination(LOG_DEBUG, nullptr);
|
2013-10-29 21:45:09 +04:00
|
|
|
delete instance;
|
|
|
|
instance = nullptr;
|
|
|
|
}
|
2016-01-28 05:09:19 +03:00
|
|
|
|
2016-01-26 19:08:25 +03:00
|
|
|
// As long as at least one PeerConnection exists in a Private Window rlog
|
2016-10-11 10:17:26 +03:00
|
|
|
// messages will not be saved in the RLogConnector. This is necessary because
|
2016-01-26 19:08:25 +03:00
|
|
|
// the log_messages buffer is shared across all instances of
|
|
|
|
// PeerConnectionImpls. There is no way with the current structure of r_log to
|
|
|
|
// run separate logs.
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::EnterPrivateMode() {
|
2016-01-26 19:08:25 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mutex_);
|
|
|
|
++disableCount_;
|
|
|
|
MOZ_ASSERT(disableCount_ != 0);
|
|
|
|
|
|
|
|
if (disableCount_ == 1) {
|
|
|
|
AddMsg("LOGGING SUSPENDED: a connection is active in a Private Window ***");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::ExitPrivateMode() {
|
2016-01-26 19:08:25 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mutex_);
|
|
|
|
MOZ_ASSERT(disableCount_ != 0);
|
|
|
|
|
|
|
|
if (--disableCount_ == 0) {
|
|
|
|
AddMsg(
|
|
|
|
"LOGGING RESUMED: no connections are active in a Private Window ***");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::Clear() {
|
2016-01-28 03:05:10 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mutex_);
|
|
|
|
log_messages_.clear();
|
|
|
|
}
|
2013-10-29 21:45:09 +04:00
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::Filter(const std::string& substring, uint32_t limit,
|
2013-10-29 21:45:09 +04:00
|
|
|
std::deque<std::string>* matching_logs) {
|
|
|
|
std::vector<std::string> substrings;
|
|
|
|
substrings.push_back(substring);
|
|
|
|
FilterAny(substrings, limit, matching_logs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool AnySubstringMatches(const std::vector<std::string>& substrings,
|
|
|
|
const std::string& string) {
|
|
|
|
for (auto sub = substrings.begin(); sub != substrings.end(); ++sub) {
|
|
|
|
if (string.find(*sub) != std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-11 10:17:26 +03:00
|
|
|
void RLogConnector::FilterAny(const std::vector<std::string>& substrings,
|
2013-10-29 21:45:09 +04:00
|
|
|
uint32_t limit,
|
|
|
|
std::deque<std::string>* matching_logs) {
|
2013-11-05 03:39:51 +04:00
|
|
|
OffTheBooksMutexAutoLock lock(mutex_);
|
2013-10-29 21:45:09 +04:00
|
|
|
if (limit == 0) {
|
|
|
|
// At a max, all of the log messages.
|
|
|
|
limit = log_limit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto log = log_messages_.begin();
|
|
|
|
log != log_messages_.end() && matching_logs->size() < limit; ++log) {
|
|
|
|
if (AnySubstringMatches(substrings, *log)) {
|
|
|
|
matching_logs->push_front(*log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|