nsConsoleService.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Maintains a circular buffer of recent messages, and notifies
* listeners when new messages are logged.
*/
/* Threadsafe. */
#include "nsMemory.h"
#include "nsIServiceManager.h"
#include "nsIProxyObjectManager.h"
#include "nsSupportsArray.h"
#include "nsConsoleService.h"
#include "nsConsoleMessage.h"
{
// XXX grab this from a pref!
// hm, but worry about circularity, bc we want to be able to report
// prefs errs...
mBufferSize = 250;
// XXX deal with these two allocations by detecting null mLock in factory?
mMessages = (nsIConsoleMessage **)
mLock = PR_NewLock();
// Array elements should be 0 initially for circular buffer algorithm.
for (PRUint32 i = 0; i < mBufferSize; i++) {
}
}
{
PRUint32 i = 0;
NS_RELEASE(mMessages[i]);
i++;
}
#ifdef DEBUG_mccabe
if (mListeners.Count() != 0) {
"WARNING - %d console error listeners still registered!\n"
"More calls to nsIConsoleService::UnregisterListener needed.\n",
mListeners.Count());
}
#endif
if (mLock)
}
{
// Copy each element into the temporary nsSupportsArray...
return PR_TRUE;
}
// nsIConsoleService methods
{
return NS_ERROR_INVALID_ARG;
/*
* Lock while updating buffer, and while taking snapshot of
* listeners array.
*/
{
/*
* 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.
*/
if (mCurrent == mBufferSize) {
mCurrent = 0; // wrap around.
}
/*
* Copy the listeners into the snapshot array - in case a listener
* is removed during an Observe(...) notification...
*/
}
if (retiredMessage != nsnull)
/*
* Iterate through any registered listeners and tell them about
* the message. We use the mListening flag to guard against
* recursive message logs. This could sometimes result in
* listeners being skipped because of activity on other threads,
* when we only care about the recursive case.
*/
return rv;
{
if (mListening)
return NS_OK;
}
returned_rv = NS_OK;
for (PRUint32 i = 0; i < snapshotCount; i++) {
returned_rv = rv;
break; // fall thru to mListening restore code below.
}
}
{
}
return returned_rv;
}
{
return this->LogMessage(msg);
}
{
/*
* Lock the whole method, as we don't want anyone mucking with mCurrent or
* mFull while we're copying out the buffer.
*/
/*
* 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.
*/
messageArray = (nsIConsoleMessage **)
*messageArray = nsnull;
*messages = messageArray;
*count = 0;
return NS_OK;
}
* resultSize);
if (messageArray == nsnull) {
*count = 0;
return NS_ERROR_FAILURE;
}
PRUint32 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.
NS_ADDREF(messageArray[i]);
}
} else {
for (i = 0; i < mCurrent; i++) {
messageArray[i] = mMessages[i];
NS_ADDREF(messageArray[i]);
}
}
*count = resultSize;
*messages = messageArray;
return NS_OK;
}
/*
* Store a threadsafe proxy to the listener rather than the
* listener itself; we want the console service to be callable
* from any thread, but listeners can be implemented in
* thread-specific ways, and we always want to call them on their
* originating thread. JavaScript is the motivating example.
*/
return rv;
{
/*
* Put the proxy event listener into a hashtable using the *real*
* listener as the key.
*
* This is necessary because proxy objects do *not* maintain
* nsISupports identity. Therefore, since GetProxyForListener(...)
* can return different proxies for the same object (see bug #85831)
* we need to use the real object as the unique key...
*/
}
return NS_OK;
}
return NS_OK;
}
{
if (proxyManager == nsnull)
return NS_ERROR_NOT_AVAILABLE;
/*
* NOTE this will fail if the calling thread doesn't have an eventQ.
*
* Would it be better to catch that case and leave the listener unproxied?
*/
(void**) aProxy);
return rv;
}