commit 3d2a0a08c68f131d8bc7884c1b98385575299bf6
parent 1568105ba1fd93d09dacc8800df6daa36d5564e8
Author: Florian Dold <florian@dold.me>
Date: Thu, 19 Jan 2023 19:38:22 +0100
add log redirection hack
Diffstat:
2 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/taler_wallet_core_lib.c b/taler_wallet_core_lib.c
@@ -214,5 +214,56 @@ void TALER_WALLET_join(struct TALER_WALLET_Instance *wh)
void
TALER_WALLET_destroy(struct TALER_WALLET_Instance *twi)
{
- // FIXME: Implement!
+ // FIXME: Implement!
}
+
+
+
+struct LogRedirectContext {
+ int active;
+ TALER_LogFn logfn;
+ void *cls;
+};
+
+static struct LogRedirectContext redir_ctx;
+static int pfd[2];
+static pthread_t log_thr;
+
+static void *thread_func(void *) {
+ ssize_t rdsz;
+ char buf[1024];
+ while ((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0) {
+ if (buf[rdsz - 1] == '\n') --rdsz;
+ buf[rdsz] = 0; /* add null-terminator */
+ redir_ctx.logfn(redir_ctx.cls, 0, buf);
+ }
+ return 0;
+}
+
+
+int
+TALER_start_redirect_std(TALER_LogFn logfn, void *cls)
+{
+ if (redir_ctx.active) {
+ return -2;
+ }
+ /* make stdout line-buffered and stderr unbuffered */
+ setvbuf(stdout, 0, _IOLBF, 0);
+ setvbuf(stderr, 0, _IONBF, 0);
+
+ /* create the pipe and redirect stdout and stderr */
+ pipe(pfd);
+ dup2(pfd[1], 1);
+ dup2(pfd[1], 2);
+
+ redir_ctx.cls = cls;
+ redir_ctx.logfn = logfn;
+ redir_ctx.active = 1;
+
+ /* spawn the logging thread */
+ if (pthread_create(&log_thr, 0, thread_func, 0) == -1) {
+ return -1;
+ }
+ pthread_detach(log_thr);
+ return 0;
+}
+\ No newline at end of file
diff --git a/taler_wallet_core_lib.h b/taler_wallet_core_lib.h
@@ -102,4 +102,20 @@ TALER_WALLET_join(struct TALER_WALLET_Instance *twi);
//void
//TALER_WALLET_destroy(struct TALER_WALLET_Instance *twi);
+/**
+ * Handler for messages that should be logged.
+ *
+ * @param stream NOT YET IMPLEMENTED: indicator for the stream that
+ * the message is coming from,
+ */
+typedef void (*TALER_LogFn)(void *cls, int stream, const char *msg);
+
+/**
+ * Redirect stderr and stdout to a function.
+ *
+ * Workaround for platforms where stderr is not visible in logs.
+ */
+int
+TALER_redirect_std(TALER_LogFn logfn, void *cls);
+
#endif /*_TALER_WALLET_LIB_H */