1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/xre/nsConsoleWriter.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsAppRunner.h" 1.9 + 1.10 +#include "prio.h" 1.11 +#include "prprf.h" 1.12 +#include "prenv.h" 1.13 + 1.14 +#include "nsCRT.h" 1.15 +#include "nsNativeCharsetUtils.h" 1.16 +#include "nsString.h" 1.17 +#include "nsXREDirProvider.h" 1.18 +#include "nsXULAppAPI.h" 1.19 + 1.20 +#include "nsIConsoleService.h" 1.21 +#include "nsIConsoleMessage.h" 1.22 + 1.23 +void 1.24 +WriteConsoleLog() 1.25 +{ 1.26 + nsresult rv; 1.27 + 1.28 + nsCOMPtr<nsIFile> lfile; 1.29 + 1.30 + char* logFileEnv = PR_GetEnv("XRE_CONSOLE_LOG"); 1.31 + if (logFileEnv && *logFileEnv) { 1.32 + rv = XRE_GetFileFromPath(logFileEnv, getter_AddRefs(lfile)); 1.33 + if (NS_FAILED(rv)) 1.34 + return; 1.35 + } 1.36 + else { 1.37 + if (!gLogConsoleErrors) 1.38 + return; 1.39 + 1.40 + rv = gDirServiceProvider->GetUserAppDataDirectory(getter_AddRefs(lfile)); 1.41 + if (NS_FAILED(rv)) 1.42 + return; 1.43 + 1.44 + lfile->AppendNative(NS_LITERAL_CSTRING("console.log")); 1.45 + } 1.46 + 1.47 + PRFileDesc *file; 1.48 + rv = lfile->OpenNSPRFileDesc(PR_WRONLY | PR_APPEND | PR_CREATE_FILE, 1.49 + 0660, &file); 1.50 + if (NS_FAILED(rv)) 1.51 + return; 1.52 + 1.53 + nsCOMPtr<nsIConsoleService> csrv 1.54 + (do_GetService(NS_CONSOLESERVICE_CONTRACTID)); 1.55 + if (!csrv) { 1.56 + PR_Close(file); 1.57 + return; 1.58 + } 1.59 + 1.60 + nsIConsoleMessage** messages; 1.61 + uint32_t mcount; 1.62 + 1.63 + rv = csrv->GetMessageArray(&mcount, &messages); 1.64 + if (NS_FAILED(rv)) { 1.65 + PR_Close(file); 1.66 + return; 1.67 + } 1.68 + 1.69 + if (mcount) { 1.70 + PRExplodedTime etime; 1.71 + PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime); 1.72 + char datetime[512]; 1.73 + PR_FormatTimeUSEnglish(datetime, sizeof(datetime), 1.74 + "%Y-%m-%d %H:%M:%S", &etime); 1.75 + 1.76 + PR_fprintf(file, NS_LINEBREAK 1.77 + "*** Console log: %s ***" NS_LINEBREAK, 1.78 + datetime); 1.79 + } 1.80 + 1.81 + // From this point on, we have to release all the messages, and free 1.82 + // the memory allocated for the messages array. XPCOM arrays suck. 1.83 + 1.84 + nsXPIDLString msg; 1.85 + nsAutoCString nativemsg; 1.86 + 1.87 + for (uint32_t i = 0; i < mcount; ++i) { 1.88 + rv = messages[i]->GetMessageMoz(getter_Copies(msg)); 1.89 + if (NS_SUCCEEDED(rv)) { 1.90 + NS_CopyUnicodeToNative(msg, nativemsg); 1.91 + PR_fprintf(file, "%s" NS_LINEBREAK, nativemsg.get()); 1.92 + } 1.93 + NS_IF_RELEASE(messages[i]); 1.94 + } 1.95 + 1.96 + PR_Close(file); 1.97 + NS_Free(messages); 1.98 +}