2014-05-05 10:30:39 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Maintains a circular buffer of recent messages, and notifies
|
|
|
|
* listeners when new messages are logged.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Threadsafe. */
|
|
|
|
|
|
|
|
#include "nsMemory.h"
|
2009-04-14 01:02:58 -07:00
|
|
|
#include "nsCOMArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "nsConsoleService.h"
|
|
|
|
#include "nsConsoleMessage.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
2013-09-23 10:29:27 -07:00
|
|
|
#include "nsIConsoleListener.h"
|
2013-10-04 10:16:53 -07:00
|
|
|
#include "nsPrintfCString.h"
|
2012-11-06 22:34:33 -08:00
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-11 13:01:29 -08:00
|
|
|
#if defined(ANDROID)
|
|
|
|
#include <android/log.h>
|
2014-09-09 16:52:08 -07:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-02-11 13:01:29 -08:00
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_IMPL_ADDREF(nsConsoleService)
|
|
|
|
NS_IMPL_RELEASE(nsConsoleService)
|
2014-08-25 12:17:15 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsConsoleService, nullptr,
|
|
|
|
nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON,
|
|
|
|
NS_CONSOLESERVICE_CID)
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsConsoleService, nsIConsoleService)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsConsoleService, nsIConsoleService)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-06 22:34:33 -08:00
|
|
|
static bool sLoggingEnabled = true;
|
2013-03-13 22:55:25 -07:00
|
|
|
static bool sLoggingBuffered = true;
|
2012-11-06 22:34:33 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsConsoleService::nsConsoleService()
|
2014-05-05 10:30:39 -07:00
|
|
|
: mMessages(nullptr)
|
|
|
|
, mCurrent(0)
|
|
|
|
, mFull(false)
|
|
|
|
, mDeliveringMessage(false)
|
|
|
|
, mLock("nsConsoleService.mLock")
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
// XXX grab this from a pref!
|
|
|
|
// hm, but worry about circularity, bc we want to be able to report
|
|
|
|
// prefs errs...
|
|
|
|
mBufferSize = 250;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsConsoleService::~nsConsoleService()
|
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
uint32_t i = 0;
|
2014-08-25 12:17:15 -07:00
|
|
|
while (i < mBufferSize && mMessages[i]) {
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_RELEASE(mMessages[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
if (mMessages) {
|
2014-05-05 10:30:39 -07:00
|
|
|
nsMemory::Free(mMessages);
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-03-13 22:55:25 -07:00
|
|
|
class AddConsolePrefWatchers : public nsRunnable
|
2012-11-06 22:34:33 -08:00
|
|
|
{
|
|
|
|
public:
|
2014-07-28 10:19:06 -07:00
|
|
|
explicit AddConsolePrefWatchers(nsConsoleService* aConsole) : mConsole(aConsole)
|
2014-05-13 10:41:38 -07:00
|
|
|
{
|
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
Preferences::AddBoolVarCache(&sLoggingEnabled, "consoleservice.enabled", true);
|
|
|
|
Preferences::AddBoolVarCache(&sLoggingBuffered, "consoleservice.buffered", true);
|
|
|
|
if (!sLoggingBuffered) {
|
|
|
|
mConsole->Reset();
|
2012-11-06 22:34:33 -08:00
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-13 22:55:25 -07:00
|
|
|
|
|
|
|
private:
|
2014-05-05 10:30:39 -07:00
|
|
|
nsRefPtr<nsConsoleService> mConsole;
|
2012-11-06 22:34:33 -08:00
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsConsoleService::Init()
|
|
|
|
{
|
2014-05-13 10:41:38 -07:00
|
|
|
mMessages = (nsIConsoleMessage**)
|
|
|
|
nsMemory::Alloc(mBufferSize * sizeof(nsIConsoleMessage*));
|
|
|
|
if (!mMessages) {
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
// Array elements should be 0 initially for circular buffer algorithm.
|
2014-05-13 10:41:38 -07:00
|
|
|
memset(mMessages, 0, mBufferSize * sizeof(nsIConsoleMessage*));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_DispatchToMainThread(new AddConsolePrefWatchers(this));
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 08:28:21 -08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class LogMessageRunnable : public nsRunnable
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-11 08:28:21 -08:00
|
|
|
public:
|
2014-05-13 10:41:38 -07:00
|
|
|
LogMessageRunnable(nsIConsoleMessage* aMessage, nsConsoleService* aService)
|
|
|
|
: mMessage(aMessage)
|
|
|
|
, mService(aService)
|
2014-05-05 10:30:39 -07:00
|
|
|
{ }
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2012-01-11 08:28:21 -08:00
|
|
|
|
|
|
|
private:
|
2014-05-05 10:30:39 -07:00
|
|
|
nsCOMPtr<nsIConsoleMessage> mMessage;
|
|
|
|
nsRefPtr<nsConsoleService> mService;
|
2012-01-11 08:28:21 -08:00
|
|
|
};
|
|
|
|
|
2013-01-29 08:02:56 -08:00
|
|
|
typedef nsCOMArray<nsIConsoleListener> ListenerArrayType;
|
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
CollectCurrentListeners(nsISupports* aKey, nsIConsoleListener* aValue,
|
2014-05-13 10:41:38 -07:00
|
|
|
void* aClosure)
|
2013-01-29 08:02:56 -08:00
|
|
|
{
|
2014-05-13 10:41:38 -07:00
|
|
|
ListenerArrayType* listeners = static_cast<ListenerArrayType*>(aClosure);
|
2014-05-05 10:30:39 -07:00
|
|
|
listeners->AppendObject(aValue);
|
|
|
|
return PL_DHASH_NEXT;
|
2013-01-29 08:02:56 -08:00
|
|
|
}
|
|
|
|
|
2012-01-11 08:28:21 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
LogMessageRunnable::Run()
|
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
// Snapshot of listeners so that we don't reenter this hash during
|
|
|
|
// enumeration.
|
|
|
|
nsCOMArray<nsIConsoleListener> listeners;
|
|
|
|
mService->EnumerateListeners(CollectCurrentListeners, &listeners);
|
2013-01-29 08:02:56 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
mService->SetIsDelivering();
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
for (int32_t i = 0; i < listeners.Count(); ++i) {
|
2014-05-05 10:30:39 -07:00
|
|
|
listeners[i]->Observe(mMessage);
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
mService->SetDoneDelivering();
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
2012-01-11 08:28:21 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-11 08:28:21 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIConsoleService methods
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 10:41:38 -07:00
|
|
|
nsConsoleService::LogMessage(nsIConsoleMessage* aMessage)
|
2012-11-09 09:52:09 -08:00
|
|
|
{
|
2014-05-13 10:41:38 -07:00
|
|
|
return LogMessageWithMode(aMessage, OutputToLog);
|
2012-11-09 09:52:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-13 10:41:38 -07:00
|
|
|
nsConsoleService::LogMessageWithMode(nsIConsoleMessage* aMessage,
|
|
|
|
nsConsoleService::OutputMode aOutputMode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-13 10:41:38 -07:00
|
|
|
if (!aMessage) {
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
if (!sLoggingEnabled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread() && mDeliveringMessage) {
|
|
|
|
nsCString msg;
|
2014-05-13 10:41:38 -07:00
|
|
|
aMessage->ToString(msg);
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_WARNING(nsPrintfCString("Reentrancy error: some client attempted "
|
|
|
|
"to display a message to the console while in a console listener. "
|
|
|
|
"The following message was discarded: \"%s\"", msg.get()).get());
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<LogMessageRunnable> r;
|
2014-05-13 10:41:38 -07:00
|
|
|
nsIConsoleMessage* retiredMessage;
|
2014-05-05 10:30:39 -07:00
|
|
|
|
|
|
|
if (sLoggingBuffered) {
|
2014-05-13 10:41:38 -07:00
|
|
|
NS_ADDREF(aMessage); // early, in case it's same as replaced below.
|
2014-05-05 10:30:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock while updating buffer, and while taking snapshot of
|
|
|
|
* listeners array.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2012-11-06 22:34:33 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
#if defined(ANDROID)
|
2014-05-13 10:41:38 -07:00
|
|
|
if (aOutputMode == OutputToLog) {
|
2014-05-05 10:30:39 -07:00
|
|
|
nsCString msg;
|
2014-05-13 10:41:38 -07:00
|
|
|
aMessage->ToString(msg);
|
2014-09-09 16:52:08 -07:00
|
|
|
|
|
|
|
/** Attempt to use the process name as the log tag. */
|
|
|
|
mozilla::dom::ContentChild* child =
|
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
nsCString appName;
|
|
|
|
if (child) {
|
|
|
|
child->GetProcessName(appName);
|
|
|
|
} else {
|
|
|
|
appName = "GeckoConsole";
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:33:36 -07:00
|
|
|
uint32_t logLevel = 0;
|
|
|
|
aMessage->GetLogLevel(&logLevel);
|
|
|
|
|
|
|
|
android_LogPriority logPriority = ANDROID_LOG_INFO;
|
|
|
|
switch (logLevel) {
|
|
|
|
case nsIConsoleMessage::debug:
|
|
|
|
logPriority = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case nsIConsoleMessage::info:
|
|
|
|
logPriority = ANDROID_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case nsIConsoleMessage::warn:
|
|
|
|
logPriority = ANDROID_LOG_WARN;
|
|
|
|
break;
|
|
|
|
case nsIConsoleMessage::error:
|
|
|
|
logPriority = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
__android_log_print(logPriority, appName.get(), "%s", msg.get());
|
2012-01-11 08:28:21 -08:00
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (IsDebuggerPresent()) {
|
|
|
|
nsString msg;
|
2014-05-13 10:41:38 -07:00
|
|
|
aMessage->GetMessageMoz(getter_Copies(msg));
|
2014-05-21 20:48:51 -07:00
|
|
|
msg.Append('\n');
|
2014-05-05 10:30:39 -07:00
|
|
|
OutputDebugStringW(msg.get());
|
2013-03-13 22:55:25 -07:00
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2014-05-05 10:30:39 -07:00
|
|
|
* If there's already a message in the slot we're about to replace,
|
|
|
|
* we've wrapped around, and we need to release the old message. We
|
|
|
|
* save a pointer to it, so we can release below outside the lock.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2014-05-05 10:30:39 -07:00
|
|
|
retiredMessage = mMessages[mCurrent];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
if (sLoggingBuffered) {
|
2014-05-13 10:41:38 -07:00
|
|
|
mMessages[mCurrent++] = aMessage;
|
2014-05-05 10:30:39 -07:00
|
|
|
if (mCurrent == mBufferSize) {
|
|
|
|
mCurrent = 0; // wrap around.
|
|
|
|
mFull = true;
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 19:12:20 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
if (mListeners.Count() > 0) {
|
2014-05-13 10:41:38 -07:00
|
|
|
r = new LogMessageRunnable(aMessage, this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
}
|
2012-09-28 09:54:36 -07:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
if (retiredMessage) {
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_RELEASE(retiredMessage);
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
if (r) {
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_DispatchToMainThread(r);
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-29 08:02:56 -08:00
|
|
|
void
|
|
|
|
nsConsoleService::EnumerateListeners(ListenerHash::EnumReadFunction aFunction,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mListeners.EnumerateRead(aFunction, aClosure);
|
2013-01-29 08:02:56 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-13 10:41:38 -07:00
|
|
|
nsConsoleService::LogStringMessage(const char16_t* aMessage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
if (!sLoggingEnabled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-06 22:34:33 -08:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
nsRefPtr<nsConsoleMessage> msg(new nsConsoleMessage(aMessage));
|
2014-05-05 10:30:39 -07:00
|
|
|
return this->LogMessage(msg);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-25 12:17:15 -07:00
|
|
|
nsConsoleService::GetMessageArray(uint32_t* aCount,
|
|
|
|
nsIConsoleMessage*** aMessages)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-13 10:41:38 -07:00
|
|
|
nsIConsoleMessage** messageArray;
|
2014-05-05 10:30:39 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the whole method, as we don't want anyone mucking with mCurrent or
|
|
|
|
* mFull while we're copying out the buffer.
|
|
|
|
*/
|
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
if (mCurrent == 0 && !mFull) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2014-05-05 10:30:39 -07:00
|
|
|
* Make a 1-length output array so that nobody gets confused,
|
|
|
|
* and return a count of 0. This should result in a 0-length
|
|
|
|
* array object when called from script.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2014-05-13 10:41:38 -07:00
|
|
|
messageArray = (nsIConsoleMessage**)
|
|
|
|
nsMemory::Alloc(sizeof(nsIConsoleMessage*));
|
2014-05-05 10:30:39 -07:00
|
|
|
*messageArray = nullptr;
|
2014-05-13 10:41:38 -07:00
|
|
|
*aMessages = messageArray;
|
|
|
|
*aCount = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t resultSize = mFull ? mBufferSize : mCurrent;
|
|
|
|
messageArray =
|
2014-05-13 10:41:38 -07:00
|
|
|
(nsIConsoleMessage**)nsMemory::Alloc((sizeof(nsIConsoleMessage*))
|
|
|
|
* resultSize);
|
2014-05-05 10:30:39 -07:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
if (!messageArray) {
|
|
|
|
*aMessages = nullptr;
|
|
|
|
*aCount = 0;
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
if (mFull) {
|
|
|
|
for (i = 0; i < mBufferSize; i++) {
|
|
|
|
// if full, fill the buffer starting from mCurrent (which'll be
|
|
|
|
// oldest) wrapping around the buffer to the most recent.
|
|
|
|
messageArray[i] = mMessages[(mCurrent + i) % mBufferSize];
|
|
|
|
NS_ADDREF(messageArray[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < mCurrent; i++) {
|
|
|
|
messageArray[i] = mMessages[i];
|
|
|
|
NS_ADDREF(messageArray[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:39 -07:00
|
|
|
}
|
2014-05-13 10:41:38 -07:00
|
|
|
*aCount = resultSize;
|
|
|
|
*aMessages = messageArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 10:41:38 -07:00
|
|
|
nsConsoleService::RegisterListener(nsIConsoleListener* aListener)
|
2012-01-11 08:28:21 -08:00
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsConsoleService::RegisterListener is main thread only.");
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aListener);
|
2014-05-05 10:30:39 -07:00
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mListeners.GetWeak(canonical)) {
|
|
|
|
// Reregistering a listener isn't good
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-05-13 10:41:38 -07:00
|
|
|
mListeners.Put(canonical, aListener);
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 10:41:38 -07:00
|
|
|
nsConsoleService::UnregisterListener(nsIConsoleListener* aListener)
|
2012-01-11 08:28:21 -08:00
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsConsoleService::UnregisterListener is main thread only.");
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aListener);
|
2012-01-11 08:28:21 -08:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
if (!mListeners.GetWeak(canonical)) {
|
|
|
|
// Unregistering a listener that was never registered?
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mListeners.Remove(canonical);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConsoleService::Reset()
|
|
|
|
{
|
2014-05-05 10:30:39 -07:00
|
|
|
/*
|
|
|
|
* Make sure nobody trips into the buffer while it's being reset
|
|
|
|
*/
|
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
mCurrent = 0;
|
|
|
|
mFull = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
/*
|
|
|
|
* Free all messages stored so far (cf. destructor)
|
|
|
|
*/
|
2014-05-13 10:41:38 -07:00
|
|
|
for (uint32_t i = 0; i < mBufferSize && mMessages[i]; i++) {
|
2014-05-05 10:30:39 -07:00
|
|
|
NS_RELEASE(mMessages[i]);
|
2014-05-13 10:41:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|