2015-10-19 22:22:11 +03: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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2016-01-06 00:05:19 +03:00
|
|
|
#include "mozilla/FileUtils.h"
|
2015-10-19 22:22:11 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2016-07-29 13:51:00 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2016-07-29 13:51:00 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2015-10-19 22:22:11 +03:00
|
|
|
#include "nsClassHashtable.h"
|
2016-01-06 00:05:19 +03:00
|
|
|
#include "nsDebug.h"
|
2016-01-05 23:16:03 +03:00
|
|
|
#include "NSPRLogModulesParser.h"
|
|
|
|
|
|
|
|
#include "prenv.h"
|
2016-02-18 15:56:00 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2015-10-19 22:22:11 +03:00
|
|
|
|
|
|
|
// NB: Initial amount determined by auditing the codebase for the total amount
|
|
|
|
// of unique module names and padding up to the next power of 2.
|
|
|
|
const uint32_t kInitialModuleCount = 256;
|
2016-07-29 13:51:00 +03:00
|
|
|
// When rotate option is added to the modules list, this is the hardcoded
|
|
|
|
// number of files we create and rotate. When there is rotate:40,
|
|
|
|
// we will keep four files per process, each limited to 10MB. Sum is 40MB,
|
|
|
|
// the given limit.
|
2017-03-06 19:42:31 +03:00
|
|
|
//
|
|
|
|
// (Note: When this is changed to be >= 10, SandboxBroker::LaunchApp must add
|
|
|
|
// another rule to allow logfile.?? be written by content processes.)
|
2016-07-29 13:51:00 +03:00
|
|
|
const uint32_t kRotateFilesNumber = 4;
|
2015-10-19 22:22:11 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-12-23 02:09:41 +03:00
|
|
|
|
2017-08-18 19:32:05 +03:00
|
|
|
LazyLogModule::operator LogModule*()
|
|
|
|
{
|
|
|
|
// NB: The use of an atomic makes the reading and assignment of mLog
|
|
|
|
// thread-safe. There is a small chance that mLog will be set more
|
|
|
|
// than once, but that's okay as it will be set to the same LogModule
|
|
|
|
// instance each time. Also note LogModule::Get is thread-safe.
|
|
|
|
LogModule* tmp = mLog;
|
|
|
|
if (MOZ_UNLIKELY(!tmp)) {
|
|
|
|
tmp = LogModule::Get(mLogName);
|
|
|
|
mLog = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
void log_print(const LogModule* aModule,
|
|
|
|
LogLevel aLevel,
|
|
|
|
const char* aFmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, aFmt);
|
|
|
|
aModule->Printv(aLevel, aFmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2016-07-29 13:51:00 +03:00
|
|
|
} // detail
|
2016-01-06 00:05:19 +03:00
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
LogLevel
|
2015-12-23 02:09:41 +03:00
|
|
|
ToLogLevel(int32_t aLevel)
|
2015-10-19 22:22:11 +03:00
|
|
|
{
|
|
|
|
aLevel = std::min(aLevel, static_cast<int32_t>(LogLevel::Verbose));
|
|
|
|
aLevel = std::max(aLevel, static_cast<int32_t>(LogLevel::Disabled));
|
|
|
|
return static_cast<LogLevel>(aLevel);
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
const char*
|
|
|
|
ToLogStr(LogLevel aLevel) {
|
|
|
|
switch (aLevel) {
|
|
|
|
case LogLevel::Error:
|
|
|
|
return "E";
|
|
|
|
case LogLevel::Warning:
|
|
|
|
return "W";
|
|
|
|
case LogLevel::Info:
|
|
|
|
return "I";
|
|
|
|
case LogLevel::Debug:
|
|
|
|
return "D";
|
|
|
|
case LogLevel::Verbose:
|
|
|
|
return "V";
|
|
|
|
case LogLevel::Disabled:
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Invalid log level.");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 13:51:00 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class providing reference counting for FILE*.
|
|
|
|
* It encapsulates the following:
|
|
|
|
* - the FILE handle
|
|
|
|
* - the order number it was created for when rotating (actual path)
|
|
|
|
* - number of active references
|
|
|
|
*/
|
|
|
|
class LogFile
|
|
|
|
{
|
|
|
|
FILE* mFile;
|
|
|
|
uint32_t mFileNum;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LogFile(FILE* aFile, uint32_t aFileNum)
|
|
|
|
: mFile(aFile)
|
|
|
|
, mFileNum(aFileNum)
|
|
|
|
, mNextToRelease(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~LogFile()
|
|
|
|
{
|
|
|
|
fclose(mFile);
|
|
|
|
delete mNextToRelease;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* File() const { return mFile; }
|
|
|
|
uint32_t Num() const { return mFileNum; }
|
|
|
|
|
|
|
|
LogFile* mNextToRelease;
|
|
|
|
};
|
|
|
|
|
2016-09-19 18:39:47 +03:00
|
|
|
const char*
|
|
|
|
ExpandPIDMarker(const char* aFilename, char (&buffer)[2048])
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFilename);
|
|
|
|
static const char kPIDToken[] = "%PID";
|
|
|
|
const char* pidTokenPtr = strstr(aFilename, kPIDToken);
|
|
|
|
if (pidTokenPtr &&
|
|
|
|
SprintfLiteral(buffer, "%.*s%s%d%s",
|
|
|
|
static_cast<int>(pidTokenPtr - aFilename), aFilename,
|
|
|
|
XRE_IsParentProcess() ? "-main." : "-child.",
|
|
|
|
base::GetCurrentProcId(),
|
|
|
|
pidTokenPtr + strlen(kPIDToken)) > 0)
|
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aFilename;
|
|
|
|
}
|
|
|
|
|
2016-07-29 13:51:00 +03:00
|
|
|
} // detail
|
|
|
|
|
2016-10-05 12:34:12 +03:00
|
|
|
namespace {
|
|
|
|
// Helper method that initializes an empty va_list to be empty.
|
|
|
|
void empty_va(va_list *va, ...)
|
|
|
|
{
|
|
|
|
va_start(*va, va);
|
|
|
|
va_end(*va);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
class LogModuleManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LogModuleManager()
|
|
|
|
: mModulesLock("logmodules")
|
|
|
|
, mModules(kInitialModuleCount)
|
2016-07-29 13:51:00 +03:00
|
|
|
, mPrintEntryCount(0)
|
2016-01-06 00:05:19 +03:00
|
|
|
, mOutFile(nullptr)
|
2016-07-29 13:51:00 +03:00
|
|
|
, mToReleaseFile(nullptr)
|
|
|
|
, mOutFileNum(0)
|
2016-09-19 18:39:47 +03:00
|
|
|
, mOutFilePath(strdup(""))
|
2016-02-02 09:21:00 +03:00
|
|
|
, mMainThread(PR_GetCurrentThread())
|
2016-09-19 18:39:47 +03:00
|
|
|
, mSetFromEnv(false)
|
2016-01-06 00:05:19 +03:00
|
|
|
, mAddTimestamp(false)
|
2016-01-23 04:21:31 +03:00
|
|
|
, mIsSync(false)
|
2016-07-29 13:51:00 +03:00
|
|
|
, mRotate(0)
|
2015-10-19 22:22:11 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~LogModuleManager()
|
|
|
|
{
|
2016-07-29 13:51:00 +03:00
|
|
|
detail::LogFile* logFile = mOutFile.exchange(nullptr);
|
|
|
|
delete logFile;
|
2015-10-19 22:22:11 +03:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:16:03 +03:00
|
|
|
/**
|
|
|
|
* Loads config from env vars if present.
|
|
|
|
*/
|
|
|
|
void Init()
|
|
|
|
{
|
2016-01-06 00:05:19 +03:00
|
|
|
bool shouldAppend = false;
|
|
|
|
bool addTimestamp = false;
|
2016-01-23 04:21:31 +03:00
|
|
|
bool isSync = false;
|
2016-07-29 13:51:00 +03:00
|
|
|
int32_t rotate = 0;
|
2016-05-17 01:49:20 +03:00
|
|
|
const char* modules = PR_GetEnv("MOZ_LOG");
|
|
|
|
if (!modules || !modules[0]) {
|
|
|
|
modules = PR_GetEnv("MOZ_LOG_MODULES");
|
|
|
|
if (modules) {
|
|
|
|
NS_WARNING("MOZ_LOG_MODULES is deprecated."
|
|
|
|
"\nPlease use MOZ_LOG instead.");
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 16:22:00 +03:00
|
|
|
if (!modules || !modules[0]) {
|
|
|
|
modules = PR_GetEnv("NSPR_LOG_MODULES");
|
2016-05-17 01:49:20 +03:00
|
|
|
if (modules) {
|
|
|
|
NS_WARNING("NSPR_LOG_MODULES is deprecated."
|
|
|
|
"\nPlease use MOZ_LOG instead.");
|
|
|
|
}
|
2016-03-10 16:22:00 +03:00
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
NSPRLogModulesParser(modules,
|
2016-07-29 13:51:00 +03:00
|
|
|
[&shouldAppend, &addTimestamp, &isSync, &rotate]
|
|
|
|
(const char* aName, LogLevel aLevel, int32_t aValue) mutable {
|
2016-01-06 00:05:19 +03:00
|
|
|
if (strcmp(aName, "append") == 0) {
|
|
|
|
shouldAppend = true;
|
|
|
|
} else if (strcmp(aName, "timestamp") == 0) {
|
|
|
|
addTimestamp = true;
|
2016-01-23 04:21:31 +03:00
|
|
|
} else if (strcmp(aName, "sync") == 0) {
|
|
|
|
isSync = true;
|
2016-07-29 13:51:00 +03:00
|
|
|
} else if (strcmp(aName, "rotate") == 0) {
|
|
|
|
rotate = (aValue << 20) / kRotateFilesNumber;
|
2016-01-06 00:05:19 +03:00
|
|
|
} else {
|
|
|
|
LogModule::Get(aName)->SetLevel(aLevel);
|
|
|
|
}
|
2016-01-05 23:16:03 +03:00
|
|
|
});
|
2016-01-06 00:05:19 +03:00
|
|
|
|
2016-07-29 13:51:00 +03:00
|
|
|
// Rotate implies timestamp to make the files readable
|
|
|
|
mAddTimestamp = addTimestamp || rotate > 0;
|
2016-01-23 04:21:31 +03:00
|
|
|
mIsSync = isSync;
|
2016-07-29 13:51:00 +03:00
|
|
|
mRotate = rotate;
|
|
|
|
|
|
|
|
if (rotate > 0 && shouldAppend) {
|
|
|
|
NS_WARNING("MOZ_LOG: when you rotate the log, you cannot use append!");
|
|
|
|
}
|
2016-01-06 00:05:19 +03:00
|
|
|
|
2016-03-10 16:22:00 +03:00
|
|
|
const char* logFile = PR_GetEnv("MOZ_LOG_FILE");
|
|
|
|
if (!logFile || !logFile[0]) {
|
|
|
|
logFile = PR_GetEnv("NSPR_LOG_FILE");
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
if (logFile && logFile[0]) {
|
2016-02-18 15:56:00 +03:00
|
|
|
char buf[2048];
|
2016-09-19 18:39:47 +03:00
|
|
|
logFile = detail::ExpandPIDMarker(logFile, buf);
|
2016-07-29 13:51:00 +03:00
|
|
|
mOutFilePath.reset(strdup(logFile));
|
|
|
|
|
|
|
|
if (mRotate > 0) {
|
|
|
|
// Delete all the previously captured files, including non-rotated
|
|
|
|
// log files, so that users don't complain our logs eat space even
|
|
|
|
// after the rotate option has been added and don't happen to send
|
|
|
|
// us old large logs along with the rotated files.
|
|
|
|
remove(mOutFilePath.get());
|
|
|
|
for (uint32_t i = 0; i < kRotateFilesNumber; ++i) {
|
|
|
|
RemoveFile(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutFile = OpenFile(shouldAppend, mOutFileNum);
|
2016-09-19 18:39:47 +03:00
|
|
|
mSetFromEnv = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLogFile(const char* aFilename)
|
|
|
|
{
|
|
|
|
// For now we don't allow you to change the file at runtime.
|
|
|
|
if (mSetFromEnv) {
|
|
|
|
NS_WARNING("LogModuleManager::SetLogFile - Log file was set from the "
|
|
|
|
"MOZ_LOG_FILE environment variable.");
|
|
|
|
return;
|
2016-07-29 13:51:00 +03:00
|
|
|
}
|
2016-09-19 18:39:47 +03:00
|
|
|
|
|
|
|
const char * filename = aFilename ? aFilename : "";
|
|
|
|
char buf[2048];
|
|
|
|
filename = detail::ExpandPIDMarker(filename, buf);
|
|
|
|
|
|
|
|
// Can't use rotate at runtime yet.
|
|
|
|
MOZ_ASSERT(mRotate == 0, "We don't allow rotate for runtime logfile changes");
|
|
|
|
mOutFilePath.reset(strdup(filename));
|
|
|
|
|
|
|
|
// Exchange mOutFile and set it to be released once all the writes are done.
|
|
|
|
detail::LogFile* newFile = OpenFile(false, 0);
|
|
|
|
detail::LogFile* oldFile = mOutFile.exchange(newFile);
|
|
|
|
|
|
|
|
// Since we don't allow changing the logfile if MOZ_LOG_FILE is already set,
|
|
|
|
// and we don't allow log rotation when setting it at runtime, mToReleaseFile
|
|
|
|
// will be null, so we're not leaking.
|
|
|
|
DebugOnly<detail::LogFile*> prevFile = mToReleaseFile.exchange(oldFile);
|
|
|
|
MOZ_ASSERT(!prevFile, "Should be null because rotation is not allowed");
|
2016-10-05 12:34:12 +03:00
|
|
|
|
|
|
|
// If we just need to release a file, we must force print, in order to
|
|
|
|
// trigger the closing and release of mToReleaseFile.
|
|
|
|
if (oldFile) {
|
|
|
|
va_list va;
|
|
|
|
empty_va(&va);
|
|
|
|
Print("Logger", LogLevel::Info, "Flushing old log files\n", va);
|
|
|
|
}
|
2016-09-19 18:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetLogFile(char *aBuffer, size_t aLength)
|
|
|
|
{
|
|
|
|
uint32_t len = strlen(mOutFilePath.get());
|
|
|
|
if (len + 1 > aLength) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
snprintf(aBuffer, aLength, "%s", mOutFilePath.get());
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIsSync(bool aIsSync)
|
|
|
|
{
|
|
|
|
mIsSync = aIsSync;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAddTimestamp(bool aAddTimestamp)
|
|
|
|
{
|
|
|
|
mAddTimestamp = aAddTimestamp;
|
2016-07-29 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
detail::LogFile* OpenFile(bool aShouldAppend, uint32_t aFileNum)
|
|
|
|
{
|
|
|
|
FILE* file;
|
|
|
|
|
|
|
|
if (mRotate > 0) {
|
|
|
|
char buf[2048];
|
2016-08-15 09:44:00 +03:00
|
|
|
SprintfLiteral(buf, "%s.%d", mOutFilePath.get(), aFileNum);
|
2016-07-29 13:51:00 +03:00
|
|
|
|
|
|
|
// rotate doesn't support append.
|
|
|
|
file = fopen(buf, "w");
|
|
|
|
} else {
|
|
|
|
file = fopen(mOutFilePath.get(), aShouldAppend ? "a" : "w");
|
2016-01-06 00:05:19 +03:00
|
|
|
}
|
2016-07-29 13:51:00 +03:00
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new detail::LogFile(file, aFileNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveFile(uint32_t aFileNum)
|
|
|
|
{
|
|
|
|
char buf[2048];
|
2016-08-15 09:44:00 +03:00
|
|
|
SprintfLiteral(buf, "%s.%d", mOutFilePath.get(), aFileNum);
|
2016-07-29 13:51:00 +03:00
|
|
|
remove(buf);
|
2016-01-05 23:16:03 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
LogModule* CreateOrGetModule(const char* aName)
|
|
|
|
{
|
|
|
|
OffTheBooksMutexAutoLock guard(mModulesLock);
|
|
|
|
LogModule* module = nullptr;
|
|
|
|
if (!mModules.Get(aName, &module)) {
|
2016-01-06 00:05:19 +03:00
|
|
|
module = new LogModule(aName, LogLevel::Disabled);
|
2015-10-19 22:22:11 +03:00
|
|
|
mModules.Put(aName, module);
|
|
|
|
}
|
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
void Print(const char* aName, LogLevel aLevel, const char* aFmt, va_list aArgs)
|
2017-05-04 21:10:19 +03:00
|
|
|
MOZ_FORMAT_PRINTF(4, 0)
|
2016-01-06 00:05:19 +03:00
|
|
|
{
|
|
|
|
const size_t kBuffSize = 1024;
|
|
|
|
char buff[kBuffSize];
|
|
|
|
|
|
|
|
char* buffToWrite = buff;
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPointer allocatedBuff;
|
2016-01-06 00:05:19 +03:00
|
|
|
|
2016-01-15 21:14:26 +03:00
|
|
|
va_list argsCopy;
|
|
|
|
va_copy(argsCopy, aArgs);
|
2017-03-01 13:02:00 +03:00
|
|
|
int charsWritten = VsprintfLiteral(buff, aFmt, argsCopy);
|
2016-01-15 21:14:26 +03:00
|
|
|
va_end(argsCopy);
|
|
|
|
|
2017-03-01 13:02:00 +03:00
|
|
|
if (charsWritten < 0) {
|
|
|
|
// Print out at least something. We must copy to the local buff,
|
|
|
|
// can't just assign aFmt to buffToWrite, since when
|
|
|
|
// buffToWrite != buff, we try to release it.
|
|
|
|
MOZ_ASSERT(false, "Probably incorrect format string in LOG?");
|
|
|
|
strncpy(buff, aFmt, kBuffSize - 1);
|
|
|
|
buff[kBuffSize - 1] = '\0';
|
|
|
|
charsWritten = strlen(buff);
|
|
|
|
} else if (static_cast<size_t>(charsWritten) >= kBuffSize - 1) {
|
2016-01-06 00:05:19 +03:00
|
|
|
// We may have maxed out, allocate a buffer instead.
|
2017-03-03 18:17:27 +03:00
|
|
|
allocatedBuff = mozilla::Vsmprintf(aFmt, aArgs);
|
|
|
|
buffToWrite = allocatedBuff.get();
|
2016-01-13 21:58:44 +03:00
|
|
|
charsWritten = strlen(buffToWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if a newline needs to be appended to the message.
|
|
|
|
const char* newline = "";
|
|
|
|
if (charsWritten == 0 || buffToWrite[charsWritten - 1] != '\n') {
|
|
|
|
newline = "\n";
|
2016-01-06 00:05:19 +03:00
|
|
|
}
|
|
|
|
|
2016-07-29 13:51:00 +03:00
|
|
|
FILE* out = stderr;
|
|
|
|
|
|
|
|
// In case we use rotate, this ensures the FILE is kept alive during
|
|
|
|
// its use. Increased before we load mOutFile.
|
|
|
|
++mPrintEntryCount;
|
|
|
|
|
|
|
|
detail::LogFile* outFile = mOutFile;
|
|
|
|
if (outFile) {
|
|
|
|
out = outFile->File();
|
|
|
|
}
|
2016-01-06 00:05:19 +03:00
|
|
|
|
|
|
|
// This differs from the NSPR format in that we do not output the
|
|
|
|
// opaque system specific thread pointer (ie pthread_t) cast
|
|
|
|
// to a long. The address of the current PR_Thread continues to be
|
|
|
|
// prefixed.
|
|
|
|
//
|
|
|
|
// Additionally we prefix the output with the abbreviated log level
|
|
|
|
// and the module name.
|
2016-02-02 09:21:00 +03:00
|
|
|
PRThread *currentThread = PR_GetCurrentThread();
|
|
|
|
const char *currentThreadName = (mMainThread == currentThread)
|
|
|
|
? "Main Thread"
|
|
|
|
: PR_GetThreadName(currentThread);
|
|
|
|
|
|
|
|
char noNameThread[40];
|
|
|
|
if (!currentThreadName) {
|
2016-08-15 09:44:00 +03:00
|
|
|
SprintfLiteral(noNameThread, "Unnamed thread %p", currentThread);
|
2016-02-02 09:21:00 +03:00
|
|
|
currentThreadName = noNameThread;
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
if (!mAddTimestamp) {
|
|
|
|
fprintf_stderr(out,
|
2016-02-02 09:21:00 +03:00
|
|
|
"[%s]: %s/%s %s%s",
|
|
|
|
currentThreadName, ToLogStr(aLevel),
|
2016-01-13 21:58:44 +03:00
|
|
|
aName, buffToWrite, newline);
|
2016-01-06 00:05:19 +03:00
|
|
|
} else {
|
|
|
|
PRExplodedTime now;
|
|
|
|
PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now);
|
|
|
|
fprintf_stderr(
|
|
|
|
out,
|
2016-02-02 09:21:00 +03:00
|
|
|
"%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%s]: %s/%s %s%s",
|
2016-01-06 00:05:19 +03:00
|
|
|
now.tm_year, now.tm_month + 1, now.tm_mday,
|
|
|
|
now.tm_hour, now.tm_min, now.tm_sec, now.tm_usec,
|
2016-02-02 09:21:00 +03:00
|
|
|
currentThreadName, ToLogStr(aLevel),
|
2016-01-13 21:58:44 +03:00
|
|
|
aName, buffToWrite, newline);
|
2016-01-06 00:05:19 +03:00
|
|
|
}
|
|
|
|
|
2016-01-23 04:21:31 +03:00
|
|
|
if (mIsSync) {
|
|
|
|
fflush(out);
|
|
|
|
}
|
|
|
|
|
2016-07-29 13:51:00 +03:00
|
|
|
if (mRotate > 0 && outFile) {
|
|
|
|
int32_t fileSize = ftell(out);
|
|
|
|
if (fileSize > mRotate) {
|
|
|
|
uint32_t fileNum = outFile->Num();
|
|
|
|
|
|
|
|
uint32_t nextFileNum = fileNum + 1;
|
|
|
|
if (nextFileNum >= kRotateFilesNumber) {
|
|
|
|
nextFileNum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// And here is the trick. The current out-file remembers its order
|
|
|
|
// number. When no other thread shifted the global file number yet,
|
|
|
|
// we are the thread to open the next file.
|
|
|
|
if (mOutFileNum.compareExchange(fileNum, nextFileNum)) {
|
|
|
|
// We can work with mToReleaseFile because we are sure the
|
|
|
|
// mPrintEntryCount can't drop to zero now - the condition
|
|
|
|
// to actually delete what's stored in that member.
|
|
|
|
// And also, no other thread can enter this piece of code
|
|
|
|
// because mOutFile is still holding the current file with
|
|
|
|
// the non-shifted number. The compareExchange() above is
|
|
|
|
// a no-op for other threads.
|
|
|
|
outFile->mNextToRelease = mToReleaseFile;
|
|
|
|
mToReleaseFile = outFile;
|
|
|
|
|
|
|
|
mOutFile = OpenFile(false, nextFileNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--mPrintEntryCount == 0 && mToReleaseFile) {
|
|
|
|
// We were the last Print() entered, if there is a file to release
|
|
|
|
// do it now. exchange() is atomic and makes sure we release the file
|
|
|
|
// only once on one thread.
|
|
|
|
detail::LogFile* release = mToReleaseFile.exchange(nullptr);
|
|
|
|
delete release;
|
|
|
|
}
|
2016-01-06 00:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
private:
|
|
|
|
OffTheBooksMutex mModulesLock;
|
|
|
|
nsClassHashtable<nsCharPtrHashKey, LogModule> mModules;
|
2016-07-29 13:51:00 +03:00
|
|
|
|
|
|
|
// Print() entry counter, actually reflects concurrent use of the current
|
|
|
|
// output file. ReleaseAcquire ensures that manipulation with mOutFile
|
|
|
|
// and mToReleaseFile is synchronized by manipulation with this value.
|
|
|
|
Atomic<uint32_t, ReleaseAcquire> mPrintEntryCount;
|
|
|
|
// File to write to. ReleaseAcquire because we need to sync mToReleaseFile
|
|
|
|
// with this.
|
|
|
|
Atomic<detail::LogFile*, ReleaseAcquire> mOutFile;
|
|
|
|
// File to be released when reference counter drops to zero. This member
|
|
|
|
// is assigned mOutFile when the current file has reached the limit.
|
|
|
|
// It can be Relaxed, since it's synchronized with mPrintEntryCount
|
|
|
|
// manipulation and we do atomic exchange() on it.
|
|
|
|
Atomic<detail::LogFile*, Relaxed> mToReleaseFile;
|
|
|
|
// The next file number. This is mostly only for synchronization sake.
|
|
|
|
// Can have relaxed ordering, since we only do compareExchange on it which
|
|
|
|
// is atomic regardless ordering.
|
|
|
|
Atomic<uint32_t, Relaxed> mOutFileNum;
|
|
|
|
// Just keeps the actual file path for further use.
|
|
|
|
UniqueFreePtr<char[]> mOutFilePath;
|
|
|
|
|
2016-02-02 09:21:00 +03:00
|
|
|
PRThread *mMainThread;
|
2016-09-19 18:39:47 +03:00
|
|
|
bool mSetFromEnv;
|
|
|
|
Atomic<bool, Relaxed> mAddTimestamp;
|
|
|
|
Atomic<bool, Relaxed> mIsSync;
|
2016-07-29 13:51:00 +03:00
|
|
|
int32_t mRotate;
|
2015-10-19 22:22:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
StaticAutoPtr<LogModuleManager> sLogModuleManager;
|
|
|
|
|
|
|
|
LogModule*
|
|
|
|
LogModule::Get(const char* aName)
|
|
|
|
{
|
|
|
|
// This is just a pass through to the LogModuleManager so
|
|
|
|
// that the LogModuleManager implementation can be kept internal.
|
|
|
|
MOZ_ASSERT(sLogModuleManager != nullptr);
|
|
|
|
return sLogModuleManager->CreateOrGetModule(aName);
|
|
|
|
}
|
|
|
|
|
2016-09-19 18:39:47 +03:00
|
|
|
void
|
|
|
|
LogModule::SetLogFile(const char* aFilename)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sLogModuleManager);
|
|
|
|
sLogModuleManager->SetLogFile(aFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
LogModule::GetLogFile(char *aBuffer, size_t aLength)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sLogModuleManager);
|
|
|
|
return sLogModuleManager->GetLogFile(aBuffer, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LogModule::SetAddTimestamp(bool aAddTimestamp)
|
|
|
|
{
|
|
|
|
sLogModuleManager->SetAddTimestamp(aAddTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LogModule::SetIsSync(bool aIsSync)
|
|
|
|
{
|
|
|
|
sLogModuleManager->SetIsSync(aIsSync);
|
|
|
|
}
|
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
void
|
|
|
|
LogModule::Init()
|
|
|
|
{
|
|
|
|
// NB: This method is not threadsafe; it is expected to be called very early
|
|
|
|
// in startup prior to any other threads being run.
|
|
|
|
if (sLogModuleManager) {
|
|
|
|
// Already initialized.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: We intentionally do not register for ClearOnShutdown as that happens
|
|
|
|
// before all logging is complete. And, yes, that means we leak, but
|
|
|
|
// we're doing that intentionally.
|
|
|
|
sLogModuleManager = new LogModuleManager();
|
2016-01-05 23:16:03 +03:00
|
|
|
sLogModuleManager->Init();
|
2015-10-19 22:22:11 +03:00
|
|
|
}
|
|
|
|
|
2016-01-06 00:05:19 +03:00
|
|
|
void
|
|
|
|
LogModule::Printv(LogLevel aLevel, const char* aFmt, va_list aArgs) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sLogModuleManager != nullptr);
|
|
|
|
|
|
|
|
// Forward to LogModule manager w/ level and name
|
|
|
|
sLogModuleManager->Print(Name(), aLevel, aFmt, aArgs);
|
|
|
|
}
|
|
|
|
|
2015-10-19 22:22:11 +03:00
|
|
|
} // namespace mozilla
|