Skip to content

Commit 870ee65

Browse files
Debug traces + CB (#90)
Documentation will be part of BabylonNative. 2 levels of logging # Code logging Works are a console log. Enable Debug Trace (off by default) and set an outputter(none set by default) then call Trace. ``` SetTraceOutput([](const char *trace){ printf("%s\n", trace); }); SetDebugTrace(true); ... Trace("Error detected."); ``` # Babylon code instrumentation Babylon code is instrumented and will use Trace function described earlier IF `JSRUNTIMEHOST_DEBUG_TRACE` preprocessor is defined. If it's not (default), no log will happen and no performance impact. To summarize, user can log in his own code with Babylon logging API. Babylon Native and JSRuntimeHost code is instrumented but will only log if `JSRUNTIMEHOST_DEBUG_TRACE` preprocessor is defined and logging is enabled and an outputer function is set. Thread safety in the logging output is responsability of the user.
1 parent 9ccb947 commit 870ee65

File tree

12 files changed

+116
-3
lines changed

12 files changed

+116
-3
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
5656
# General
5757
option(JSRUNTIMEHOST_TESTS "Include JsRuntimeHost Tests." ${PROJECT_IS_TOP_LEVEL})
5858
option(NAPI_BUILD_ABI "Build the ABI layer." ON)
59+
option(BABYLON_DEBUG_TRACE "Debug Trace callback." OFF)
5960

6061
# Core
6162
option(JSRUNTIMEHOST_CORE_APPRUNTIME "Include JsRuntimeHost Core AppRuntime" ON)
@@ -80,6 +81,10 @@ if(JSRUNTIMEHOST_POLYFILL_XMLHTTPREQUEST)
8081
set_property(TARGET UrlLib PROPERTY FOLDER Dependencies)
8182
endif()
8283

84+
if(BABYLON_DEBUG_TRACE)
85+
add_definitions(-DBABYLON_DEBUG_TRACE)
86+
endif()
87+
8388
if(NAPI_JAVASCRIPT_ENGINE STREQUAL "V8" AND JSRUNTIMEHOST_CORE_APPRUNTIME_V8_INSPECTOR)
8489
FetchContent_MakeAvailable_With_Message(asio)
8590
add_library(asio INTERFACE)

Core/Foundation/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
set(SOURCES
2-
"Include/Babylon/Api.h")
2+
"Include/Babylon/Api.h"
3+
"Include/Babylon/DebugTrace.h"
4+
"Source/DebugTrace.cpp")
35

4-
add_library(Foundation INTERFACE)
6+
add_library(Foundation ${SOURCES})
57

