Skip to content

Commit 6d84b99

Browse files
authored
Merge pull request #1038 from NativeScript/trifonov/console-log-release
Do not console.logs in release builds
2 parents a56f918 + d5ccf25 commit 6d84b99

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

test-app/runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ endif()
3232

3333
if ( NOT OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD )
3434
# When building in Release mode we do not include the V8 inspector sources
35-
add_definitions(-DINCLUDE_INSPECTOR)
35+
add_definitions(-DAPPLICATION_IN_DEBUG)
3636

3737
set(
3838
INSPECTOR_SOURCES

test-app/runtime/src/main/cpp/Runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "sys/system_properties.h"
2828
#include "ManualInstrumentation.h"
2929

30-
#ifdef INCLUDE_INSPECTOR
30+
#ifdef APPLICATION_IN_DEBUG
3131
#include "JsV8InspectorClient.h"
3232
#endif
3333

@@ -580,7 +580,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
580580
global->DefineOwnProperty(context, ArgConverter::ConvertToV8String(isolate, "self"), global, readOnlyFlags);
581581
}
582582

583-
#ifdef INCLUDE_INSPECTOR
583+
#ifdef APPLICATION_IN_DEBUG
584584
/*
585585
* Attach __inspector object with function callbacks that report to the Chrome DevTools frontend
586586
*/

test-app/runtime/src/main/cpp/console/Console.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ const std::string buildLogString(const v8::FunctionCallbackInfo<v8::Value>& info
158158
}
159159

160160
void Console::assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
161+
if (!isApplicationInDebug) {
162+
return;
163+
}
161164
try {
162165
auto isolate = info.GetIsolate();
163166

@@ -193,6 +196,9 @@ void Console::assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
193196
}
194197

195198
void Console::errorCallback(const v8::FunctionCallbackInfo <v8::Value>& info) {
199+
if (!isApplicationInDebug) {
200+
return;
201+
}
196202
try {
197203
std::string log = buildLogString(info);
198204

@@ -212,6 +218,9 @@ void Console::errorCallback(const v8::FunctionCallbackInfo <v8::Value>& info) {
212218
}
213219

214220
void Console::infoCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
221+
if (!isApplicationInDebug) {
222+
return;
223+
}
215224
try {
216225
std::string log = buildLogString(info);
217226

@@ -231,6 +240,9 @@ void Console::infoCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
231240
}
232241

233242
void Console::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
243+
if (!isApplicationInDebug) {
244+
return;
245+
}
234246
try {
235247
std::string log = buildLogString(info);
236248

@@ -250,6 +262,9 @@ void Console::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
250262
}
251263

252264
void Console::warnCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
265+
if (!isApplicationInDebug) {
266+
return;
267+
}
253268
try {
254269
std::string log = buildLogString(info);
255270

@@ -269,6 +284,9 @@ void Console::warnCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
269284
}
270285

271286
void Console::dirCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
287+
if (!isApplicationInDebug) {
288+
return;
289+
}
272290
try {
273291
auto isolate = info.GetIsolate();
274292
auto context = isolate->GetCurrentContext();
@@ -383,6 +401,9 @@ const std::string buildStacktraceFrameMessage(v8::Local<v8::StackFrame> frame) {
383401
}
384402

385403
void Console::traceCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
404+
if (!isApplicationInDebug) {
405+
return;
406+
}
386407
try {
387408
auto isolate = info.GetIsolate();
388409
std::stringstream ss;
@@ -426,6 +447,9 @@ void Console::traceCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
426447
}
427448

428449
void Console::timeCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
450+
if (!isApplicationInDebug) {
451+
return;
452+
}
429453
try {
430454
auto isolate = info.GetIsolate();
431455

@@ -462,6 +486,9 @@ void Console::timeCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
462486
}
463487

464488
void Console::timeEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
489+
if (!isApplicationInDebug) {
490+
return;
491+
}
465492
try {
466493
auto isolate = info.GetIsolate();
467494

@@ -520,4 +547,10 @@ void Console::timeEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
520547
const char* Console::LOG_TAG = "JS";
521548
std::map<v8::Isolate*, std::map<std::string, double>> Console::s_isolateToConsoleTimersMap;
522549
ConsoleCallback Console::m_callback = nullptr;
550+
551+
#ifdef APPLICATION_IN_DEBUG
552+
bool Console::isApplicationInDebug = true;
553+
#else
554+
bool Console::isApplicationInDebug = false;
555+
#endif
523556
}

test-app/runtime/src/main/cpp/console/Console.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Console {
3232
static const char* LOG_TAG;
3333
static std::map<v8::Isolate*, std::map<std::string, double>> s_isolateToConsoleTimersMap;
3434

35+
static bool isApplicationInDebug;
36+
3537
// heavily inspired by 'createBoundFunctionProperty' of V8's v8-console.h
3638
static void bindFunctionProperty(v8::Local<v8::Context> context,
3739
v8::Local<v8::Object> consoleInstance,

0 commit comments

Comments
 (0)