michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=99: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Debug Logging support. */ michael@0: michael@0: #include "XPCLog.h" michael@0: #include "prlog.h" michael@0: #include "prprf.h" michael@0: #include "mozilla/mozalloc.h" michael@0: #include "mozilla/NullPtr.h" michael@0: #include michael@0: #include michael@0: michael@0: // this all only works for DEBUG... michael@0: #ifdef DEBUG michael@0: michael@0: #define SPACE_COUNT 200 michael@0: #define LINE_LEN 200 michael@0: #define INDENT_FACTOR 2 michael@0: michael@0: #define CAN_RUN (g_InitState == 1 || (g_InitState == 0 && Init())) michael@0: michael@0: static char* g_Spaces; michael@0: static int g_InitState = 0; michael@0: static int g_Indent = 0; michael@0: static PRLogModuleInfo* g_LogMod = nullptr; michael@0: michael@0: static bool Init() michael@0: { michael@0: g_LogMod = PR_NewLogModule("xpclog"); michael@0: g_Spaces = new char[SPACE_COUNT+1]; michael@0: if (!g_LogMod || !g_Spaces || !PR_LOG_TEST(g_LogMod,1)) { michael@0: g_InitState = 1; michael@0: XPC_Log_Finish(); michael@0: return false; michael@0: } michael@0: memset(g_Spaces, ' ', SPACE_COUNT); michael@0: g_Spaces[SPACE_COUNT] = 0; michael@0: g_InitState = 1; michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: XPC_Log_Finish() michael@0: { michael@0: if (g_InitState == 1) { michael@0: delete [] g_Spaces; michael@0: // we'd like to properly cleanup the LogModule, but nspr owns that michael@0: g_LogMod = nullptr; michael@0: } michael@0: g_InitState = -1; michael@0: } michael@0: michael@0: void michael@0: XPC_Log_print(const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: char line[LINE_LEN]; michael@0: michael@0: va_start(ap, fmt); michael@0: PR_vsnprintf(line, sizeof(line)-1, fmt, ap); michael@0: va_end(ap); michael@0: if (g_Indent) michael@0: PR_LogPrint("%s%s",g_Spaces+SPACE_COUNT-(INDENT_FACTOR*g_Indent),line); michael@0: else michael@0: PR_LogPrint("%s",line); michael@0: } michael@0: michael@0: bool michael@0: XPC_Log_Check(int i) michael@0: { michael@0: return CAN_RUN && PR_LOG_TEST(g_LogMod,1); michael@0: } michael@0: michael@0: void michael@0: XPC_Log_Indent() michael@0: { michael@0: if (INDENT_FACTOR*(++g_Indent) > SPACE_COUNT) michael@0: g_Indent-- ; michael@0: } michael@0: michael@0: void michael@0: XPC_Log_Outdent() michael@0: { michael@0: if (--g_Indent < 0) michael@0: g_Indent++; michael@0: } michael@0: michael@0: void michael@0: XPC_Log_Clear_Indent() michael@0: { michael@0: g_Indent = 0; michael@0: } michael@0: michael@0: #endif