WasmLog.cpp (2644B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * 4 * Copyright 2021 Mozilla Foundation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "wasm/WasmLog.h" 20 21 #include <stdio.h> 22 23 #include "jit/JitOptions.h" 24 #include "js/Printf.h" 25 #include "js/Utility.h" 26 #include "vm/JSContext.h" 27 #include "vm/Logging.h" 28 #include "vm/Warnings.h" 29 30 using namespace js; 31 using namespace js::wasm; 32 33 void wasm::Log(JSContext* cx, const char* fmt, ...) { 34 MOZ_ASSERT(!cx->isExceptionPending() || cx->isThrowingOutOfMemory()); 35 36 bool shouldWarn = JS::Prefs::wasm_trace_api(); 37 bool shouldLog = wasmApiModule.shouldLog(LogLevel::Info); 38 39 if (cx->isThrowingOutOfMemory() || (!shouldWarn && !shouldLog)) { 40 return; 41 } 42 43 va_list args; 44 va_start(args, fmt); 45 46 if (UniqueChars chars = JS_vsmprintf(fmt, args)) { 47 if (shouldWarn) { 48 WarnNumberASCII(cx, JSMSG_WASM_VERBOSE, chars.get()); 49 if (cx->isExceptionPending()) { 50 cx->clearPendingException(); 51 } 52 } 53 if (shouldLog) { 54 wasmApiModule.interface.logPrint(wasmApiModule.logger, LogLevel::Info, 55 "%s", chars.get()); 56 } 57 } 58 59 va_end(args); 60 } 61 62 void wasm::LogOffThread(const char* fmt, ...) { 63 if (!wasmApiModule.shouldLog(LogLevel::Info)) { 64 return; 65 } 66 67 va_list args; 68 va_start(args, fmt); 69 if (UniqueChars chars = JS_vsmprintf(fmt, args)) { 70 wasmApiModule.interface.logPrint(wasmApiModule.logger, LogLevel::Info, "%s", 71 chars.get()); 72 } 73 va_end(args); 74 } 75 76 #ifdef WASM_CODEGEN_DEBUG 77 bool wasm::IsCodegenDebugEnabled(DebugChannel channel) { 78 switch (channel) { 79 case DebugChannel::Function: 80 return jit::JitOptions.enableWasmFuncCallSpew; 81 case DebugChannel::Import: 82 return jit::JitOptions.enableWasmImportCallSpew; 83 } 84 return false; 85 } 86 #endif 87 88 void wasm::DebugCodegen(DebugChannel channel, const char* fmt, ...) { 89 #ifdef WASM_CODEGEN_DEBUG 90 if (!IsCodegenDebugEnabled(channel)) { 91 return; 92 } 93 va_list ap; 94 va_start(ap, fmt); 95 vfprintf(stderr, fmt, ap); 96 va_end(ap); 97 #endif 98 }