Add a print method for stroke counters
authorMartin Willi <martin@revosec.ch>
Mon, 8 Oct 2012 09:59:20 +0000 (11:59 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 24 Oct 2012 09:34:31 +0000 (11:34 +0200)
src/libcharon/plugins/stroke/stroke_counter.c
src/libcharon/plugins/stroke/stroke_counter.h

index d120098..6e30a9f 100644 (file)
@@ -199,6 +199,27 @@ METHOD(listener_t, message_hook, bool,
        return TRUE;
 }
 
+METHOD(stroke_counter_t, print, void,
+       private_stroke_counter_t *this, FILE *out)
+{
+       u_int64_t counter[COUNTER_MAX];
+       int i;
+
+       /* Take a snapshot to have congruent results, */
+       this->lock->lock(this->lock);
+       for (i = 0; i < countof(this->counter); i++)
+       {
+               counter[i] = this->counter[i];
+       }
+       this->lock->unlock(this->lock);
+
+       /* but do blocking write without the lock. */
+       for (i = 0; i < countof(this->counter); i++)
+       {
+               fprintf(out, "%-18N %12llu\n", stroke_counter_type_names, i, counter[i]);
+       }
+}
+
 METHOD(stroke_counter_t, destroy, void,
        private_stroke_counter_t *this)
 {
@@ -221,6 +242,7 @@ stroke_counter_t *stroke_counter_create()
                                .child_rekey = _child_rekey,
                                .message = _message_hook,
                        },
+                       .print = _print,
                        .destroy = _destroy,
                },
                .lock = spinlock_create(),
index fecdce4..efaae0d 100644 (file)
@@ -84,6 +84,13 @@ struct stroke_counter_t {
        listener_t listener;
 
        /**
+        * Print counter values to an output stream.
+        *
+        * @param out           output stream to write to
+        */
+       void (*print)(stroke_counter_t *this, FILE *out);
+
+       /**
         * Destroy a stroke_counter_t.
         */
        void (*destroy)(stroke_counter_t *this);