Skip to content

Commit

Permalink
Add BARE_ANDROID_USE_LOGCAT build option (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Jul 3, 2024
1 parent 1c1787b commit 5c971ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
26 changes: 19 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include(bare)

bare_target(target)

include(cmake/options.cmake)

install_node_modules(LOCKFILE)

mirror_drive(
Expand All @@ -15,11 +17,9 @@ mirror_drive(
CHECKOUT 141
)

if(MSVC)
if(target MATCHES "win32")
add_compile_options(/MT$<$<CONFIG:Debug>:d>)
endif()

if(WIN32)
add_definitions(-DWIN32_LEAN_AND_MEAN)
endif()

Expand Down Expand Up @@ -89,12 +89,10 @@ if(NOT TARGET v8)
m
)
elseif(target MATCHES "android")
find_library(log log)

target_link_libraries(
v8
INTERFACE
${log}
log
)
elseif(target MATCHES "win32")
target_link_libraries(
Expand Down Expand Up @@ -206,14 +204,28 @@ target_link_libraries(
hex
)

if(MSVC)
if(target MATCHES "win32")
target_compile_options(
bare
PRIVATE
/experimental:c11atomics
)
endif()

if(target MATCHES "android")
target_compile_definitions(
bare
PRIVATE
BARE_ANDROID_USE_LOGCAT=$<BOOL:BARE_ANDROID_USE_LOGCAT>
)

target_link_libraries(
bare
PUBLIC
log
)
endif()

add_library(bare_shared SHARED)

set_target_properties(
Expand Down
1 change: 1 addition & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option(BARE_ANDROID_USE_LOGCAT "Use Logcat instead of standard I/O for logging on Android" OFF)
24 changes: 24 additions & 0 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#include "../include/bare.h"

#if BARE_ANDROID_USE_LOGCAT
#include <android/log.h>
#endif

#include "addon.h"
#include "bare.js.h"
#include "runtime.h"
Expand Down Expand Up @@ -72,8 +76,13 @@ err: {
err = js_close_handle_scope(env, scope);
assert(err == 0);

#if BARE_ANDROID_USE_LOGCAT
err = __android_log_print(ANDROID_LOG_FATAL, "bare", "Uncaught %s", str);
assert(err == 1);
#else
err = fprintf(stderr, "Uncaught %s\n", str);
assert(err >= 0);
#endif

abort();
}
Expand Down Expand Up @@ -130,8 +139,13 @@ err: {
err = js_close_handle_scope(env, scope);
assert(err == 0);

#if BARE_ANDROID_USE_LOGCAT
err = __android_log_print(ANDROID_LOG_FATAL, "bare", "Uncaught (in promise) %s", str);
assert(err == 1);
#else
err = fprintf(stderr, "Uncaught (in promise) %s\n", str);
assert(err >= 0);
#endif

abort();
}
Expand Down Expand Up @@ -454,8 +468,13 @@ bare_runtime_print_info (js_env_t *env, js_callback_info_t *info) {
err = js_close_handle_scope(env, scope);
assert(err == 0);

#if BARE_ANDROID_USE_LOGCAT
err = __android_log_print(ANDROID_LOG_INFO, "bare", "%s", data);
assert(err == 1);
#else
err = fprintf(stdout, "%s", data);
assert(err >= 0);
#endif

err = fflush(stdout);
assert(err == 0);
Expand Down Expand Up @@ -494,8 +513,13 @@ bare_runtime_print_error (js_env_t *env, js_callback_info_t *info) {
err = js_close_handle_scope(env, scope);
assert(err == 0);

#if BARE_ANDROID_USE_LOGCAT
err = __android_log_print(ANDROID_LOG_ERROR, "bare", "%s", data);
assert(err == 1);
#else
err = fprintf(stderr, "%s", data);
assert(err >= 0);
#endif

err = fflush(stderr);
assert(err == 0);
Expand Down

0 comments on commit 5c971ce

Please sign in to comment.