From e2264e837467554216b0e64d1382d62bcad561fb Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Tue, 18 Aug 2020 14:58:13 -0500 Subject: [PATCH] base: Make log facility opaque, ref-counted --- lib/base/heimbase.h | 14 +------------- lib/base/log.c | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/base/heimbase.h b/lib/base/heimbase.h index f0ae516e3..c0c94e264 100644 --- a/lib/base/heimbase.h +++ b/lib/base/heimbase.h @@ -93,19 +93,7 @@ typedef void (HEIM_CALLCONV *heim_log_log_func_t)(heim_context, void *); typedef void (HEIM_CALLCONV *heim_log_close_func_t)(void *); -struct heim_log_facility_internal { - int min; - int max; - heim_log_log_func_t log_func; - heim_log_close_func_t close_func; - void *data; -}; - -typedef struct heim_log_facility_s { - char *program; - int len; - struct heim_log_facility_internal *val; -} heim_log_facility; +typedef struct heim_log_facility_s heim_log_facility; typedef uintptr_t (HEIM_LIB_CALL *heim_get_instance_func_t)(const char *); diff --git a/lib/base/log.c b/lib/base/log.c index 3248814de..7ebbc3b9d 100644 --- a/lib/base/log.c +++ b/lib/base/log.c @@ -41,6 +41,21 @@ #include #include +struct heim_log_facility_internal { + int min; + int max; + heim_log_log_func_t log_func; + heim_log_close_func_t close_func; + void *data; +}; + +struct heim_log_facility_s { + char *program; + size_t refs; + size_t len; + struct heim_log_facility_internal *val; +}; + typedef struct heim_pcontext_s *heim_pcontext; typedef struct heim_pconfig *heim_pconfig; struct heim_svc_req_desc_common_s { @@ -126,6 +141,7 @@ heim_initlog(heim_context context, heim_log_facility *f = calloc(1, sizeof(*f)); if (f == NULL) return heim_enomem(context); + f->refs = 1; f->program = strdup(program); if (f->program == NULL) { free(f); @@ -135,6 +151,14 @@ heim_initlog(heim_context context, return 0; } +heim_log_facility * +heim_log_ref(heim_log_facility *fac) +{ + if (fac) + fac->refs++; + return fac; +} + heim_error_code heim_addlog_func(heim_context context, heim_log_facility *fac, @@ -432,7 +456,7 @@ heim_closelog(heim_context context, heim_log_facility *fac) { int i; - if (!fac) + if (!fac || --(fac->refs)) return; for (i = 0; i < fac->len; i++) (*fac->val[i].close_func)(fac->val[i].data);