XPCLog.cpp (1891B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* Debug Logging support. */ 8 9 #include "XPCLog.h" 10 #include "mozilla/Logging.h" 11 #include "mozilla/Sprintf.h" 12 #include "mozilla/mozalloc.h" 13 #include "prlog.h" 14 #include <string.h> 15 #include <stdarg.h> 16 17 // this all only works for DEBUG... 18 #ifdef DEBUG 19 20 # define SPACE_COUNT 200 21 # define LINE_LEN 200 22 # define INDENT_FACTOR 2 23 24 # define CAN_RUN (g_InitState == 1 || (g_InitState == 0 && Init())) 25 26 static char* g_Spaces; 27 static int g_InitState = 0; 28 static int g_Indent = 0; 29 static mozilla::LazyLogModule g_LogMod("xpclog"); 30 31 static bool Init() { 32 g_Spaces = new char[SPACE_COUNT + 1]; 33 if (!g_Spaces || !MOZ_LOG_TEST(g_LogMod, mozilla::LogLevel::Error)) { 34 g_InitState = 1; 35 XPC_Log_Finish(); 36 return false; 37 } 38 memset(g_Spaces, ' ', SPACE_COUNT); 39 g_Spaces[SPACE_COUNT] = 0; 40 g_InitState = 1; 41 return true; 42 } 43 44 void XPC_Log_Finish() { 45 if (g_InitState == 1) { 46 delete[] g_Spaces; 47 } 48 g_InitState = -1; 49 } 50 51 void XPC_Log_print(const char* fmt, ...) { 52 va_list ap; 53 char line[LINE_LEN]; 54 55 va_start(ap, fmt); 56 VsprintfLiteral(line, fmt, ap); 57 va_end(ap); 58 if (g_Indent) { 59 PR_LogPrint("%s%s", g_Spaces + SPACE_COUNT - (INDENT_FACTOR * g_Indent), 60 line); 61 } else { 62 PR_LogPrint("%s", line); 63 } 64 } 65 66 bool XPC_Log_Check(int i) { 67 return CAN_RUN && MOZ_LOG_TEST(g_LogMod, mozilla::LogLevel::Error); 68 } 69 70 void XPC_Log_Indent() { 71 if (INDENT_FACTOR * (++g_Indent) > SPACE_COUNT) { 72 g_Indent--; 73 } 74 } 75 76 void XPC_Log_Outdent() { 77 if (--g_Indent < 0) { 78 g_Indent++; 79 } 80 } 81 82 void XPC_Log_Clear_Indent() { g_Indent = 0; } 83 84 #endif