summaryrefslogtreecommitdiff
path: root/src/basic/log.h
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-21 14:06:21 +0100
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-21 14:25:10 +0100
commit883354532cb6f050baf86511fa59baf908391714 (patch)
tree1dbb98917682d8512e12f7ecc8d9fd8e9a23946d /src/basic/log.h
parent26f51ae430883125e5a4424bb567b603f9cefe86 (diff)
downloadsystemd-883354532cb6f050baf86511fa59baf908391714.tar.gz
log: Avoid pushing the same fields more than once on the log context
Let's try to optimize against pushing the same fields multiple times onto the log context. To achieve this we make the log context reference counted and return an existing context object if it's using the same fields. A consequence of this is that we have to make sure attaching/detaching is coupled to the lifetime of the context object, so we make the attach and detach functions private for now. If we need independent attach/detach in the future, we can make that work with some extra complexity but since we don't need it yet, let's not support it for now.
Diffstat (limited to 'src/basic/log.h')
-rw-r--r--src/basic/log.h22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/basic/log.h b/src/basic/log.h
index c4ac73e27b..f17a97ee37 100644
--- a/src/basic/log.h
+++ b/src/basic/log.h
@@ -457,25 +457,23 @@ typedef struct LogContext LogContext;
bool log_context_enabled(void);
-LogContext* log_context_attach(LogContext *c);
-LogContext* log_context_detach(LogContext *c);
-
LogContext* log_context_new(char **fields, bool owned);
LogContext* log_context_newv(struct iovec *input_iovec, size_t n_input_iovec, bool owned);
-LogContext* log_context_free(LogContext *c);
/* Same as log_context_new(), but frees the given fields strv/iovec on failure. */
LogContext* log_context_new_consume(char **fields);
LogContext* log_context_new_consumev(struct iovec *input_iovec, size_t n_input_iovec);
+LogContext *log_context_ref(LogContext *c);
+LogContext *log_context_unref(LogContext *c);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(LogContext*, log_context_unref);
+
/* Returns the number of attached log context objects. */
size_t log_context_num_contexts(void);
/* Returns the number of fields in all attached log contexts. */
size_t log_context_num_fields(void);
-DEFINE_TRIVIAL_CLEANUP_FUNC(LogContext*, log_context_detach);
-DEFINE_TRIVIAL_CLEANUP_FUNC(LogContext*, log_context_free);
-
#define LOG_CONTEXT_PUSH(...) \
LOG_CONTEXT_PUSH_STRV(STRV_MAKE(__VA_ARGS__))
@@ -483,13 +481,13 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(LogContext*, log_context_free);
LOG_CONTEXT_PUSH(snprintf_ok((char[LINE_MAX]) {}, LINE_MAX, __VA_ARGS__))
#define _LOG_CONTEXT_PUSH_STRV(strv, c) \
- _unused_ _cleanup_(log_context_freep) LogContext *c = log_context_new(strv, /*owned=*/ false);
+ _unused_ _cleanup_(log_context_unrefp) LogContext *c = log_context_new(strv, /*owned=*/ false);
#define LOG_CONTEXT_PUSH_STRV(strv) \
_LOG_CONTEXT_PUSH_STRV(strv, UNIQ_T(c, UNIQ))
#define _LOG_CONTEXT_PUSH_IOV(input_iovec, n_input_iovec, c) \
- _unused_ _cleanup_(log_context_freep) LogContext *c = log_context_newv(input_iovec, n_input_iovec, /*owned=*/ false);
+ _unused_ _cleanup_(log_context_unrefp) LogContext *c = log_context_newv(input_iovec, n_input_iovec, /*owned=*/ false);
#define LOG_CONTEXT_PUSH_IOV(input_iovec, n_input_iovec) \
_LOG_CONTEXT_PUSH_IOV(input_iovec, n_input_iovec, UNIQ_T(c, UNIQ))
@@ -503,19 +501,19 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(LogContext*, log_context_free);
_unused_ _cleanup_strv_free_ strv = strv_new(s); \
if (!strv) \
free(s); \
- _unused_ _cleanup_(log_context_freep) LogContext *c = log_context_new_consume(TAKE_PTR(strv))
+ _unused_ _cleanup_(log_context_unrefp) LogContext *c = log_context_new_consume(TAKE_PTR(strv))
#define LOG_CONTEXT_CONSUME_STR(s) \
_LOG_CONTEXT_CONSUME_STR(s, UNIQ_T(c, UNIQ), UNIQ_T(sv, UNIQ))
#define _LOG_CONTEXT_CONSUME_STRV(strv, c) \
- _unused_ _cleanup_(log_context_freep) LogContext *c = log_context_new_consume(strv);
+ _unused_ _cleanup_(log_context_unrefp) LogContext *c = log_context_new_consume(strv);
#define LOG_CONTEXT_CONSUME_STRV(strv) \
_LOG_CONTEXT_CONSUME_STRV(strv, UNIQ_T(c, UNIQ))
#define _LOG_CONTEXT_CONSUME_IOV(input_iovec, n_input_iovec, c) \
- _unused_ _cleanup_(log_context_freep) LogContext *c = log_context_new_consumev(input_iovec, n_input_iovec);
+ _unused_ _cleanup_(log_context_unrefp) LogContext *c = log_context_new_consumev(input_iovec, n_input_iovec);
#define LOG_CONTEXT_CONSUME_IOV(input_iovec, n_input_iovec) \
_LOG_CONTEXT_CONSUME_IOV(input_iovec, n_input_iovec, UNIQ_T(c, UNIQ))