1999-12-29 23:53:17 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape 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 http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* 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 Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsLogging.h"
|
|
|
|
|
|
|
|
#ifdef NS_ENABLE_LOGGING
|
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsSpecialSystemDirectory.h"
|
2000-10-27 22:30:16 +04:00
|
|
|
#ifdef XP_WIN
|
1999-12-29 23:53:17 +03:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef printf
|
|
|
|
#undef fprintf
|
|
|
|
|
|
|
|
static PRMonitor* gLogMonitor = nsnull;
|
|
|
|
static nsObjectHashtable* gSettings = nsnull;
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_IMPL_LOG_ENABLED(LogInfo)
|
|
|
|
#define PRINTF NS_LOG_PRINTF(LogInfo)
|
|
|
|
#define FLUSH NS_LOG_FLUSH(LogInfo)
|
1999-12-29 23:53:17 +03:00
|
|
|
|
|
|
|
NS_DEFINE_CID(kLoggingServiceCID, NS_LOGGINGSERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsLoggingService
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
static nsLoggingService* gLoggingService = nsnull;
|
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
nsLoggingService::nsLoggingService()
|
2000-10-27 10:18:39 +04:00
|
|
|
: mLogs(16),
|
|
|
|
mDefaultControlFlags(nsILog::DEFAULT_DISABLED |
|
|
|
|
nsILog::PRINT_THREAD_ID |
|
|
|
|
nsILog::PRINT_LOG_NAME)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_INIT_ISUPPORTS();
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsLoggingService::~nsLoggingService()
|
|
|
|
{
|
2000-11-01 04:41:15 +03:00
|
|
|
#ifdef DEBUG_warren
|
1999-12-29 23:53:17 +03:00
|
|
|
DescribeLogs(LogInfo);
|
2000-11-01 04:41:15 +03:00
|
|
|
#endif
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsLoggingService, nsILoggingService)
|
|
|
|
NS_IMPL_ADDREF(nsLoggingService)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsLoggingService::Release(void)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
|
|
|
NS_ASSERT_OWNINGTHREAD(nsLoggingService);
|
|
|
|
--mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, mRefCnt, "nsLoggingService");
|
|
|
|
if (mRefCnt == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
NS_DELETEXPCOM(this);
|
|
|
|
|
|
|
|
// special action -- null out global
|
|
|
|
gLoggingService = nsnull;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
1999-12-29 23:53:17 +03:00
|
|
|
|
2000-10-27 22:30:16 +04:00
|
|
|
static void* PR_CALLBACK
|
1999-12-29 23:53:17 +03:00
|
|
|
levelClone(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
PRUint32 level = (PRUint32)aData;
|
|
|
|
return (void*)level;
|
|
|
|
}
|
|
|
|
|
2000-10-27 22:30:16 +04:00
|
|
|
static PRBool PR_CALLBACK
|
1999-12-29 23:53:17 +03:00
|
|
|
levelDestroy(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RecordSetting(const char* name, const char* value)
|
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
PRUint32 level = 2;
|
1999-12-29 23:53:17 +03:00
|
|
|
if (nsCRT::strcasecmp(value, "ERROR") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "2") == 0) {
|
2000-10-27 10:18:39 +04:00
|
|
|
level = 2;
|
|
|
|
PRINTF("### NS_LOG: %s = ERROR\n", name);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
else if (nsCRT::strcasecmp(value, "WARN") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "WARNING") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "3") == 0) {
|
2000-10-27 10:18:39 +04:00
|
|
|
level = 3;
|
|
|
|
PRINTF("### NS_LOG: %s = WARN\n", name);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
else if (nsCRT::strcasecmp(value, "STDOUT") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "OUT") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "4") == 0) {
|
2000-10-27 10:18:39 +04:00
|
|
|
level = 4;
|
|
|
|
PRINTF("### NS_LOG: %s = STDOUT\n", name);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
else if (nsCRT::strcasecmp(value, "DBG") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "DEBUG") == 0 ||
|
|
|
|
nsCRT::strcasecmp(value, "5") == 0) {
|
2000-10-27 10:18:39 +04:00
|
|
|
level = 5;
|
|
|
|
PRINTF("### NS_LOG: %s = DBG\n", name);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-10-27 10:18:39 +04:00
|
|
|
PRINTF("### NS_LOG error: %s = %s (bad level)\n", name, value);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsCStringKey key(name);
|
1999-12-29 23:53:17 +03:00
|
|
|
gSettings->Put(&key, (void*)level);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLoggingService::Init()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink* defaultSink = nsnull;
|
1999-12-29 23:53:17 +03:00
|
|
|
const char* outputPath = nsnull;
|
|
|
|
|
|
|
|
if (gLogMonitor == nsnull) {
|
|
|
|
gLogMonitor = PR_NewMonitor();
|
|
|
|
if (gLogMonitor == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gSettings == nsnull) {
|
|
|
|
gSettings = new nsObjectHashtable(levelClone, nsnull,
|
|
|
|
levelDestroy, nsnull,
|
|
|
|
16);
|
|
|
|
if (gSettings == nsnull) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try the nspr log environment variables first:
|
|
|
|
{
|
|
|
|
const char* nspr_log_modules = getenv("NSPR_LOG_MODULES");
|
|
|
|
if (nspr_log_modules) {
|
2000-10-27 10:18:39 +04:00
|
|
|
PRINTF("### NS_LOG: using NSPR_LOG_MODULES (instead of .nslog)\n");
|
1999-12-29 23:53:17 +03:00
|
|
|
char* head = nsCRT::strdup(nspr_log_modules);
|
|
|
|
char* rest = nsCRT::strdup(nspr_log_modules);
|
|
|
|
while (1) {
|
|
|
|
char* name = nsCRT::strtok(rest, ":", &rest);
|
|
|
|
if (name == nsnull) break;
|
|
|
|
char* value = nsCRT::strtok(rest, ";", &rest);
|
|
|
|
if (value == nsnull) break;
|
|
|
|
RecordSetting(name, value);
|
|
|
|
}
|
|
|
|
nsCRT::free(head);
|
|
|
|
}
|
|
|
|
const char* nspr_log_file = getenv("NSPR_LOG_FILE");
|
|
|
|
if (nspr_log_file) {
|
2000-10-27 10:18:39 +04:00
|
|
|
PRINTF("### NS_LOG: using NSPR_LOG_FILE (instead of .nslog) -- logging to %s\n",
|
1999-12-29 23:53:17 +03:00
|
|
|
nspr_log_file);
|
|
|
|
outputPath = nspr_log_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// then load up log description file:
|
|
|
|
{
|
|
|
|
nsSpecialSystemDirectory file(nsSpecialSystemDirectory::OS_CurrentProcessDirectory);
|
|
|
|
file += ".nslog";
|
|
|
|
const char* path = file.GetNativePathCString();
|
|
|
|
FILE* f = ::fopen(path, "r");
|
|
|
|
if (f != nsnull) {
|
|
|
|
PRInt32 cnt;
|
|
|
|
while (PR_TRUE) {
|
|
|
|
char name[64];
|
|
|
|
char value[64];
|
|
|
|
// cnt = ::fscanf(f, "%64s=%64s\n", name, value);
|
|
|
|
cnt = ::fscanf(f, "%64s\n", name);
|
|
|
|
if (cnt <= 0) break;
|
|
|
|
cnt = ::fscanf(f, "%64s\n", value);
|
|
|
|
if (cnt <= 0) break;
|
|
|
|
RecordSetting(name, value);
|
|
|
|
}
|
|
|
|
::fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
defaultSink = new nsFileLogEventSink();
|
1999-12-29 23:53:17 +03:00
|
|
|
if (defaultSink == nsnull) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(defaultSink);
|
|
|
|
if (outputPath)
|
2000-10-27 10:18:39 +04:00
|
|
|
rv = defaultSink->Init(outputPath);
|
1999-12-29 23:53:17 +03:00
|
|
|
else
|
2000-10-27 10:18:39 +04:00
|
|
|
rv = defaultSink->InitFromFILE("stderr", stderr);
|
1999-12-29 23:53:17 +03:00
|
|
|
if (NS_FAILED(rv)) goto error;
|
|
|
|
|
|
|
|
mDefaultSink = defaultSink;
|
|
|
|
NS_RELEASE(defaultSink);
|
2000-10-27 10:18:39 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
DescribeLogs(LogInfo);
|
|
|
|
#endif
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
NS_IF_RELEASE(defaultSink);
|
|
|
|
if (gLogMonitor) {
|
|
|
|
PR_DestroyMonitor(gLogMonitor);
|
|
|
|
gLogMonitor = nsnull;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
static nsresult
|
|
|
|
EnsureLoggingService()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (gLoggingService == nsnull) {
|
|
|
|
gLoggingService = new nsLoggingService();
|
|
|
|
if (gLoggingService == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = gLoggingService->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete gLoggingService;
|
2000-11-01 23:30:47 +03:00
|
|
|
gLoggingService = nsnull;
|
2000-10-27 10:18:39 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// Note that there's no AddRef here. That's because when the service manager
|
|
|
|
// gets around to calling Create (below) sometime later, we'll AddRef it then
|
|
|
|
// and the service manager will be the sole owner. This allows us to use it
|
|
|
|
// before xpcom has started up.
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
NS_METHOD
|
|
|
|
nsLoggingService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (outer)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
if (gLoggingService == nsnull) {
|
|
|
|
rv = EnsureLoggingService();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
return gLoggingService->QueryInterface(aIID, aInstancePtr);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLoggingService::GetLog(const char* name, nsILog* *result)
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsCStringKey key(name);
|
1999-12-29 23:53:17 +03:00
|
|
|
nsILog* log = (nsILog*)mLogs.Get(&key);
|
|
|
|
if (log) {
|
|
|
|
*result = log;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLog* newLog = new nsLog();
|
|
|
|
if (newLog == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nsresult rv = newLog->Init(name, mDefaultControlFlags, mDefaultSink);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete newLog;
|
|
|
|
return rv;
|
|
|
|
}
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_ADDREF(newLog);
|
1999-12-29 23:53:17 +03:00
|
|
|
mLogs.Put(&key, newLog);
|
|
|
|
*result = newLog;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
PR_IMPLEMENT(nsILog*)
|
|
|
|
NS_GetLog(const char* name, PRUint32 controlFlags)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
nsresult rv;
|
1999-12-29 23:53:17 +03:00
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
if (gLoggingService == nsnull) {
|
|
|
|
rv = EnsureLoggingService();
|
|
|
|
if (NS_FAILED(rv)) return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsILog* log;
|
|
|
|
rv = gLoggingService->GetLog(name, &log);
|
|
|
|
if (NS_FAILED(rv)) return nsnull;
|
|
|
|
|
|
|
|
// add in additional flags:
|
|
|
|
PRUint32 flags;
|
|
|
|
log->GetControlFlags(&flags);
|
|
|
|
flags |= controlFlags;
|
|
|
|
log->SetControlFlags(flags);
|
|
|
|
|
|
|
|
return log;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
2000-10-27 22:30:16 +04:00
|
|
|
static PRBool PR_CALLBACK
|
2000-10-27 10:18:39 +04:00
|
|
|
DescribeLog(nsHashKey *aKey, void *aData, void* closure)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsILog* log = (nsILog*)aData;
|
|
|
|
nsILog* out = (nsILog*)closure;
|
2000-10-27 10:18:39 +04:00
|
|
|
(void)log->Describe(out);
|
1999-12-29 23:53:17 +03:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLoggingService::DescribeLogs(nsILog* out)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_LOG_PRINTF(out)("%-20.20s %-8.8s %s\n", "LOG NAME", "ENABLED", "DESTINATION");
|
|
|
|
mLogs.Enumerate(DescribeLog, out);
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLoggingService::GetDefaultControlFlags(PRUint32 *controlFlags)
|
|
|
|
{
|
|
|
|
*controlFlags = mDefaultControlFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLoggingService::SetDefaultControlFlags(PRUint32 controlFlags)
|
|
|
|
{
|
|
|
|
mDefaultControlFlags = controlFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLoggingService::GetDefaultLogEventSink(nsILogEventSink* *sink)
|
|
|
|
{
|
|
|
|
*sink = mDefaultSink;
|
|
|
|
NS_ADDREF(*sink);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLoggingService::SetDefaultLogEventSink(nsILogEventSink* sink)
|
|
|
|
{
|
|
|
|
mDefaultSink = sink;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsLog
|
|
|
|
|
|
|
|
nsLog::nsLog()
|
|
|
|
: mName(nsnull),
|
|
|
|
mIndentLevel(0)
|
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_INIT_ISUPPORTS();
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsLog::~nsLog()
|
|
|
|
{
|
|
|
|
if (mName) nsCRT::free(mName);
|
|
|
|
}
|
|
|
|
|
2000-11-01 04:41:15 +03:00
|
|
|
// !!! We don't use NS_IMPL_ISUPPORTS for nsLog because logs always appear to
|
|
|
|
// leak due to the way NS_IMPL_LOG works.
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsLog::AddRef(void)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
|
|
|
|
NS_ASSERT_OWNINGTHREAD(nsLog);
|
|
|
|
++mRefCnt;
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsLog::Release(void)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
|
|
|
NS_ASSERT_OWNINGTHREAD(nsLog);
|
|
|
|
--mRefCnt;
|
|
|
|
if (mRefCnt == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
NS_DELETEXPCOM(this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsLog, nsILog)
|
1999-12-29 23:53:17 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLog::Init(const char* name, PRUint32 controlFlags, nsILogEventSink* sink)
|
|
|
|
{
|
|
|
|
mName = nsCRT::strdup(name);
|
|
|
|
if (mName == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
mControlFlags = controlFlags;
|
|
|
|
mSink = sink;
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsCStringKey key(name);
|
1999-12-29 23:53:17 +03:00
|
|
|
PRUint32 level = (PRUint32)gSettings->Get(&key);
|
2000-10-27 10:18:39 +04:00
|
|
|
if (level != 0) {
|
|
|
|
mControlFlags |= nsILog::DEFAULT_ENABLED;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLog::GetName(char* *aName)
|
|
|
|
{
|
|
|
|
*aName = nsCRT::strdup(mName);
|
|
|
|
return *aName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::Enabled(PRBool *result)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
*result = Test();
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::Print(const PRUnichar *message)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
2000-10-27 10:18:39 +04:00
|
|
|
|
|
|
|
nsCString str; str.AssignWithConversion(message);
|
2001-03-02 12:26:57 +03:00
|
|
|
nsresult rv = Printf(str.get());
|
1999-12-29 23:53:17 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLog::Flush(void)
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
return mSink->Flush(this);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLog::IncreaseIndent()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
|
|
|
mIndentLevel++;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLog::DecreaseIndent()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
PR_ASSERT(mIndentLevel > 0);
|
1999-12-29 23:53:17 +03:00
|
|
|
if (mIndentLevel == 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mIndentLevel--;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLog::GetIndentLevel(PRUint32 *aIndentLevel)
|
|
|
|
{
|
|
|
|
*aIndentLevel = mIndentLevel;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLog::Describe(nsILog* out)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-10-27 10:18:39 +04:00
|
|
|
#if 0
|
1999-12-29 23:53:17 +03:00
|
|
|
const char* levelName;
|
|
|
|
switch (mEnabledLevel) {
|
|
|
|
case nsILog::LEVEL_NEVER: levelName = "NEVER"; break;
|
|
|
|
case nsILog::LEVEL_ERROR: levelName = "ERROR"; break;
|
|
|
|
case nsILog::LEVEL_WARN: levelName = "WARN"; break;
|
|
|
|
case nsILog::LEVEL_STDOUT: levelName = "STDOUT"; break;
|
|
|
|
case nsILog::LEVEL_DBG: levelName = "DBG"; break;
|
|
|
|
default: levelName = "<unknown>"; break;
|
|
|
|
}
|
2000-10-27 10:18:39 +04:00
|
|
|
#endif
|
1999-12-29 23:53:17 +03:00
|
|
|
char* dest = nsnull;
|
|
|
|
rv = mSink->GetDestinationName(&dest);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
dest = nsCRT::strdup("<unknown>");
|
|
|
|
}
|
2000-10-27 10:18:39 +04:00
|
|
|
// NS_LOG(out, (" %-20.20s %-8.8s %s\n", mName, levelName, dest));
|
|
|
|
PRBool enabled = mControlFlags & nsILog::DEFAULT_ENABLED;
|
|
|
|
NS_LOG_PRINTF(out)("%-20.20s %-8.8s %s\n",
|
|
|
|
mName, (enabled ? "yes" : "no"), dest);
|
1999-12-29 23:53:17 +03:00
|
|
|
if (dest) nsCRT::free(dest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::GetControlFlags(PRUint32 *flags)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
1999-12-29 23:53:17 +03:00
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
*flags = mControlFlags;
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::SetControlFlags(PRUint32 flags)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
1999-12-29 23:53:17 +03:00
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
mControlFlags = flags;
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::GetLogEventSink(nsILogEventSink* *sink)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
*sink = mSink;
|
|
|
|
NS_ADDREF(*sink);
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::SetLogEventSink(nsILogEventSink* sink)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
mSink = sink;
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsLog
|
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::Printf(const char* format, ...)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2000-11-01 04:41:15 +03:00
|
|
|
nsresult rv = Vprintf(format, args);
|
2000-10-27 10:18:39 +04:00
|
|
|
va_end(args);
|
2000-11-01 04:41:15 +03:00
|
|
|
return rv;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLog::Vprintf(const char* format, va_list args)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
2000-11-01 04:41:15 +03:00
|
|
|
char* msg = PR_vsmprintf(format, args);
|
|
|
|
if (!msg) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nsresult rv = mSink->Print(this, msg);
|
|
|
|
PR_smprintf_free(msg);
|
|
|
|
return rv;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2000-10-27 10:18:39 +04:00
|
|
|
// nsLogIndent
|
1999-12-29 23:53:17 +03:00
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLogIndent::nsLogIndent(nsILog* log, const char* msg)
|
|
|
|
: mLog(log), mHeaderMsg(msg)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
if (mHeaderMsg) mLog->Printf("[ Begin %s", mHeaderMsg);
|
|
|
|
(void)mLog->IncreaseIndent();
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsLogIndent::~nsLogIndent()
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
(void)mLog->DecreaseIndent();
|
|
|
|
if (mHeaderMsg) mLog->Printf("] End %s", mHeaderMsg);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2000-10-27 10:18:39 +04:00
|
|
|
// nsFileLogEventSink
|
1999-12-29 23:53:17 +03:00
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::nsFileLogEventSink()
|
1999-12-29 23:53:17 +03:00
|
|
|
: mName(nsnull),
|
|
|
|
mOutput(nsnull),
|
|
|
|
mBeginningOfLine(PR_TRUE),
|
|
|
|
mCloseFile(PR_FALSE)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::~nsFileLogEventSink()
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
if (mCloseFile) {
|
|
|
|
::fclose(mOutput);
|
|
|
|
mOutput = nsnull;
|
|
|
|
}
|
|
|
|
if (mName) nsCRT::free(mName);
|
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(nsFileLogEventSink,
|
|
|
|
nsIFileLogEventSink,
|
1999-12-29 23:53:17 +03:00
|
|
|
nsILogEventSink)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::GetDestinationName(char* *result)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
*result = nsCRT::strdup(mName);
|
|
|
|
return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::Init(const char* filePath)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
FILE* filePtr;
|
|
|
|
if (nsCRT::strcmp(filePath, "1") == 0) {
|
|
|
|
filePtr = stdout;
|
|
|
|
}
|
|
|
|
else if (nsCRT::strcmp(filePath, "2") == 0) {
|
|
|
|
filePtr = stderr;
|
|
|
|
}
|
|
|
|
else {
|
2000-11-01 23:30:47 +03:00
|
|
|
filePtr = ::fopen(filePath, "w");
|
1999-12-29 23:53:17 +03:00
|
|
|
if (filePtr == nsnull)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mCloseFile = PR_TRUE;
|
|
|
|
}
|
2000-10-27 10:18:39 +04:00
|
|
|
return InitFromFILE(filePath, filePtr);
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::InitFromFILE(const char* name, FILE* filePtr)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
mOutput = filePtr;
|
|
|
|
mName = nsCRT::strdup(name);
|
|
|
|
return mName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::Print(nsILog* log, const char* msg)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
if (!NS_LOG_ENABLED(log))
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
// do debug output first
|
2000-10-27 22:30:16 +04:00
|
|
|
#ifdef XP_WIN
|
2000-10-27 10:18:39 +04:00
|
|
|
OutputDebugString(msg);
|
1999-12-29 23:53:17 +03:00
|
|
|
#elif defined(XP_MAC)
|
2000-10-27 10:18:39 +04:00
|
|
|
{
|
|
|
|
# define BUF_SIZE 1024
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
PRUint32 len =
|
|
|
|
PR_snprintf(buf+1, BUF_SIZE-1, "%s", msg);
|
|
|
|
buf[0] = (char) (len > 255 ? 255 : len);
|
|
|
|
DebugStr(StringPtr(buf));
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
2000-10-27 10:18:39 +04:00
|
|
|
#endif
|
1999-12-29 23:53:17 +03:00
|
|
|
|
|
|
|
if (!mBeginningOfLine) {
|
|
|
|
::fputc('\n', mOutput);
|
2000-10-27 10:18:39 +04:00
|
|
|
mBeginningOfLine = PR_TRUE;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// print preamble
|
|
|
|
PRUint32 flags;
|
|
|
|
rv = log->GetControlFlags(&flags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-10-27 10:18:39 +04:00
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
if (flags & nsILog::PRINT_THREAD_ID) {
|
2000-10-27 10:18:39 +04:00
|
|
|
::fprintf(mOutput, "%8x ", (PRInt32)PR_CurrentThread());
|
|
|
|
mBeginningOfLine = PR_FALSE;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
2000-10-27 10:18:39 +04:00
|
|
|
if (flags & nsILog::PRINT_LOG_NAME) {
|
|
|
|
char* name;
|
|
|
|
rv = log->GetName(&name);
|
|
|
|
::fprintf(mOutput, "%-8.8s ",
|
|
|
|
flags & nsILog::PRINT_LOG_NAME ? name : "");
|
|
|
|
nsCRT::free(name);
|
|
|
|
mBeginningOfLine = PR_FALSE;
|
|
|
|
}
|
1999-12-29 23:53:17 +03:00
|
|
|
|
|
|
|
PRUint32 indentLevel;
|
|
|
|
rv = log->GetIndentLevel(&indentLevel);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
do {
|
|
|
|
indent:
|
|
|
|
// do indentation
|
|
|
|
for (PRUint32 i = 0; i < indentLevel; i++) {
|
2000-10-27 10:18:39 +04:00
|
|
|
::fprintf(mOutput, "| ");
|
|
|
|
mBeginningOfLine = PR_FALSE;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char c;
|
|
|
|
while ((c = *msg++)) {
|
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
if (*msg == '\0') {
|
|
|
|
::fputc('\n', mOutput);
|
|
|
|
mBeginningOfLine = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2000-10-27 10:18:39 +04:00
|
|
|
::fprintf(mOutput, "\n ");
|
|
|
|
mBeginningOfLine = PR_FALSE;
|
1999-12-29 23:53:17 +03:00
|
|
|
goto indent;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
::fputc(c, mOutput);
|
2000-10-27 10:18:39 +04:00
|
|
|
mBeginningOfLine = PR_FALSE;
|
1999-12-29 23:53:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
2000-10-27 10:18:39 +04:00
|
|
|
|
|
|
|
if (!mBeginningOfLine) {
|
|
|
|
::fputc('\n', mOutput);
|
|
|
|
mBeginningOfLine = PR_TRUE;
|
|
|
|
}
|
1999-12-29 23:53:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-10-27 10:18:39 +04:00
|
|
|
nsFileLogEventSink::Flush(nsILog* log)
|
1999-12-29 23:53:17 +03:00
|
|
|
{
|
2000-10-27 10:18:39 +04:00
|
|
|
nsAutoMonitor monitor(gLogMonitor);
|
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
::fflush(mOutput);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-11-01 04:41:15 +03:00
|
|
|
#else
|
|
|
|
|
|
|
|
PR_IMPLEMENT(nsILog*)
|
|
|
|
NS_GetLog(const char* name, PRUint32 controlFlags)
|
|
|
|
{
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
1999-12-29 23:53:17 +03:00
|
|
|
#endif // NS_ENABLE_LOGGING
|