Skip to content

Commit 7ac389f

Browse files
authored
Merge pull request #1057 from NativeScript/trifonov/add-forceLog
Added forceLog setting to log in release
2 parents ba1fd1d + e2f1020 commit 7ac389f

File tree

8 files changed

+46
-27
lines changed

8 files changed

+46
-27
lines changed

test-app/app/src/main/assets/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"memoryCheckInterval": 10,
88
"freeMemoryRatio": 0.50,
99
"markingMode": "full",
10-
"maxLogcatObjectSize": 1024
10+
"maxLogcatObjectSize": 1024,
11+
"forceLog": false
1112
}
1213
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,17 @@ ObjectManager* Runtime::GetObjectManager() const {
148148
return m_objectManager;
149149
}
150150

151-
void Runtime::Init(JNIEnv* _env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize) {
151+
void Runtime::Init(JNIEnv* _env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize, bool forceLog) {
152152
JEnv env(_env);
153153

154154
auto runtime = new Runtime(env, obj, runtimeId);
155155

156156
auto enableLog = verboseLoggingEnabled == JNI_TRUE;
157157

158-
runtime->Init(filesPath, nativeLibDir, enableLog, isDebuggable, packageName, args, callingDir, maxLogcatObjectSize);
158+
runtime->Init(filesPath, nativeLibDir, enableLog, isDebuggable, packageName, args, callingDir, maxLogcatObjectSize, forceLog);
159159
}
160160

161-
void Runtime::Init(jstring filesPath, jstring nativeLibDir, bool verboseLoggingEnabled, bool isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize) {
161+
void Runtime::Init(jstring filesPath, jstring nativeLibDir, bool verboseLoggingEnabled, bool isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize, bool forceLog) {
162162
LogEnabled = verboseLoggingEnabled;
163163

164164
auto filesRoot = ArgConverter::jstringToString(filesPath);
@@ -183,7 +183,7 @@ void Runtime::Init(jstring filesPath, jstring nativeLibDir, bool verboseLoggingE
183183
auto profilerOutputDirStr = ArgConverter::jstringToString(profilerOutputDir);
184184

185185
NativeScriptException::Init(m_objectManager);
186-
m_isolate = PrepareV8Runtime(filesRoot, nativeLibDirStr, packageNameStr, isDebuggable, callingDirStr, profilerOutputDirStr, maxLogcatObjectSize);
186+
m_isolate = PrepareV8Runtime(filesRoot, nativeLibDirStr, packageNameStr, isDebuggable, callingDirStr, profilerOutputDirStr, maxLogcatObjectSize, forceLog);
187187

188188
s_isolate2RuntimesCache.insert(make_pair(m_isolate, this));
189189
}
@@ -408,7 +408,7 @@ static void InitializeV8() {
408408
V8::Initialize();
409409
}
410410

411-
Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& nativeLibDir, const string& packageName, bool isDebuggable, const string& callingDir, const string& profilerOutputDir, const int maxLogcatObjectSize) {
411+
Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& nativeLibDir, const string& packageName, bool isDebuggable, const string& callingDir, const string& profilerOutputDir, const int maxLogcatObjectSize, const bool forceLog) {
412412
tns::instrumentation::Frame frame("Runtime.PrepareV8Runtime");
413413

414414
Isolate::CreateParams create_params;
@@ -611,9 +611,9 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
611611
}
612612

613613
#ifdef APPLICATION_IN_DEBUG
614-
v8::Local<v8::Object> console = Console::createConsole(context, JsV8InspectorClient::consoleLogCallback, maxLogcatObjectSize);
614+
v8::Local<v8::Object> console = Console::createConsole(context, JsV8InspectorClient::consoleLogCallback, maxLogcatObjectSize, forceLog);
615615
#else
616-
v8::Local<v8::Object> console = Console::createConsole(context, nullptr, maxLogcatObjectSize);
616+
v8::Local<v8::Object> console = Console::createConsole(context, nullptr, maxLogcatObjectSize, forceLog);
617617
#endif
618618

619619
/*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ class Runtime {
2929

3030
static void Init(JavaVM* vm, void* reserved);
3131

32-
static void Init(JNIEnv* _env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibsDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize);
32+
static void Init(JNIEnv* _env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibsDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize,
33+
bool forceLog);
3334

3435
static void SetManualInstrumentationMode(jstring mode);
3536

36-
void Init(jstring filesPath, jstring nativeLibsDir, bool verboseLoggingEnabled, bool isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize);
37+
void Init(jstring filesPath, jstring nativeLibsDir, bool verboseLoggingEnabled, bool isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize, bool forceLog);
3738

3839
v8::Isolate* GetIsolate() const;
3940

@@ -83,7 +84,7 @@ class Runtime {
8384
v8::Persistent<v8::Function>* m_gcFunc;
8485
volatile bool m_runGC;
8586

86-
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, const std::string& nativeLibsDir, const std::string& packageName, bool isDebuggable, const std::string& callingDir, const std::string& profilerOutputDir, const int maxLogcatObjectSize);
87+
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, const std::string& nativeLibsDir, const std::string& packageName, bool isDebuggable, const std::string& callingDir, const std::string& profilerOutputDir, const int maxLogcatObjectSize, const bool forceLog);
8788
jobject ConvertJsValueToJavaObject(JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);
8889
static int GetAndroidVersion();
8990
static int m_androidVersion;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ extern "C" JNIEXPORT void Java_com_tns_Runtime_SetManualInstrumentationMode(JNIE
3434
}
3535
}
3636

37-
extern "C" JNIEXPORT void Java_com_tns_Runtime_initNativeScript(JNIEnv* _env, jobject obj, jint runtimeId, jstring filesPath, jstring nativeLibDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, jint maxLogcatObjectSize) {
37+
extern "C" JNIEXPORT void Java_com_tns_Runtime_initNativeScript(JNIEnv* _env, jobject obj, jint runtimeId, jstring filesPath, jstring nativeLibDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, jint maxLogcatObjectSize, jboolean forceLog) {
3838
try {
39-
Runtime::Init(_env, obj, runtimeId, filesPath, nativeLibDir, verboseLoggingEnabled, isDebuggable, packageName, args, callingDir, maxLogcatObjectSize);
39+
Runtime::Init(_env, obj, runtimeId, filesPath, nativeLibDir, verboseLoggingEnabled, isDebuggable, packageName, args, callingDir, maxLogcatObjectSize, forceLog);
4040
} catch (NativeScriptException& e) {
4141
e.ReThrowToJava();
4242
} catch (std::exception e) {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
namespace tns {
1717

18-
v8::Local<v8::Object> Console::createConsole(v8::Local<v8::Context> context, ConsoleCallback callback, const int maxLogcatObjectSize) {
18+
v8::Local<v8::Object> Console::createConsole(v8::Local<v8::Context> context, ConsoleCallback callback, const int maxLogcatObjectSize, const bool forceLog) {
1919
m_callback = callback;
2020
m_maxLogcatObjectSize = maxLogcatObjectSize;
21+
m_forceLog = forceLog;
2122
v8::Context::Scope contextScope(context);
2223
v8::Isolate* isolate = context->GetIsolate();
2324

@@ -165,7 +166,7 @@ const std::string buildLogString(const v8::FunctionCallbackInfo<v8::Value>& info
165166
}
166167

167168
void Console::assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
168-
if (!isApplicationInDebug) {
169+
if (!shouldLog()) {
169170
return;
170171
}
171172
try {
@@ -203,7 +204,7 @@ void Console::assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
203204
}
204205

205206
void Console::errorCallback(const v8::FunctionCallbackInfo <v8::Value>& info) {
206-
if (!isApplicationInDebug) {
207+
if (!shouldLog()) {
207208
return;
208209
}
209210
try {
@@ -225,7 +226,7 @@ void Console::errorCallback(const v8::FunctionCallbackInfo <v8::Value>& info) {
225226
}
226227

227228
void Console::infoCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
228-
if (!isApplicationInDebug) {
229+
if (!shouldLog()) {
229230
return;
230231
}
231232
try {
@@ -247,7 +248,7 @@ void Console::infoCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
247248
}
248249

249250
void Console::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
250-
if (!isApplicationInDebug) {
251+
if (!shouldLog()) {
251252
return;
252253
}
253254
try {
@@ -269,7 +270,7 @@ void Console::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
269270
}
270271

271272
void Console::warnCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
272-
if (!isApplicationInDebug) {
273+
if (!shouldLog()) {
273274
return;
274275
}
275276
try {
@@ -291,7 +292,7 @@ void Console::warnCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
291292
}
292293

293294
void Console::dirCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
294-
if (!isApplicationInDebug) {
295+
if (!shouldLog()) {
295296
return;
296297
}
297298
try {
@@ -408,7 +409,7 @@ const std::string buildStacktraceFrameMessage(v8::Local<v8::StackFrame> frame) {
408409
}
409410

410411
void Console::traceCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
411-
if (!isApplicationInDebug) {
412+
if (!shouldLog()) {
412413
return;
413414
}
414415
try {
@@ -454,7 +455,7 @@ void Console::traceCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
454455
}
455456

456457
void Console::timeCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
457-
if (!isApplicationInDebug) {
458+
if (!shouldLog()) {
458459
return;
459460
}
460461
try {
@@ -493,7 +494,7 @@ void Console::timeCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
493494
}
494495

495496
void Console::timeEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
496-
if (!isApplicationInDebug) {
497+
if (!shouldLog()) {
497498
return;
498499
}
499500
try {
@@ -555,6 +556,7 @@ const char* Console::LOG_TAG = "JS";
555556
std::map<v8::Isolate*, std::map<std::string, double>> Console::s_isolateToConsoleTimersMap;
556557
ConsoleCallback Console::m_callback = nullptr;
557558
int Console::m_maxLogcatObjectSize;
559+
bool Console::m_forceLog;
558560

559561
#ifdef APPLICATION_IN_DEBUG
560562
bool Console::isApplicationInDebug = true;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef void (*ConsoleCallback)(const std::string& message, const std::string& l
1515

1616
class Console {
1717
public:
18-
static v8::Local<v8::Object> createConsole(v8::Local<v8::Context> context, ConsoleCallback callback, const int maxLogcatObjectSize);
18+
static v8::Local<v8::Object> createConsole(v8::Local<v8::Context> context, ConsoleCallback callback, const int maxLogcatObjectSize, const bool forceLog);
1919

2020
static void assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
2121
static void errorCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
@@ -29,11 +29,15 @@ class Console {
2929

3030
private:
3131
static int m_maxLogcatObjectSize;
32+
static bool m_forceLog;
3233
static ConsoleCallback m_callback;
3334
static const char* LOG_TAG;
3435
static std::map<v8::Isolate*, std::map<std::string, double>> s_isolateToConsoleTimersMap;
3536

3637
static bool isApplicationInDebug;
38+
static bool shouldLog() {
39+
return m_forceLog || isApplicationInDebug;
40+
}
3741

3842
// heavily inspired by 'createBoundFunctionProperty' of V8's v8-console.h
3943
static void bindFunctionProperty(v8::Local<v8::Context> context,

test-app/runtime/src/main/java/com/tns/AppConfig.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ protected enum KnownKeys {
1818
Profiling("profiling", ""),
1919
MarkingMode("markingMode", com.tns.MarkingMode.full),
2020
HandleTimeZoneChanges("handleTimeZoneChanges", false),
21-
MaxLogcatObjectSize("maxLogcatObjectSize", 1024);
21+
MaxLogcatObjectSize("maxLogcatObjectSize", 1024),
22+
ForceLog("forceLog", false);
2223

2324
private final String name;
2425
private final Object defaultValue;
@@ -108,6 +109,9 @@ public AppConfig(File appDir) {
108109
if (androidObject.has(KnownKeys.MaxLogcatObjectSize.getName())) {
109110
values[KnownKeys.MaxLogcatObjectSize.ordinal()] = androidObject.getInt(KnownKeys.MaxLogcatObjectSize.getName());
110111
}
112+
if (androidObject.has(KnownKeys.ForceLog.getName())) {
113+
values[KnownKeys.ForceLog.ordinal()] = androidObject.getBoolean(KnownKeys.ForceLog.getName());
114+
}
111115
}
112116
}
113117
} catch (Exception e) {
@@ -154,4 +158,8 @@ public boolean handleTimeZoneChanges() {
154158
public int getMaxLogcatObjectSize() {
155159
return (int)values[KnownKeys.MaxLogcatObjectSize.ordinal()];
156160
}
161+
162+
public boolean getForceLog() {
163+
return (boolean)values[KnownKeys.ForceLog.ordinal()];
164+
}
157165
}

test-app/runtime/src/main/java/com/tns/Runtime.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.tns.bindings.ProxyGenerator;
3333

3434
public class Runtime {
35-
private native void initNativeScript(int runtimeId, String filesPath, String nativeLibDir, boolean verboseLoggingEnabled, boolean isDebuggable, String packageName, Object[] v8Options, String callingDir, int maxLogcatObjectSize);
35+
private native void initNativeScript(int runtimeId, String filesPath, String nativeLibDir, boolean verboseLoggingEnabled, boolean isDebuggable, String packageName, Object[] v8Options, String callingDir, int maxLogcatObjectSize, boolean forceLog);
3636

3737
private native void runModule(int runtimeId, String filePath) throws NativeScriptException;
3838

@@ -490,7 +490,10 @@ private void init(Logger logger, String appName, String nativeLibDir, File rootD
490490
throw new RuntimeException("Fail to initialize Require class", ex);
491491
}
492492

493-
initNativeScript(getRuntimeId(), Module.getApplicationFilesPath(), nativeLibDir, logger.isEnabled(), isDebuggable, appName, appConfig.getAsArray(), callingJsDir, appConfig.getMaxLogcatObjectSize());
493+
boolean forceConsoleLog = appConfig.getForceLog() || "timeline".equalsIgnoreCase(appConfig.getProfilingMode());
494+
495+
initNativeScript(getRuntimeId(), Module.getApplicationFilesPath(), nativeLibDir, logger.isEnabled(), isDebuggable, appName, appConfig.getAsArray(),
496+
callingJsDir, appConfig.getMaxLogcatObjectSize(), forceConsoleLog);
494497

495498
//clearStartupData(getRuntimeId()); // It's safe to delete the data after the V8 debugger is initialized
496499

0 commit comments

Comments
 (0)