js/xpconnect/src/XPCLog.cpp

branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
equal deleted inserted replaced
-1:000000000000 0:8618e1383c58
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */
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 "prlog.h"
11 #include "prprf.h"
12 #include "mozilla/mozalloc.h"
13 #include "mozilla/NullPtr.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 PRLogModuleInfo* g_LogMod = nullptr;
30
31 static bool Init()
32 {
33 g_LogMod = PR_NewLogModule("xpclog");
34 g_Spaces = new char[SPACE_COUNT+1];
35 if (!g_LogMod || !g_Spaces || !PR_LOG_TEST(g_LogMod,1)) {
36 g_InitState = 1;
37 XPC_Log_Finish();
38 return false;
39 }
40 memset(g_Spaces, ' ', SPACE_COUNT);
41 g_Spaces[SPACE_COUNT] = 0;
42 g_InitState = 1;
43 return true;
44 }
45
46 void
47 XPC_Log_Finish()
48 {
49 if (g_InitState == 1) {
50 delete [] g_Spaces;
51 // we'd like to properly cleanup the LogModule, but nspr owns that
52 g_LogMod = nullptr;
53 }
54 g_InitState = -1;
55 }
56
57 void
58 XPC_Log_print(const char *fmt, ...)
59 {
60 va_list ap;
61 char line[LINE_LEN];
62
63 va_start(ap, fmt);
64 PR_vsnprintf(line, sizeof(line)-1, fmt, ap);
65 va_end(ap);
66 if (g_Indent)
67 PR_LogPrint("%s%s",g_Spaces+SPACE_COUNT-(INDENT_FACTOR*g_Indent),line);
68 else
69 PR_LogPrint("%s",line);
70 }
71
72 bool
73 XPC_Log_Check(int i)
74 {
75 return CAN_RUN && PR_LOG_TEST(g_LogMod,1);
76 }
77
78 void
79 XPC_Log_Indent()
80 {
81 if (INDENT_FACTOR*(++g_Indent) > SPACE_COUNT)
82 g_Indent-- ;
83 }
84
85 void
86 XPC_Log_Outdent()
87 {
88 if (--g_Indent < 0)
89 g_Indent++;
90 }
91
92 void
93 XPC_Log_Clear_Indent()
94 {
95 g_Indent = 0;
96 }
97
98 #endif

mercurial