toolkit/xre/nsConsoleWriter.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial