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/. */
5 #include "nsAppRunner.h"
12 #include "nsNativeCharsetUtils.h"
14 #include "nsXREDirProvider.h"
15 #include "nsXULAppAPI.h"
17 #include "nsIConsoleService.h"
18 #include "nsIConsoleMessage.h"
20 void WriteConsoleLog() {
23 nsCOMPtr
<nsIFile
> lfile
;
25 char* logFileEnv
= PR_GetEnv("XRE_CONSOLE_LOG");
26 if (logFileEnv
&& *logFileEnv
) {
27 rv
= XRE_GetFileFromPath(logFileEnv
, getter_AddRefs(lfile
));
28 if (NS_FAILED(rv
)) return;
30 if (!gLogConsoleErrors
) return;
32 rv
= nsXREDirProvider::GetUserAppDataDirectory(getter_AddRefs(lfile
));
33 if (NS_FAILED(rv
)) return;
35 lfile
->AppendNative("console.log"_ns
);
39 rv
= lfile
->OpenNSPRFileDesc(PR_WRONLY
| PR_APPEND
| PR_CREATE_FILE
, 0660,
41 if (NS_FAILED(rv
)) return;
43 nsCOMPtr
<nsIConsoleService
> csrv(do_GetService(NS_CONSOLESERVICE_CONTRACTID
));
49 nsTArray
<RefPtr
<nsIConsoleMessage
>> messages
;
51 rv
= csrv
->GetMessageArray(messages
);
57 if (!messages
.IsEmpty()) {
59 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters
, &etime
);
61 PR_FormatTimeUSEnglish(datetime
, sizeof(datetime
), "%Y-%m-%d %H:%M:%S",
64 PR_fprintf(file
, NS_LINEBREAK
"*** Console log: %s ***" NS_LINEBREAK
,
69 nsAutoCString nativemsg
;
71 for (auto& message
: messages
) {
72 rv
= message
->GetMessageMoz(msg
);
73 if (NS_SUCCEEDED(rv
)) {
74 NS_CopyUnicodeToNative(msg
, nativemsg
);
75 PR_fprintf(file
, "%s" NS_LINEBREAK
, nativemsg
.get());