From a233429dec9494e4cf104924008295806c3717c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Kr=C3=A4mer?= Date: Fri, 3 Feb 2023 14:49:56 +0100 Subject: [PATCH] Add: the possibility to set reference value for logs (#735) To be able to detect corresponding log messages it is possible to set a reference value, which is then displayed in logs afterwards. --- base/logging.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++--- base/logging.h | 9 +++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/base/logging.c b/base/logging.c index a6a8b0d50..9ebb92745 100644 --- a/base/logging.c +++ b/base/logging.c @@ -48,6 +48,12 @@ */ #define G_LOG_DOMAIN "libgvm base" +/** + * @brief for backward compatibility + * + */ +#define LOG_REFERENCES_AVAILABLE + /** * @struct gvm_logging_t * @brief Logging stores the parameters loaded from a log configuration @@ -383,6 +389,55 @@ gvm_log_unlock (void) g_mutex_unlock (logger_mutex); } +char *reference = NULL; + +/** + * @brief Set the log reference object. + * + * In order to be able to see which logs are related to each other, we define a + * common reference for them. E.g. when multiple scans in OpenVAS are running + * simultaniousely it is possible to detect all log corresponding to the same + * scan. The log reference is optional and must be set before calling + * setup_log_handlers. The data given must be freed by calling + * free_log_reference(). If called multiple times the old reference gets freed + * and the new one is set instead. + * + * @param ref + */ +void +set_log_reference (char *ref) +{ + g_free (reference); + reference = ref; +} + +/** + * @brief Get the log reference object + * + * This function returns the current log reference. This enables the possibility + * to save or modify the current reference value. + * + * @return char* + */ +char * +get_log_reference (void) +{ + return reference; +} + +/** + * @brief Free the log reference object + * + * The reference object is used to detect corresponding logs. + * + */ +void +free_log_reference (void) +{ + g_free (reference); + reference = NULL; +} + /** * @brief Creates the formatted string and outputs it to the log destination. * @@ -524,9 +579,19 @@ gvm_log_func (const char *log_domain, GLogLevelFlags log_level, /* If the current char is a % and the next one is a p, get the pid. */ if ((*tmp == '%') && (*(tmp + 1) == 'p')) { - /* Use g_strdup, a new string returned. Store it in a tmp var until - * we free the old one. */ - prepend_tmp = g_strdup_printf ("%s%d", prepend_buf, (int) getpid ()); + if (reference) + { + prepend_tmp = + g_strdup_printf ("%s%d%s%s", prepend_buf, (int) getpid (), + log_separator, reference); + } + else + { + /* Use g_strdup, a new string returned. Store it in a tmp var + * until we free the old one. */ + prepend_tmp = + g_strdup_printf ("%s%d", prepend_buf, (int) getpid ()); + } /* Free the old string. */ g_free (prepend_buf); /* Point the buf ptr to the new string. */ diff --git a/base/logging.h b/base/logging.h index c934a3afa..0aab6d034 100644 --- a/base/logging.h +++ b/base/logging.h @@ -53,4 +53,13 @@ gvm_log_lock (void); void gvm_log_unlock (void); +void +set_log_reference (char *); + +char * +get_log_reference (void); + +void +free_log_reference (void); + #endif /* not _GVM_LOGGING_H */