Browse Source

Add abitility to get a string representation of an event. Used as DEBUG logging of event.

1.0.0
Georg Hopp 11 years ago
parent
commit
b255fc9a7e
  1. 2
      include/tr/event.h
  2. 1
      include/tr/event_subject.h
  3. 1
      src/Makefile.am
  4. 8
      src/event_dispatcher.c
  5. 8
      src/event_handler_handle_event.c
  6. 10
      src/event_handler_issue_event.c
  7. 1
      src/event_subject.c
  8. 41
      src/get_event_string.c

2
include/tr/event.h

@ -42,6 +42,8 @@ TR_CLASSVARS_DECL(TR_Event) {
#define TR_eventSetData(event, data) (((TR_Event)event)->data = (void *)data) #define TR_eventSetData(event, data) (((TR_Event)event)->data = (void *)data)
const char * TR_getEventString(TR_Event);
#endif // __TR_EVENT_H__ #endif // __TR_EVENT_H__
// vim: set ts=4 sw=4: // vim: set ts=4 sw=4:

1
include/tr/event_subject.h

@ -34,6 +34,7 @@ TR_CLASS(TR_EventSubject) {
}; };
TR_INSTANCE_INIT(TR_EventSubject); TR_INSTANCE_INIT(TR_EventSubject);
TR_CLASSVARS_DECL(TR_EventSubject) { TR_CLASSVARS_DECL(TR_EventSubject) {
const char ** eventStrings;
size_t nevents; size_t nevents;
intptr_t * events; intptr_t * events;
}; };

1
src/Makefile.am

@ -4,6 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects
AM_CFLAGS += -I../include/ AM_CFLAGS += -I../include/
TREVENT = event.c \ TREVENT = event.c \
get_event_string.c \
event_dispatcher.c \ event_dispatcher.c \
event_dispatcher_register_handler.c \ event_dispatcher_register_handler.c \
event_dispatcher_set_hearbeat.c \ event_dispatcher_set_hearbeat.c \

8
src/event_dispatcher.c

@ -112,6 +112,13 @@ eventDispatcherCvInit(TR_class_ptr cls)
TR_EVENT_CREATE(cls, TR_DISPATCHER_EVENT_SHUTDOWN); TR_EVENT_CREATE(cls, TR_DISPATCHER_EVENT_SHUTDOWN);
} }
const char * TR_eventDispatcherStrings[] = {
"TR_DISPATCHER_EVENT_HEARTBEAT",
"TR_DISPATCHER_EVENT_USER_WAIT",
"TR_DISPATCHER_EVENT_DATA_WAIT",
"TR_DISPATCHER_EVENT_SHUTDOWN"
};
intptr_t dispatcher_events[TR_DISPATCHER_EVENT_MAX + 1]; intptr_t dispatcher_events[TR_DISPATCHER_EVENT_MAX + 1];
TR_INIT_IFACE(TR_Class, eventDispatcherCtor, eventDispatcherDtor, NULL); TR_INIT_IFACE(TR_Class, eventDispatcherCtor, eventDispatcherDtor, NULL);
TR_CREATE_CLASS( TR_CREATE_CLASS(
@ -120,6 +127,7 @@ TR_CREATE_CLASS(
eventDispatcherCvInit, eventDispatcherCvInit,
TR_IF(TR_Class)) = { TR_IF(TR_Class)) = {
{ {
TR_eventDispatcherStrings,
TR_DISPATCHER_EVENT_MAX + 1, TR_DISPATCHER_EVENT_MAX + 1,
dispatcher_events dispatcher_events
} }

8
src/event_handler_handle_event.c

@ -20,6 +20,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdio.h>
#include "trbase.h" #include "trbase.h"
#include "trdata.h" #include "trdata.h"
#include "trhash.h" #include "trhash.h"
@ -35,6 +37,12 @@ TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event)
TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(this))->event_methods, TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(this))->event_methods,
TR_sdbm((unsigned char *)&event->id, sizeof(event->id))); TR_sdbm((unsigned char *)&event->id, sizeof(event->id)));
TR_loggerLog(TR_logger, TR_LOGGER_DEBUG,
"HANDLE: %s event on %p with no. %d\n",
TR_getEventString(event),
event->subject,
event->serial);
if (! handle_func_hv) { if (! handle_func_hv) {
return 0; return 0;
} }

10
src/event_handler_issue_event.c

@ -20,6 +20,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdio.h>
#include "trbase.h" #include "trbase.h"
#include "tr/event.h" #include "tr/event.h"
@ -28,9 +30,15 @@
void void
TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event) TR_eventHandlerIssueEvent(TR_EventHandler this, TR_Event event)
{ {
if (event) {
int i; int i;
if (event) {
TR_loggerLog(TR_logger, TR_LOGGER_DEBUG,
"ISSUE: %s event on %p with no. %d\n",
TR_getEventString(event),
event->subject,
event->serial);
for (i=0; i<this->ndispatcher; i++) { for (i=0; i<this->ndispatcher; i++) {
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event);
} }

1
src/event_subject.c

@ -32,6 +32,7 @@ static void eventSubjectDtor(void * _this) {}
TR_INIT_IFACE(TR_Class, eventSubjectCtor, eventSubjectDtor, NULL); TR_INIT_IFACE(TR_Class, eventSubjectCtor, eventSubjectDtor, NULL);
TR_CREATE_CLASS(TR_EventSubject, NULL, NULL, TR_IF(TR_Class)) = { TR_CREATE_CLASS(TR_EventSubject, NULL, NULL, TR_IF(TR_Class)) = {
NULL,
0, 0,
NULL NULL
}; };

41
src/get_event_string.c

@ -0,0 +1,41 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include "tr/event.h"
#include "tr/event_subject.h"
#include "tr/logger.h"
#include "trbase.h"
const char *
TR_getEventString(TR_Event event)
{
TR_class_ptr cls = (TR_class_ptr)(event->id >> 8);
size_t idx = event->id & 0xFF;
idx -= TR_CLASSVARS(TR_EventSubject, cls->parent)->nevents;
return TR_CLASSVARS(TR_EventSubject, cls)->eventStrings[idx];
}
// vim: set ts=4 sw=4:
Loading…
Cancel
Save