Skip to content

Commit

Permalink
Add: the possibility to set reference value for logs (#735)
Browse files Browse the repository at this point in the history
To be able to detect corresponding log messages it is possible to set a reference value, which is then displayed in logs afterwards.
  • Loading branch information
Kraemii authored Feb 3, 2023
1 parent ca0f2a5 commit a233429
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
71 changes: 68 additions & 3 deletions base/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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. */
Expand Down
9 changes: 9 additions & 0 deletions base/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit a233429

Please sign in to comment.