+/******************************************************************************
+ * section: default callbacks for tracing service interface (evt)
+ *****************************************************************************/
+
+static void _hook_evt_(const char *evt, int id, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ _hook_("evt-%s:%d", format, ap, evt, id);
+ va_end(ap);
+}
+
+static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
+{
+ _hook_evt_(evt, id, "create");
+}
+
+static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
+{
+ _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
+}
+
+
+static void hook_evt_push_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
+{
+ _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
+}
+
+static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
+{
+ _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
+}
+
+static void hook_evt_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
+{
+ _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
+}
+
+static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
+{
+ _hook_evt_(evt, id, "name -> %s", result);
+}
+
+static void hook_evt_addref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
+{
+ _hook_evt_(evt, id, "addref");
+}
+
+static void hook_evt_unref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
+{
+ _hook_evt_(evt, id, "unref");
+}
+
+static struct afb_hook_evt_itf hook_evt_default_itf = {
+ .hook_evt_create = hook_evt_create_cb,
+ .hook_evt_push_before = hook_evt_push_before_cb,
+ .hook_evt_push_after = hook_evt_push_after_cb,
+ .hook_evt_broadcast_before = hook_evt_broadcast_before_cb,
+ .hook_evt_broadcast_after = hook_evt_broadcast_after_cb,
+ .hook_evt_name = hook_evt_name_cb,
+ .hook_evt_addref = hook_evt_addref_cb,
+ .hook_evt_unref = hook_evt_unref_cb
+};
+
+/******************************************************************************
+ * section: hooks for tracing events interface (evt)
+ *****************************************************************************/
+
+#define _HOOK_EVT_(what,...) \
+ struct afb_hook_evt *hook; \
+ struct afb_hookid hookid; \
+ pthread_rwlock_rdlock(&rwlock); \
+ init_hookid(&hookid); \
+ hook = list_of_evt_hooks; \
+ while (hook) { \
+ if (hook->itf->hook_evt_##what \
+ && (hook->flags & afb_hook_flag_evt_##what) != 0 \
+ && MATCH_EVENT(hook->pattern, evt)) { \
+ hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
+ } \
+ hook = hook->next; \
+ } \
+ pthread_rwlock_unlock(&rwlock);
+
+void afb_hook_evt_create(const char *evt, int id)
+{
+ _HOOK_EVT_(create, evt, id);
+}
+
+void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
+{
+ _HOOK_EVT_(push_before, evt, id, obj);
+}
+
+int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
+{
+ _HOOK_EVT_(push_after, evt, id, obj, result);
+ return result;
+}
+
+void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
+{
+ _HOOK_EVT_(broadcast_before, evt, id, obj);
+}
+
+int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
+{
+ _HOOK_EVT_(broadcast_after, evt, id, obj, result);
+ return result;
+}
+
+void afb_hook_evt_name(const char *evt, int id, const char *result)
+{
+ _HOOK_EVT_(name, evt, id, result);
+}
+
+void afb_hook_evt_addref(const char *evt, int id)
+{
+ _HOOK_EVT_(addref, evt, id);
+}
+
+void afb_hook_evt_unref(const char *evt, int id)
+{
+ _HOOK_EVT_(unref, evt, id);
+}
+
+/******************************************************************************
+ * section: hooking services (evt)
+ *****************************************************************************/
+
+int afb_hook_flags_evt(const char *name)
+{
+ int flags;
+ struct afb_hook_evt *hook;
+
+ flags = 0;
+ pthread_rwlock_rdlock(&rwlock);
+ hook = list_of_evt_hooks;
+ while (hook) {
+ if (!name || MATCH_EVENT(hook->pattern, name))
+ flags |= hook->flags;
+ hook = hook->next;
+ }
+ pthread_rwlock_unlock(&rwlock);
+ return flags;
+}
+
+struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
+{
+ struct afb_hook_evt *hook;
+
+ /* alloc the result */
+ hook = calloc(1, sizeof *hook);
+ if (hook == NULL)
+ return NULL;
+
+ /* get a copy of the names */
+ hook->pattern = pattern ? strdup(pattern) : NULL;
+ if (pattern && !hook->pattern) {
+ free(hook);
+ return NULL;
+ }
+
+ /* initialise the rest */
+ hook->refcount = 1;
+ hook->flags = flags;
+ hook->itf = itf ? itf : &hook_evt_default_itf;
+ hook->closure = closure;
+
+ /* record the hook */
+ pthread_rwlock_wrlock(&rwlock);
+ hook->next = list_of_evt_hooks;
+ list_of_evt_hooks = hook;
+ pthread_rwlock_unlock(&rwlock);
+
+ /* returns it */
+ return hook;
+}
+
+struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
+{
+ pthread_rwlock_wrlock(&rwlock);
+ hook->refcount++;
+ pthread_rwlock_unlock(&rwlock);
+ return hook;
+}
+
+void afb_hook_unref_evt(struct afb_hook_evt *hook)
+{
+ struct afb_hook_evt **prv;
+
+ if (hook) {
+ pthread_rwlock_wrlock(&rwlock);
+ if (--hook->refcount)
+ hook = NULL;
+ else {
+ /* unlink */
+ prv = &list_of_evt_hooks;
+ while (*prv && *prv != hook)
+ prv = &(*prv)->next;
+ if(*prv)
+ *prv = hook->next;
+ }
+ pthread_rwlock_unlock(&rwlock);
+ if (hook) {
+ /* free */
+ free(hook->pattern);
+ free(hook);
+ }
+ }
+}
+
+/******************************************************************************
+ * section: default callbacks for sessions (session)
+ *****************************************************************************/
+
+static void _hook_session_(struct afb_session *session, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ _hook_("session-%s", format, ap, afb_session_uuid(session));
+ va_end(ap);
+}
+
+static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "create -> token=%s", afb_session_token(session));
+}
+
+static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "close");
+}
+
+static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "destroy");
+}
+
+static void hook_session_renew_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "renew -> token=%s", afb_session_token(session));
+}
+
+static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "addref");
+}
+
+static void hook_session_unref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "unref");
+}
+
+static struct afb_hook_session_itf hook_session_default_itf = {
+ .hook_session_create = hook_session_create_cb,
+ .hook_session_close = hook_session_close_cb,
+ .hook_session_destroy = hook_session_destroy_cb,
+ .hook_session_renew = hook_session_renew_cb,
+ .hook_session_addref = hook_session_addref_cb,
+ .hook_session_unref = hook_session_unref_cb
+};
+
+/******************************************************************************
+ * section: hooks for tracing sessions (session)
+ *****************************************************************************/
+
+#define _HOOK_SESSION_(what,...) \
+ struct afb_hook_session *hook; \
+ struct afb_hookid hookid; \
+ const char *sessid = 0; \
+ pthread_rwlock_rdlock(&rwlock); \
+ init_hookid(&hookid); \
+ hook = list_of_session_hooks; \
+ while (hook) { \
+ if (hook->itf->hook_session_##what \
+ && (hook->flags & afb_hook_flag_session_##what) != 0 \
+ && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \
+ hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
+ } \
+ hook = hook->next; \
+ } \
+ pthread_rwlock_unlock(&rwlock);
+
+void afb_hook_session_create(struct afb_session *session)
+{
+ _HOOK_SESSION_(create, session);
+}
+
+void afb_hook_session_close(struct afb_session *session)
+{
+ _HOOK_SESSION_(close, session);
+}
+
+void afb_hook_session_destroy(struct afb_session *session)
+{
+ _HOOK_SESSION_(destroy, session);
+}
+
+void afb_hook_session_renew(struct afb_session *session)
+{
+ _HOOK_SESSION_(renew, session);
+}
+
+void afb_hook_session_addref(struct afb_session *session)
+{
+ _HOOK_SESSION_(addref, session);
+}
+
+void afb_hook_session_unref(struct afb_session *session)
+{
+ _HOOK_SESSION_(unref, session);
+}
+
+
+/******************************************************************************
+ * section: hooking sessions (session)
+ *****************************************************************************/
+
+struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
+{
+ struct afb_hook_session *hook;
+
+ /* alloc the result */
+ hook = calloc(1, sizeof *hook);
+ if (hook == NULL)
+ return NULL;
+
+ /* get a copy of the names */
+ hook->pattern = pattern ? strdup(pattern) : NULL;
+ if (pattern && !hook->pattern) {
+ free(hook);
+ return NULL;
+ }
+
+ /* initialise the rest */
+ hook->refcount = 1;
+ hook->flags = flags;
+ hook->itf = itf ? itf : &hook_session_default_itf;
+ hook->closure = closure;
+
+ /* record the hook */
+ pthread_rwlock_wrlock(&rwlock);
+ hook->next = list_of_session_hooks;
+ list_of_session_hooks = hook;
+ pthread_rwlock_unlock(&rwlock);
+
+ /* returns it */
+ return hook;
+}
+
+struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
+{
+ pthread_rwlock_wrlock(&rwlock);
+ hook->refcount++;
+ pthread_rwlock_unlock(&rwlock);
+ return hook;
+}
+
+void afb_hook_unref_session(struct afb_hook_session *hook)
+{
+ struct afb_hook_session **prv;
+
+ if (hook) {
+ pthread_rwlock_wrlock(&rwlock);
+ if (--hook->refcount)
+ hook = NULL;
+ else {
+ /* unlink */
+ prv = &list_of_session_hooks;
+ while (*prv && *prv != hook)
+ prv = &(*prv)->next;
+ if(*prv)
+ *prv = hook->next;
+ }
+ pthread_rwlock_unlock(&rwlock);
+ if (hook) {
+ /* free */
+ free(hook->pattern);
+ free(hook);
+ }
+ }
+}
+
+/******************************************************************************
+ * section: default callbacks for globals (global)
+ *****************************************************************************/
+
+static void _hook_global_(const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ _hook_("global", format, ap);
+ va_end(ap);
+}
+
+static void hook_global_vverbose_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
+{
+ int len;
+ char *msg;
+ va_list ap;
+
+ va_copy(ap, args);
+ len = vasprintf(&msg, fmt, ap);
+ va_end(ap);
+
+ if (len < 0)
+ _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
+ else {
+ _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
+ free(msg);
+ }
+}
+
+static struct afb_hook_global_itf hook_global_default_itf = {
+ .hook_global_vverbose = hook_global_vverbose_cb
+};
+
+/******************************************************************************
+ * section: hooks for tracing globals (global)
+ *****************************************************************************/
+
+#define _HOOK_GLOBAL_(what,...) \
+ struct afb_hook_global *hook; \
+ struct afb_hookid hookid; \
+ pthread_rwlock_rdlock(&rwlock); \
+ init_hookid(&hookid); \
+ hook = list_of_global_hooks; \
+ while (hook) { \
+ if (hook->itf->hook_global_##what \
+ && (hook->flags & afb_hook_flag_global_##what) != 0) { \
+ hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
+ } \
+ hook = hook->next; \
+ } \
+ pthread_rwlock_unlock(&rwlock);
+
+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);
+}
+
+/******************************************************************************
+ * section: hooking globals (global)
+ *****************************************************************************/
+
+static void update_global()
+{
+ struct afb_hook_global *hook;
+ int flags = 0;
+
+ pthread_rwlock_rdlock(&rwlock);
+ hook = list_of_global_hooks;
+ while (hook) {
+ flags = hook->flags;
+ hook = hook->next;
+ }
+ verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
+ pthread_rwlock_unlock(&rwlock);
+}
+
+struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
+{
+ struct afb_hook_global *hook;
+
+ /* alloc the result */
+ hook = calloc(1, sizeof *hook);
+ if (hook == NULL)
+ return NULL;
+
+ /* initialise the rest */
+ hook->refcount = 1;
+ hook->flags = flags;
+ hook->itf = itf ? itf : &hook_global_default_itf;
+ hook->closure = closure;
+
+ /* record the hook */
+ pthread_rwlock_wrlock(&rwlock);
+ hook->next = list_of_global_hooks;
+ list_of_global_hooks = hook;
+ pthread_rwlock_unlock(&rwlock);
+
+ /* update hooking */
+ update_global();
+
+ /* returns it */
+ return hook;
+}
+
+struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
+{
+ pthread_rwlock_wrlock(&rwlock);
+ hook->refcount++;
+ pthread_rwlock_unlock(&rwlock);
+ return hook;
+}
+
+void afb_hook_unref_global(struct afb_hook_global *hook)
+{
+ struct afb_hook_global **prv;
+
+ if (hook) {
+ pthread_rwlock_wrlock(&rwlock);
+ if (--hook->refcount)
+ hook = NULL;
+ else {
+ /* unlink */
+ prv = &list_of_global_hooks;
+ while (*prv && *prv != hook)
+ prv = &(*prv)->next;
+ if(*prv)
+ *prv = hook->next;
+ }
+ pthread_rwlock_unlock(&rwlock);
+ if (hook) {
+ /* free */
+ free(hook);
+
+ /* update hooking */
+ update_global();
+ }
+ }
+}
+