Improve handling of verbosity
[src/app-framework-binder.git] / src / afb-hook.c
index 1407628..7dd81cb 100644 (file)
@@ -24,6 +24,7 @@
 #include <pthread.h>
 #include <unistd.h>
 #include <fnmatch.h>
+#include <sys/uio.h>
 
 #include <json-c/json.h>
 
@@ -135,7 +136,7 @@ static void init_hookid(struct afb_hookid *hookid)
  * section: default callbacks for tracing requests
  *****************************************************************************/
 
-static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf)
+static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
 {
        int rc;
        va_list cp;
@@ -151,18 +152,21 @@ static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, si
                        sbuf = *palloc;
        }
        va_end(cp);
+       if (rc >= 0 && outlen)
+               *outlen = (size_t)rc;
        return sbuf;
 }
 
+#if 0 /* old behaviour: use NOTICE */
 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
 {
        char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
        va_list arg1;
 
-       data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2);
+       data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
 
        va_start(arg1, arg2);
-       tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1);
+       tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
        va_end(arg1);
 
        NOTICE("[HOOK %s] %s", tag, data);
@@ -170,6 +174,35 @@ static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
        free(mem1);
        free(mem2);
 }
+#else /* new behaviour: emits directly to stderr */
+static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
+{
+       static const char chars[] = "HOOK: [] \n";
+       char *mem1, *mem2, buf1[256], buf2[2000];
+       struct iovec iov[5];
+       va_list arg1;
+
+       iov[0].iov_base = (void*)&chars[0];
+       iov[0].iov_len = 7;
+
+       va_start(arg1, arg2);
+       iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
+       va_end(arg1);
+
+       iov[2].iov_base = (void*)&chars[7];
+       iov[2].iov_len = 2;
+
+       iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
+
+       iov[4].iov_base = (void*)&chars[9];
+       iov[4].iov_len = 1;
+
+       writev(2, iov, 5);
+
+       free(mem1);
+       free(mem2);
+}
+#endif
 
 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
 {
@@ -317,6 +350,11 @@ static void hook_xreq_subcall_req_result_default_cb(void *closure, const struct
        _hook_xreq_(xreq, "    ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
 }
 
+static void hook_xreq_has_permission_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
+{
+       _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
+}
+
 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
        .hook_xreq_begin = hook_xreq_begin_default_cb,
        .hook_xreq_end = hook_xreq_end_default_cb,
@@ -340,7 +378,8 @@ static struct afb_hook_xreq_itf hook_xreq_default_itf = {
        .hook_xreq_store = hook_xreq_store_default_cb,
        .hook_xreq_unstore = hook_xreq_unstore_default_cb,
        .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
-       .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb
+       .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
+       .hook_xreq_has_permission = hook_xreq_has_permission_default_cb
 };
 
 /******************************************************************************
@@ -488,6 +527,12 @@ void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, s
        _HOOK_XREQ_(subcall_req_result, xreq, status, result);
 }
 
+int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
+{
+       _HOOK_XREQ_(has_permission, xreq, permission, result);
+       return result;
+}
+
 /******************************************************************************
  * section: hooking xreqs
  *****************************************************************************/
@@ -1370,7 +1415,7 @@ static struct afb_hook_global_itf hook_global_default_itf = {
 
 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
 {
-       _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt, args);
+       _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
 }
 
 /******************************************************************************