Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable stdout/stderr logging to logcat on android #72

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions monero_libwallet2_api_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if(${HOST_ABI} STREQUAL "x86_64-linux-android" OR
${HOST_ABI} STREQUAL "aarch64-linux-android" OR
${HOST_ABI} STREQUAL "armv7a-linux-androideabi")
add_link_options(-stdlib=libc++ -static-libstdc++)
set(EXTRA_LIBS_ANDROID log)
endif()

add_library( wallet2_api_c
Expand Down
67 changes: 66 additions & 1 deletion monero_libwallet2_api_c/src/main/cpp/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,71 @@
#include <set>
#include <sstream>
#include <cstring>
#include <thread>

#ifdef __ANDROID__
#include <android/log.h>

#define LOG_TAG "moneroc"
#define BUFFER_SIZE 1024*32

static int stdoutToLogcat(const char *buf, int size) {
__android_log_write(ANDROID_LOG_INFO, LOG_TAG, buf);
return size;
}

static int stderrToLogcat(const char *buf, int size) {
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG, buf);
return size;
}

void redirectStdoutThread(int pipe_stdout[2]) {
char bufferStdout[BUFFER_SIZE];
while (true) {
int read_size = read(pipe_stdout[0], bufferStdout, sizeof(bufferStdout) - 1);
if (read_size > 0) {
bufferStdout[read_size] = '\0';
stdoutToLogcat(bufferStdout, read_size);
}
}
}

void redirectStderrThread(int pipe_stderr[2]) {
char bufferStderr[BUFFER_SIZE];
while (true) {
int read_size = read(pipe_stderr[0], bufferStderr, sizeof(bufferStderr) - 1);
if (read_size > 0) {
bufferStderr[read_size] = '\0';
stderrToLogcat(bufferStderr, read_size);
}
}
}

void setupAndroidLogging() {
static int pfdStdout[2];
static int pfdStderr[2];

pipe(pfdStdout);
pipe(pfdStderr);

dup2(pfdStdout[1], STDOUT_FILENO);
dup2(pfdStderr[1], STDERR_FILENO);

std::thread stdoutThread(redirectStdoutThread, pfdStdout);
std::thread stderrThread(redirectStderrThread, pfdStderr);

stdoutThread.detach();
stderrThread.detach();
}

#endif // __ANDROID__

__attribute__((constructor))
void library_init() {
#ifdef __ANDROID__
setupAndroidLogging(); // This will now run automatically when the library is loaded
#endif
}

const char* vectorToString(const std::vector<std::string>& vec, const std::string separator) {
// Check if the vector is empty
Expand Down Expand Up @@ -195,4 +260,4 @@ std::vector<uint64_t> splitStringUint(const std::string& str, const std::string&
}
tokens.push_back(std::stoull(content)); // Inserting the last token
return tokens;
}
}
Loading