Bug 1865172 Part 1 - Always store a page name value when a breakpoint is first found...
[gecko.git] / toolkit / xre / nsConsoleWriter.cpp
blobd89ea3bde31dac9d45d77c388d52e83d4b37d053
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"
7 #include "prio.h"
8 #include "prprf.h"
9 #include "prenv.h"
11 #include "nsCRT.h"
12 #include "nsNativeCharsetUtils.h"
13 #include "nsString.h"
14 #include "nsXREDirProvider.h"
15 #include "nsXULAppAPI.h"
17 #include "nsIConsoleService.h"
18 #include "nsIConsoleMessage.h"
20 void WriteConsoleLog() {
21 nsresult rv;
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;
29 } else {
30 if (!gLogConsoleErrors) return;
32 rv = nsXREDirProvider::GetUserAppDataDirectory(getter_AddRefs(lfile));
33 if (NS_FAILED(rv)) return;
35 lfile->AppendNative("console.log"_ns);
38 PRFileDesc* file;
39 rv = lfile->OpenNSPRFileDesc(PR_WRONLY | PR_APPEND | PR_CREATE_FILE, 0660,
40 &file);
41 if (NS_FAILED(rv)) return;
43 nsCOMPtr<nsIConsoleService> csrv(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
44 if (!csrv) {
45 PR_Close(file);
46 return;
49 nsTArray<RefPtr<nsIConsoleMessage>> messages;
51 rv = csrv->GetMessageArray(messages);
52 if (NS_FAILED(rv)) {
53 PR_Close(file);
54 return;
57 if (!messages.IsEmpty()) {
58 PRExplodedTime etime;
59 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime);
60 char datetime[512];
61 PR_FormatTimeUSEnglish(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S",
62 &etime);
64 PR_fprintf(file, NS_LINEBREAK "*** Console log: %s ***" NS_LINEBREAK,
65 datetime);
68 nsString msg;
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());
79 PR_Close(file);