michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAppRunner.h" michael@0: michael@0: #include "prio.h" michael@0: #include "prprf.h" michael@0: #include "prenv.h" michael@0: michael@0: #include "nsCRT.h" michael@0: #include "nsNativeCharsetUtils.h" michael@0: #include "nsString.h" michael@0: #include "nsXREDirProvider.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include "nsIConsoleService.h" michael@0: #include "nsIConsoleMessage.h" michael@0: michael@0: void michael@0: WriteConsoleLog() michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr lfile; michael@0: michael@0: char* logFileEnv = PR_GetEnv("XRE_CONSOLE_LOG"); michael@0: if (logFileEnv && *logFileEnv) { michael@0: rv = XRE_GetFileFromPath(logFileEnv, getter_AddRefs(lfile)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: } michael@0: else { michael@0: if (!gLogConsoleErrors) michael@0: return; michael@0: michael@0: rv = gDirServiceProvider->GetUserAppDataDirectory(getter_AddRefs(lfile)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: lfile->AppendNative(NS_LITERAL_CSTRING("console.log")); michael@0: } michael@0: michael@0: PRFileDesc *file; michael@0: rv = lfile->OpenNSPRFileDesc(PR_WRONLY | PR_APPEND | PR_CREATE_FILE, michael@0: 0660, &file); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: nsCOMPtr csrv michael@0: (do_GetService(NS_CONSOLESERVICE_CONTRACTID)); michael@0: if (!csrv) { michael@0: PR_Close(file); michael@0: return; michael@0: } michael@0: michael@0: nsIConsoleMessage** messages; michael@0: uint32_t mcount; michael@0: michael@0: rv = csrv->GetMessageArray(&mcount, &messages); michael@0: if (NS_FAILED(rv)) { michael@0: PR_Close(file); michael@0: return; michael@0: } michael@0: michael@0: if (mcount) { michael@0: PRExplodedTime etime; michael@0: PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &etime); michael@0: char datetime[512]; michael@0: PR_FormatTimeUSEnglish(datetime, sizeof(datetime), michael@0: "%Y-%m-%d %H:%M:%S", &etime); michael@0: michael@0: PR_fprintf(file, NS_LINEBREAK michael@0: "*** Console log: %s ***" NS_LINEBREAK, michael@0: datetime); michael@0: } michael@0: michael@0: // From this point on, we have to release all the messages, and free michael@0: // the memory allocated for the messages array. XPCOM arrays suck. michael@0: michael@0: nsXPIDLString msg; michael@0: nsAutoCString nativemsg; michael@0: michael@0: for (uint32_t i = 0; i < mcount; ++i) { michael@0: rv = messages[i]->GetMessageMoz(getter_Copies(msg)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: NS_CopyUnicodeToNative(msg, nativemsg); michael@0: PR_fprintf(file, "%s" NS_LINEBREAK, nativemsg.get()); michael@0: } michael@0: NS_IF_RELEASE(messages[i]); michael@0: } michael@0: michael@0: PR_Close(file); michael@0: NS_Free(messages); michael@0: }