By adding this hook to bus.c we can retrieve a unique id for each log
message, allowing us to uniquely identify log messages.
Signed-off-by: Thomas Egerer <[email protected]>
---
src/libcharon/bus/bus.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++-
src/libcharon/bus/bus.h | 13 ++++++++++++
2 files changed, 61 insertions(+), 1 deletions(-)
diff --git a/src/libcharon/bus/bus.c b/src/libcharon/bus/bus.c
index f395431..2972419 100644
--- a/src/libcharon/bus/bus.c
+++ b/src/libcharon/bus/bus.c
@@ -66,6 +66,11 @@ struct private_bus_t {
* Thread local storage the threads IKE_SA
*/
thread_value_t *thread_sa;
+
+ /**
+ * Error message unification hook.
+ */
+ error_unifier_t *error_unifier;
};
typedef struct entry_t entry_t;
@@ -106,6 +111,17 @@ struct log_entry_t {
};
+static u_int64_t return_0(error_unifier_t *this, const char *format)
+{
+ (void)this;
+ (void)format;
+ return 0;
+}
+
+static error_unifier_t error_unifier_empty = {
+ .get_id_by_format = return_0,
+};
+
METHOD(bus_t, add_listener, void,
private_bus_t *this, listener_t *listener)
{
@@ -306,7 +322,8 @@ METHOD(bus_t, vlog, void,
data.group = group;
data.level = level;
data.message = buf;
- data.id = 0;
+ data.id = this->error_unifier->get_id_by_format(this->error_unifier,
+ format);
va_copy(copy, args);
len = vsnprintf(data.message, sizeof(buf), format, copy);
@@ -763,6 +780,30 @@ METHOD(bus_t, narrow, void,
this->mutex->unlock(this->mutex);
}
+METHOD(bus_t, set_error_unifier, error_unifier_t *,
+ private_bus_t *this, error_unifier_t *unifier)
+{
+ error_unifier_t *old;
+
+ this->mutex->lock(this->mutex);
+ old = this->error_unifier;
+ if (NULL != unifier)
+ {
+ this->error_unifier = unifier;
+ }
+ else
+ {
+ this->error_unifier = &error_unifier_empty;
+ }
+ this->mutex->unlock(this->mutex);
+
+ if (&error_unifier_empty == old) {
+ old = NULL;
+ }
+
+ return old;
+}
+
METHOD(bus_t, destroy, void,
private_bus_t *this)
{
@@ -777,6 +818,10 @@ METHOD(bus_t, destroy, void,
this->thread_sa->destroy(this->thread_sa);
this->log_lock->destroy(this->log_lock);
this->mutex->destroy(this->mutex);
+ if (this->error_unifier != &error_unifier_empty)
+ {
+ this->error_unifier->destroy(this->error_unifier);
+ }
free(this);
}
@@ -811,12 +856,14 @@ bus_t *bus_create()
.child_rekey = _child_rekey,
.authorize = _authorize,
.narrow = _narrow,
+ .set_error_unifier = _set_error_unifier,
.destroy = _destroy,
},
.listeners = linked_list_create(),
.mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
.log_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
.thread_sa = thread_value_create(NULL),
+ .error_unifier = &error_unifier_empty,
);
for (group = 0; group <= DBG_MAX; group++)
diff --git a/src/libcharon/bus/bus.h b/src/libcharon/bus/bus.h
index 4645bbd..fd88ff6 100644
--- a/src/libcharon/bus/bus.h
+++ b/src/libcharon/bus/bus.h
@@ -34,6 +34,7 @@ typedef struct bus_t bus_t;
#include <processing/jobs/job.h>
#include <bus/listeners/logger.h>
#include <bus/listeners/listener.h>
+#include <bus/error_unifier/error_unifier.h>
/* undefine the definitions from libstrongswan */
#undef DBG0
@@ -384,6 +385,18 @@ struct bus_t {
void (*child_rekey)(bus_t *this, child_sa_t *old, child_sa_t *new);
/**
+ * Set error code unifier.
+ *
+ * @param unifier handler to unify error strings into unique ids;
+ * if unifier is NULL, the default unifier (which
+ * does nothing) is used;
+ * @return old error unifier for destruction, NULL if the
+ * default unifier had been used before; returned
+ * object must be destroyed;
+ */
+ error_unifier_t *(*set_error_unifier)(bus_t *this, error_unifier_t *unifier);
+
+ /**
* Destroy the event bus.
*/
void (*destroy) (bus_t *this);
_______________________________________________
Dev mailing list
[email protected]
https://lists.strongswan.org/mailman/listinfo/dev