Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* Debug Logging support. */ |
michael@0 | 8 | |
michael@0 | 9 | #include "XPCLog.h" |
michael@0 | 10 | #include "prlog.h" |
michael@0 | 11 | #include "prprf.h" |
michael@0 | 12 | #include "mozilla/mozalloc.h" |
michael@0 | 13 | #include "mozilla/NullPtr.h" |
michael@0 | 14 | #include <string.h> |
michael@0 | 15 | #include <stdarg.h> |
michael@0 | 16 | |
michael@0 | 17 | // this all only works for DEBUG... |
michael@0 | 18 | #ifdef DEBUG |
michael@0 | 19 | |
michael@0 | 20 | #define SPACE_COUNT 200 |
michael@0 | 21 | #define LINE_LEN 200 |
michael@0 | 22 | #define INDENT_FACTOR 2 |
michael@0 | 23 | |
michael@0 | 24 | #define CAN_RUN (g_InitState == 1 || (g_InitState == 0 && Init())) |
michael@0 | 25 | |
michael@0 | 26 | static char* g_Spaces; |
michael@0 | 27 | static int g_InitState = 0; |
michael@0 | 28 | static int g_Indent = 0; |
michael@0 | 29 | static PRLogModuleInfo* g_LogMod = nullptr; |
michael@0 | 30 | |
michael@0 | 31 | static bool Init() |
michael@0 | 32 | { |
michael@0 | 33 | g_LogMod = PR_NewLogModule("xpclog"); |
michael@0 | 34 | g_Spaces = new char[SPACE_COUNT+1]; |
michael@0 | 35 | if (!g_LogMod || !g_Spaces || !PR_LOG_TEST(g_LogMod,1)) { |
michael@0 | 36 | g_InitState = 1; |
michael@0 | 37 | XPC_Log_Finish(); |
michael@0 | 38 | return false; |
michael@0 | 39 | } |
michael@0 | 40 | memset(g_Spaces, ' ', SPACE_COUNT); |
michael@0 | 41 | g_Spaces[SPACE_COUNT] = 0; |
michael@0 | 42 | g_InitState = 1; |
michael@0 | 43 | return true; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | void |
michael@0 | 47 | XPC_Log_Finish() |
michael@0 | 48 | { |
michael@0 | 49 | if (g_InitState == 1) { |
michael@0 | 50 | delete [] g_Spaces; |
michael@0 | 51 | // we'd like to properly cleanup the LogModule, but nspr owns that |
michael@0 | 52 | g_LogMod = nullptr; |
michael@0 | 53 | } |
michael@0 | 54 | g_InitState = -1; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | void |
michael@0 | 58 | XPC_Log_print(const char *fmt, ...) |
michael@0 | 59 | { |
michael@0 | 60 | va_list ap; |
michael@0 | 61 | char line[LINE_LEN]; |
michael@0 | 62 | |
michael@0 | 63 | va_start(ap, fmt); |
michael@0 | 64 | PR_vsnprintf(line, sizeof(line)-1, fmt, ap); |
michael@0 | 65 | va_end(ap); |
michael@0 | 66 | if (g_Indent) |
michael@0 | 67 | PR_LogPrint("%s%s",g_Spaces+SPACE_COUNT-(INDENT_FACTOR*g_Indent),line); |
michael@0 | 68 | else |
michael@0 | 69 | PR_LogPrint("%s",line); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | bool |
michael@0 | 73 | XPC_Log_Check(int i) |
michael@0 | 74 | { |
michael@0 | 75 | return CAN_RUN && PR_LOG_TEST(g_LogMod,1); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | void |
michael@0 | 79 | XPC_Log_Indent() |
michael@0 | 80 | { |
michael@0 | 81 | if (INDENT_FACTOR*(++g_Indent) > SPACE_COUNT) |
michael@0 | 82 | g_Indent-- ; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void |
michael@0 | 86 | XPC_Log_Outdent() |
michael@0 | 87 | { |
michael@0 | 88 | if (--g_Indent < 0) |
michael@0 | 89 | g_Indent++; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void |
michael@0 | 93 | XPC_Log_Clear_Indent() |
michael@0 | 94 | { |
michael@0 | 95 | g_Indent = 0; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | #endif |