Skip to content

Commit dd0b3b9

Browse files
committed
Add cross-platform logging
1 parent c962a1f commit dd0b3b9

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

packages/react-native-node-api-modules/android/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ target_include_directories(weak-node-api INTERFACE
1717

1818
add_library(node-api-host SHARED
1919
src/main/cpp/OnLoad.cpp
20+
../cpp/Logger.cpp
2021
../cpp/CxxNodeApiHostModule.cpp
2122
../cpp/WeakNodeApiInjector.cpp
2223
)
@@ -27,9 +28,10 @@ target_include_directories(node-api-host PRIVATE
2728

2829
target_link_libraries(node-api-host
2930
# android
31+
log
3032
ReactAndroid::reactnative
3133
ReactAndroid::jsi
3234
hermes-engine::libhermes
3335
weak-node-api
3436
# react_codegen_NodeApiHostSpec
35-
)
37+
)

packages/react-native-node-api-modules/cpp/AddonLoaders.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
#pragma once
2+
#include "Logger.hpp"
3+
24
#include <assert.h>
35

46
#if defined(__APPLE__) || defined(__ANDROID__)
57
#include <dlfcn.h>
68
#include <stdio.h>
79

10+
using callstack::nodeapihost::log_debug;
11+
812
struct PosixLoader {
913
using Module = void *;
1014
using Symbol = void *;
1115

1216
static Module loadLibrary(const char *filePath) {
1317
assert(NULL != filePath);
18+
1419
Module result = dlopen(filePath, RTLD_NOW | RTLD_LOCAL);
1520
if (NULL == result) {
16-
fprintf(stderr, "NapiHost: Failed to load library '%s': %s", filePath,
17-
dlerror());
21+
log_debug("NapiHost: Failed to load library '%s': %s", filePath,
22+
dlerror());
1823
}
1924
return result;
2025
}

packages/react-native-node-api-modules/cpp/CxxNodeApiHostModule.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CxxNodeApiHostModule.hpp"
2+
#include "Logger.hpp"
23

34
using namespace facebook;
45

@@ -58,10 +59,14 @@ bool CxxNodeApiHostModule::loadNodeAddon(NodeAddon &addon,
5859
abort()
5960
#endif
6061

62+
log_debug("[%s] Loading addon by '%s'", libraryName.c_str(),
63+
libraryPath.c_str());
64+
6165
typename LoaderPolicy::Symbol initFn = NULL;
6266
typename LoaderPolicy::Module library =
6367
LoaderPolicy::loadLibrary(libraryPath.c_str());
6468
if (NULL != library) {
69+
log_debug("[%s] Loaded addon", libraryName.c_str());
6570
addon.moduleHandle = library;
6671

6772
// Generate a name allowing us to reference the exports object from JSI
@@ -73,12 +78,20 @@ bool CxxNodeApiHostModule::loadNodeAddon(NodeAddon &addon,
7378

7479
initFn = LoaderPolicy::getSymbol(library, "napi_register_module_v1");
7580
if (NULL != initFn) {
81+
log_debug("[%s] Found napi_register_module_v1 (%p)", libraryName.c_str(),
82+
initFn);
7683
addon.init = (napi_addon_register_func)initFn;
84+
} else {
85+
log_debug("[%s] Failed to find napi_register_module_v1. Expecting the "
86+
"addon to call napi_module_register to register itself.",
87+
libraryName.c_str());
7788
}
7889
// TODO: Read "node_api_module_get_api_version_v1" to support the addon
7990
// declaring its Node-API version
8091
// @see
8192
// https://github.com/callstackincubator/react-native-node-api-modules/issues/4
93+
} else {
94+
log_debug("[%s] Failed to load library", libraryName.c_str());
8295
}
8396
return NULL != initFn;
8497
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "Logger.hpp"
2+
#include <cstdarg>
3+
#include <cstdio>
4+
5+
#if defined(__ANDROID__)
6+
#include <android/log.h>
7+
#define LOG_TAG "NodeApiHost"
8+
#elif defined(__APPLE__)
9+
#include <TargetConditionals.h>
10+
#endif
11+
12+
namespace callstack::nodeapihost {
13+
void log_debug(const char *format, ...) {
14+
// TODO: Disable logging in release builds
15+
16+
va_list args;
17+
va_start(args, format);
18+
19+
#if defined(__ANDROID__)
20+
__android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG, format, args);
21+
#elif defined(__APPLE__)
22+
// iOS or macOS
23+
fprintf(stderr, "[NodeApiHost] ");
24+
vfprintf(stderr, format, args);
25+
fprintf(stderr, "\n");
26+
#else
27+
// Fallback for other platforms
28+
fprintf(stderr, "[NodeApiHost] ");
29+
vfprintf(stdout, format, args);
30+
fprintf(stdout, "\n");
31+
#endif
32+
33+
va_end(args);
34+
}
35+
} // namespace callstack::nodeapihost
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace callstack::nodeapihost {
6+
void log_debug(const char *format, ...);
7+
}

packages/react-native-node-api-modules/scripts/generate-weak-node-api-injector.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ export const CPP_SOURCE_PATH = path.join(__dirname, "../cpp");
1212
export function generateSource(functions: FunctionDecl[]) {
1313
return [
1414
"// This file is generated by react-native-node-api-modules",
15+
`#include <Logger.hpp>`,
1516
`#include <weak_node_api.hpp>`,
1617
"namespace callstack::nodeapihost {",
1718
"using node_api::internal::inject_host;",
1819
"using node_api::internal::NodeApiHost;",
1920
"void injectIntoWeakNodeApi() {",
20-
" inject_host(node_api::internal::NodeApiHost {",
21+
` log_debug("Injecting WeakNodeApiHost");`,
22+
" inject_host(NodeApiHost {",
2123
...functions
2224
.filter(({ kind }) => kind === "engine")
2325
.flatMap(({ name }) => {

0 commit comments

Comments
 (0)