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.

     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
    21 WriteConsoleLog()
    22 {
    23   nsresult rv;
    25   nsCOMPtr<nsIFile> lfile;
    27   char* logFileEnv = PR_GetEnv("XRE_CONSOLE_LOG");
    28   if (logFileEnv && *logFileEnv) {
    29     rv = XRE_GetFileFromPath(logFileEnv, getter_AddRefs(lfile));
    30     if (NS_FAILED(rv))
    31       return;
    32   }
    33   else {
    34     if (!gLogConsoleErrors)
    35       return;
    37     rv = gDirServiceProvider->GetUserAppDataDirectory(getter_AddRefs(lfile));
    38     if (NS_FAILED(rv))
    39       return;
    41     lfile->AppendNative(NS_LITERAL_CSTRING("console.log"));
    42   }
    44   PRFileDesc *file;
    45   rv = lfile->OpenNSPRFileDesc(PR_WRONLY | PR_APPEND | PR_CREATE_FILE,
    46                                0660, &file);
    47   if (NS_FAILED(rv))
    48     return;
    50   nsCOMPtr<nsIConsoleService> csrv
    51     (do_GetService(NS_CONSOLESERVICE_CONTRACTID));
    52   if (!csrv) {
    53     PR_Close(file);
    54     return;
    55   }
    57   nsIConsoleMessage** messages;
    58   uint32_t mcount;
    60   rv = csrv->GetMessageArray(&mcount, &messages);
    61   if (NS_FAILED(rv)) {
    62     PR_Close(file);
    63     return;
    64   }
    66   if (mcount) {
    67     PRExplodedTime etime;
    68     PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime);
    69     char datetime[512];
    70     PR_FormatTimeUSEnglish(datetime, sizeof(datetime),
    71                            "%Y-%m-%d %H:%M:%S", &etime);
    73     PR_fprintf(file, NS_LINEBREAK
    74                      "*** Console log: %s ***" NS_LINEBREAK,
    75                datetime);
    76   }
    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.
    81   nsXPIDLString msg;
    82   nsAutoCString nativemsg;
    84   for (uint32_t i = 0; i < mcount; ++i) {
    85     rv = messages[i]->GetMessageMoz(getter_Copies(msg));
    86     if (NS_SUCCEEDED(rv)) {
    87       NS_CopyUnicodeToNative(msg, nativemsg);
    88       PR_fprintf(file, "%s" NS_LINEBREAK, nativemsg.get());
    89     }
    90     NS_IF_RELEASE(messages[i]);
    91   }
    93   PR_Close(file);
    94   NS_Free(messages);
    95 }

mercurial