1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/src/XPCLog.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* Debug Logging support. */ 1.11 + 1.12 +#include "XPCLog.h" 1.13 +#include "prlog.h" 1.14 +#include "prprf.h" 1.15 +#include "mozilla/mozalloc.h" 1.16 +#include "mozilla/NullPtr.h" 1.17 +#include <string.h> 1.18 +#include <stdarg.h> 1.19 + 1.20 +// this all only works for DEBUG... 1.21 +#ifdef DEBUG 1.22 + 1.23 +#define SPACE_COUNT 200 1.24 +#define LINE_LEN 200 1.25 +#define INDENT_FACTOR 2 1.26 + 1.27 +#define CAN_RUN (g_InitState == 1 || (g_InitState == 0 && Init())) 1.28 + 1.29 +static char* g_Spaces; 1.30 +static int g_InitState = 0; 1.31 +static int g_Indent = 0; 1.32 +static PRLogModuleInfo* g_LogMod = nullptr; 1.33 + 1.34 +static bool Init() 1.35 +{ 1.36 + g_LogMod = PR_NewLogModule("xpclog"); 1.37 + g_Spaces = new char[SPACE_COUNT+1]; 1.38 + if (!g_LogMod || !g_Spaces || !PR_LOG_TEST(g_LogMod,1)) { 1.39 + g_InitState = 1; 1.40 + XPC_Log_Finish(); 1.41 + return false; 1.42 + } 1.43 + memset(g_Spaces, ' ', SPACE_COUNT); 1.44 + g_Spaces[SPACE_COUNT] = 0; 1.45 + g_InitState = 1; 1.46 + return true; 1.47 +} 1.48 + 1.49 +void 1.50 +XPC_Log_Finish() 1.51 +{ 1.52 + if (g_InitState == 1) { 1.53 + delete [] g_Spaces; 1.54 + // we'd like to properly cleanup the LogModule, but nspr owns that 1.55 + g_LogMod = nullptr; 1.56 + } 1.57 + g_InitState = -1; 1.58 +} 1.59 + 1.60 +void 1.61 +XPC_Log_print(const char *fmt, ...) 1.62 +{ 1.63 + va_list ap; 1.64 + char line[LINE_LEN]; 1.65 + 1.66 + va_start(ap, fmt); 1.67 + PR_vsnprintf(line, sizeof(line)-1, fmt, ap); 1.68 + va_end(ap); 1.69 + if (g_Indent) 1.70 + PR_LogPrint("%s%s",g_Spaces+SPACE_COUNT-(INDENT_FACTOR*g_Indent),line); 1.71 + else 1.72 + PR_LogPrint("%s",line); 1.73 +} 1.74 + 1.75 +bool 1.76 +XPC_Log_Check(int i) 1.77 +{ 1.78 + return CAN_RUN && PR_LOG_TEST(g_LogMod,1); 1.79 +} 1.80 + 1.81 +void 1.82 +XPC_Log_Indent() 1.83 +{ 1.84 + if (INDENT_FACTOR*(++g_Indent) > SPACE_COUNT) 1.85 + g_Indent-- ; 1.86 +} 1.87 + 1.88 +void 1.89 +XPC_Log_Outdent() 1.90 +{ 1.91 + if (--g_Indent < 0) 1.92 + g_Indent++; 1.93 +} 1.94 + 1.95 +void 1.96 +XPC_Log_Clear_Indent() 1.97 +{ 1.98 + g_Indent = 0; 1.99 +} 1.100 + 1.101 +#endif