Line data Source code
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 0 : WriteConsoleLog()
22 : {
23 : nsresult rv;
24 :
25 0 : nsCOMPtr<nsIFile> lfile;
26 :
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 : }
33 : else {
34 0 : if (!gLogConsoleErrors)
35 0 : return;
36 :
37 0 : rv = gDirServiceProvider->GetUserAppDataDirectory(getter_AddRefs(lfile));
38 0 : if (NS_FAILED(rv))
39 0 : return;
40 :
41 0 : lfile->AppendNative(NS_LITERAL_CSTRING("console.log"));
42 : }
43 :
44 : 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 :
50 : nsCOMPtr<nsIConsoleService> csrv
51 0 : (do_GetService(NS_CONSOLESERVICE_CONTRACTID));
52 0 : if (!csrv) {
53 0 : PR_Close(file);
54 0 : return;
55 : }
56 :
57 : nsIConsoleMessage** messages;
58 : uint32_t mcount;
59 :
60 0 : rv = csrv->GetMessageArray(&mcount, &messages);
61 0 : if (NS_FAILED(rv)) {
62 0 : PR_Close(file);
63 0 : return;
64 : }
65 :
66 0 : if (mcount) {
67 : PRExplodedTime etime;
68 0 : PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime);
69 : char datetime[512];
70 : PR_FormatTimeUSEnglish(datetime, sizeof(datetime),
71 0 : "%Y-%m-%d %H:%M:%S", &etime);
72 :
73 : PR_fprintf(file, NS_LINEBREAK
74 : "*** Console log: %s ***" NS_LINEBREAK,
75 0 : 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 0 : nsXPIDLString msg;
82 0 : nsAutoCString nativemsg;
83 :
84 0 : for (uint32_t i = 0; i < mcount; ++i) {
85 0 : rv = messages[i]->GetMessageMoz(getter_Copies(msg));
86 0 : if (NS_SUCCEEDED(rv)) {
87 0 : NS_CopyUnicodeToNative(msg, nativemsg);
88 0 : PR_fprintf(file, "%s" NS_LINEBREAK, nativemsg.get());
89 : }
90 0 : NS_IF_RELEASE(messages[i]);
91 : }
92 :
93 0 : PR_Close(file);
94 0 : free(messages);
95 : }
|