Redirect stdout/stderr to android logcat

Underlying wallet error messages were not going anywhere useful; this
redirects them into the android logger so that you can access them (e.g.
with adb logcat).
This commit is contained in:
Jason Rhinelander 2023-01-06 12:58:20 -04:00
parent 66dabf3fee
commit 6e7c5be0ea
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262
6 changed files with 51 additions and 5 deletions

View File

@ -32,7 +32,7 @@ class Routes {
static const addressBookAddContact = '/address_book_add_contact';
static const dangerzoneKeys = '/dangerzone/keys';
static const dangerzoneSeed = '/dangerzone/seed';
static const dangerzoneRemoveWallet = '//dangerzone/remove_wallet';
static const dangerzoneRemoveWallet = '/dangerzone/remove_wallet';
static const showKeys = '/dangerzone/show/keys';
static const subaddressList = '/subaddress_list';
static const restoreWalletFromSeedDetails = '/restore_from_seed_details';

View File

@ -1,5 +1,6 @@
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:hive/hive.dart';
import 'package:oxen_coin/stake.dart' as oxen_stake;
import 'package:oxen_coin/transaction_history.dart' as transaction_history;
@ -55,7 +56,20 @@ class OxenWallet extends Wallet {
static Future<OxenWallet> load(
Box<WalletInfo> walletInfoSource, String name, WalletType type) async {
final id = (walletTypeToString(type)?.toLowerCase() ?? 'unknown') + '_' + name;
final walletInfo = walletInfoSource.values.firstWhere((info) => info.id == id);
print('Loading wallet $id');
var walletInfo = walletInfoSource.values.firstWhereOrNull((info) => info.id == id);
if (walletInfo == null) {
walletInfo = WalletInfo(
id: id,
name: name,
type: type,
timestamp: DateTime.now().millisecondsSinceEpoch,
// We don't know what these were, so use conservative values:
isRecovery: false,
restoreHeight: 0);
walletInfoSource.add(walletInfo);
}
return await configured(
walletInfoSource: walletInfoSource, walletInfo: walletInfo);
}

View File

@ -121,7 +121,7 @@ class OxenWalletsManager extends WalletsManager {
@override
Future<Wallet> openWallet(String name, String password) async {
print('opening a Wallet with nettype $nettype');
print('opening Wallet $name with nettype $nettype');
try {
final path = await pathForWallet(name: name);
oxen_wallet_manager.openWallet(path: path, password: password, nettype: nettype);

View File

@ -15,4 +15,4 @@ abstract class WalletsManager {
Future<bool> isWalletExit(String name);
Future remove(WalletDescription wallet);
}
}

View File

@ -21,5 +21,5 @@ set_target_properties(wallet_api PROPERTIES IMPORTED_LOCATION
include_directories( ${EXTERNAL_LIBS_DIR}/oxen/include )
target_link_libraries( oxen_coin PRIVATE wallet_api )
target_link_libraries( oxen_coin PRIVATE wallet_api log )
target_include_directories( oxen_coin PRIVATE wallet_api )

View File

@ -6,6 +6,7 @@
#include <iostream>
#include <unistd.h>
#include <thread>
#include <mutex>
// #include "OxenWalletListener.h"
#ifdef ANDROID
#include "../External/android/oxen/include/wallet2_api.h"
@ -18,6 +19,37 @@ namespace Oxen = Wallet;
// Macro to force symbol visibility, and prevent the symbol being stripped
#define EXPORT __attribute__((visibility("default"))) __attribute__((used))
#ifdef __ANDROID_API__
extern "C" int __android_log_write(int prio, const char* tag, const char* text);
static bool start_logger()
{
// Set stdout/stderr to the typical line-buffered and unbuffered:
setvbuf(stdout, 0, _IOLBF, 0);
setvbuf(stderr, 0, _IONBF, 0);
// Create a pipe and redirect both stdout/stderr into it:
int pipe_fd[2];
pipe(pipe_fd);
dup2(pipe_fd[1], 1);
dup2(pipe_fd[1], 2);
// Spawn and detach a logging thread that forever captures stderr (via the pipe) and redirects
// it into the android log subsystem:
std::thread{[](int fd) {
ssize_t sz;
char buf[4096];
while ((sz = read(fd, buf, sizeof(buf) - 1)) > 0) {
if (buf[sz - 1] == '\n') --sz; // strip trailing newline (android_log_write adds one)
buf[sz] = 0; // null terminate
__android_log_write(4 /*ANDROID_LOG_INFO*/, "oxen-wallet", buf);
}
}, pipe_fd[0]}.detach();
return true;
}
static bool started_logger = start_logger();
#endif
extern "C"
{
typedef struct