68
target_include_directories(Foundation INTERFACE "Include")
79

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include <functional>
3+
4+
#ifdef BABYLON_DEBUG_TRACE
5+
#define _STRINGIFY(x) #x
6+
#define _TOSTRING(x) _STRINGIFY(x)
7+
#define _DEBUG_TRACE(format, ...) Babylon::DebugTrace::Trace(__FILE__ "(" _TOSTRING(__LINE__) "): " format "\n", ##__VA_ARGS__);
8+
9+
#define DEBUG_TRACE _DEBUG_TRACE
10+
#else // No debugging trace enabled
11+
#define DEBUG_TRACE(...)
12+
#endif // BABYLON_DEBUG_TRACE
13+
14+
namespace Babylon
15+
{
16+
namespace DebugTrace
17+
{
18+
// off by default.
19+
void EnableDebugTrace(bool enabled);
20+
// equivalent of a printf that will use TraceOutput function to log. Need Debug Trace to be enabled.
21+
void Trace(const char* format, ...);
22+
// set the function used to output.
23+
void SetTraceOutput(std::function<void(const char* trace)> traceOutputFunction);
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdarg.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <functional>
5+
6+
namespace
7+
{
8+
bool g_debugTraceEnabled{};
9+
std::function<void(const char* output)> g_traceOutputFunction;
10+
void TraceArgs(const char* format, va_list args)
11+
{
12+
char temp[8192];
13+
auto len = vsnprintf(temp, sizeof(temp), format, args);
14+
if (len >= 0 && len < sizeof(temp))
15+
{
16+
temp[len] = '\0';
17+
g_traceOutputFunction(temp);
18+
}
19+
else
20+
{
21+
g_traceOutputFunction("Error while printing trace string.");
22+
}
23+
}
24+
}
25+
26+
namespace Babylon
27+
{
28+
namespace DebugTrace
29+
{
30+
void EnableDebugTrace(bool enabled)
31+
{
32+
g_debugTraceEnabled = enabled;
33+
}
34+
35+
void Trace(const char* format, ...)
36+
{
37+
if (!(g_debugTraceEnabled && g_traceOutputFunction))
38+
{
39+
return;
40+
}
41+
va_list args;
42+
va_start(args, format);
43+
TraceArgs(format, args);
44+
va_end(args);
45+
}
46+
47+
void SetTraceOutput(std::function<void(const char* trace)> traceOutputFunction)
48+
{
49+
g_traceOutputFunction = std::move(traceOutputFunction);
50+
}
51+
}
52+
}

Core/JsRuntime/Source/JsRuntime.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "JsRuntime.h"
2+
#include "Babylon/DebugTrace.h"
23

34
namespace Babylon
45
{
@@ -23,6 +24,8 @@ namespace Babylon
2324

2425
Napi::Value jsRuntime = Napi::External<JsRuntime>::New(env, this, [](Napi::Env, JsRuntime* runtime) { delete runtime; });
2526
jsNative.Set(JS_RUNTIME_NAME, jsRuntime);
27+
28+
DEBUG_TRACE("JsRuntime created");
2629
}
2730

2831
JsRuntime& BABYLON_API JsRuntime::CreateForJavaScript(Napi::Env env, DispatchFunctionT dispatchFunction)

Core/ScriptLoader/Source/ScriptLoader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Babylon/ScriptLoader.h>
22
#include <UrlLib/UrlLib.h>
33
#include <arcana/threading/task.h>
4+
#include "Babylon/DebugTrace.h"
45

56
namespace Babylon
67
{
@@ -16,11 +17,13 @@ namespace Babylon
1617
void LoadScript(std::string url)
1718
{
1819
UrlLib::UrlRequest request;
20+
DEBUG_TRACE("Loading script at url %s", url.c_str());
1921
request.Open(UrlLib::UrlMethod::Get, url);
2022
request.ResponseType(UrlLib::UrlResponseType::String);
2123
m_task = arcana::when_all(m_task, request.SendAsync()).then(arcana::inline_scheduler, arcana::cancellation::none(), [dispatchFunction = m_dispatchFunction, request = std::move(request), url = std::move(url)](auto) mutable {
2224
arcana::task_completion_source<void, std::exception_ptr> taskCompletionSource{};
2325
dispatchFunction([taskCompletionSource, request = std::move(request), url = std::move(url)](Napi::Env env) mutable {
26+
DEBUG_TRACE("Evaluating script at url %s", url.c_str());
2427
Napi::Eval(env, request.ResponseString().data(), url.data());
2528
taskCompletionSource.complete();
2629
});

Tests/UnitTests/Android/app/src/main/cpp/JNI.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <AndroidExtensions/Globals.h>
44
#include <AndroidExtensions/JavaWrappers.h>
55
#include <AndroidExtensions/StdoutLogger.h>
6-
6+
#include "Babylon/DebugTrace.h"
77
#include <Shared/Shared.h>
88

99
extern "C" JNIEXPORT jint JNICALL
@@ -21,6 +21,9 @@ Java_com_jsruntimehost_unittests_Native_javaScriptTests(JNIEnv* env, jclass claz
2121

2222
android::global::Initialize(javaVM, context);
2323

24+
Babylon::DebugTrace::EnableDebugTrace(true);
25+
Babylon::DebugTrace::SetTraceOutput([](const char* trace) { printf("%s\n", trace); fflush(stdout); });
26+
2427
auto testResult = RunTests();
2528

2629
android::StdoutLogger::Stop();

Tests/UnitTests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ target_link_libraries(UnitTests
5252
PRIVATE XMLHttpRequest
5353
PRIVATE WebSocket
5454
PRIVATE gtest_main
55+
PRIVATE Foundation
5556
${ADDITIONAL_LIBRARIES})
5657

5758
# See https://gitlab.kitware.com/cmake/cmake/-/issues/23543

Tests/UnitTests/Linux/App.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include "../Shared/Shared.h"
2+
#include "Babylon/DebugTrace.h"
3+
#include <cstdio>
24

35
int main()
46
{
7+
Babylon::DebugTrace::EnableDebugTrace(true);
8+
Babylon::DebugTrace::SetTraceOutput([](const char* trace) { printf("%s\n", trace); fflush(stdout); });
9+
510
return RunTests();
611
}

Tests/UnitTests/Win32/App.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include "../Shared/Shared.h"
22
#include <Windows.h>
3+
#include "Babylon/DebugTrace.h"
34

45
int main()
56
{
67
SetConsoleOutputCP(CP_UTF8);
78

9+
Babylon::DebugTrace::EnableDebugTrace(true);
10+
Babylon::DebugTrace::SetTraceOutput([](const char* trace) { OutputDebugStringA(trace); });
11+
812
return RunTests();
913
}

0 commit comments

Comments
 (0)