/src/mozilla-central/toolkit/xre/nsConsoleWriter.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
23 | 0 | nsresult rv; |
24 | 0 |
|
25 | 0 | nsCOMPtr<nsIFile> lfile; |
26 | 0 |
|
27 | 0 | char* logFileEnv = PR_GetEnv("XRE_CONSOLE_LOG"); |
28 | 0 | if (logFileEnv && *logFileEnv) { |
29 | 0 | rv = XRE_GetFileFromPath(logFileEnv, getter_AddRefs(lfile)); |
30 | 0 | if (NS_FAILED(rv)) |
31 | 0 | return; |
32 | 0 | } |
33 | 0 | else { |
34 | 0 | if (!gLogConsoleErrors) |
35 | 0 | return; |
36 | 0 | |
37 | 0 | rv = nsXREDirProvider::GetUserAppDataDirectory(getter_AddRefs(lfile)); |
38 | 0 | if (NS_FAILED(rv)) |
39 | 0 | return; |
40 | 0 | |
41 | 0 | lfile->AppendNative(NS_LITERAL_CSTRING("console.log")); |
42 | 0 | } |
43 | 0 |
|
44 | 0 | PRFileDesc *file; |
45 | 0 | rv = lfile->OpenNSPRFileDesc(PR_WRONLY | PR_APPEND | PR_CREATE_FILE, |
46 | 0 | 0660, &file); |
47 | 0 | if (NS_FAILED(rv)) |
48 | 0 | return; |
49 | 0 | |
50 | 0 | nsCOMPtr<nsIConsoleService> csrv |
51 | 0 | (do_GetService(NS_CONSOLESERVICE_CONTRACTID)); |
52 | 0 | if (!csrv) { |
53 | 0 | PR_Close(file); |
54 | 0 | return; |
55 | 0 | } |
56 | 0 | |
57 | 0 | nsIConsoleMessage** messages; |
58 | 0 | uint32_t mcount; |
59 | 0 |
|
60 | 0 | rv = csrv->GetMessageArray(&mcount, &messages); |
61 | 0 | if (NS_FAILED(rv)) { |
62 | 0 | PR_Close(file); |
63 | 0 | return; |
64 | 0 | } |
65 | 0 | |
66 | 0 | if (mcount) { |
67 | 0 | PRExplodedTime etime; |
68 | 0 | PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime); |
69 | 0 | char datetime[512]; |
70 | 0 | PR_FormatTimeUSEnglish(datetime, sizeof(datetime), |
71 | 0 | "%Y-%m-%d %H:%M:%S", &etime); |
72 | 0 |
|
73 | 0 | PR_fprintf(file, NS_LINEBREAK |
74 | 0 | "*** Console log: %s ***" NS_LINEBREAK, |
75 | 0 | datetime); |
76 | 0 | } |
77 | 0 |
|
78 | 0 | // From this point on, we have to release all the messages, and free |
79 | 0 | // the memory allocated for the messages array. XPCOM arrays suck. |
80 | 0 |
|
81 | 0 | nsString msg; |
82 | 0 | nsAutoCString nativemsg; |
83 | 0 |
|
84 | 0 | for (uint32_t i = 0; i < mcount; ++i) { |
85 | 0 | rv = messages[i]->GetMessageMoz(msg); |
86 | 0 | if (NS_SUCCEEDED(rv)) { |
87 | 0 | NS_CopyUnicodeToNative(msg, nativemsg); |
88 | 0 | PR_fprintf(file, "%s" NS_LINEBREAK, nativemsg.get()); |
89 | 0 | } |
90 | 0 | NS_IF_RELEASE(messages[i]); |
91 | 0 | } |
92 | 0 |
|
93 | 0 | PR_Close(file); |
94 | 0 | free(messages); |
95 | 0 | } |