toolkit/xre/nsConsoleWriter.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:6152e95291a3
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "nsAppRunner.h"
6
7 #include "prio.h"
8 #include "prprf.h"
9 #include "prenv.h"
10
11 #include "nsCRT.h"
12 #include "nsNativeCharsetUtils.h"
13 #include "nsString.h"
14 #include "nsXREDirProvider.h"
15 #include "nsXULAppAPI.h"
16
17 #include "nsIConsoleService.h"
18 #include "nsIConsoleMessage.h"
19
20 void
21 WriteConsoleLog()
22 {
23 nsresult rv;
24
25 nsCOMPtr<nsIFile> lfile;
26
27 char* logFileEnv = PR_GetEnv("XRE_CONSOLE_LOG");
28 if (logFileEnv && *logFileEnv) {
29 rv = XRE_GetFileFromPath(logFileEnv, getter_AddRefs(lfile));
30 if (NS_FAILED(rv))
31 return;
32 }
33 else {
34 if (!gLogConsoleErrors)
35 return;
36
37 rv = gDirServiceProvider->GetUserAppDataDirectory(getter_AddRefs(lfile));
38 if (NS_FAILED(rv))
39 return;
40
41 lfile->AppendNative(NS_LITERAL_CSTRING("console.log"));
42 }
43
44 PRFileDesc *file;
45 rv = lfile->OpenNSPRFileDesc(PR_WRONLY | PR_APPEND | PR_CREATE_FILE,
46 0660, &file);
47 if (NS_FAILED(rv))
48 return;
49
50 nsCOMPtr<nsIConsoleService> csrv
51 (do_GetService(NS_CONSOLESERVICE_CONTRACTID));
52 if (!csrv) {
53 PR_Close(file);
54 return;
55 }
56
57 nsIConsoleMessage** messages;
58 uint32_t mcount;
59
60 rv = csrv->GetMessageArray(&mcount, &messages);
61 if (NS_FAILED(rv)) {
62 PR_Close(file);
63 return;
64 }
65
66 if (mcount) {
67 PRExplodedTime etime;
68 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime);
69 char datetime[512];
70 PR_FormatTimeUSEnglish(datetime, sizeof(datetime),
71 "%Y-%m-%d %H:%M:%S", &etime);
72
73 PR_fprintf(file, NS_LINEBREAK
74 "*** Console log: %s ***" NS_LINEBREAK,
75 datetime);
76 }
77
78 // From this point on, we have to release all the messages, and free
79 // the memory allocated for the messages array. XPCOM arrays suck.
80
81 nsXPIDLString msg;
82 nsAutoCString nativemsg;
83
84 for (uint32_t i = 0; i < mcount; ++i) {
85 rv = messages[i]->GetMessageMoz(getter_Copies(msg));
86 if (NS_SUCCEEDED(rv)) {
87 NS_CopyUnicodeToNative(msg, nativemsg);
88 PR_fprintf(file, "%s" NS_LINEBREAK, nativemsg.get());
89 }
90 NS_IF_RELEASE(messages[i]);
91 }
92
93 PR_Close(file);
94 NS_Free(messages);
95 }

mercurial