1 #include "openils/oils_event.h"
2 #include <libxml/parser.h>
3 #include <libxml/tree.h>
4 #include "opensrf/osrf_settings.h"
6 static void _oilsEventParseEvents();
8 // The following two osrfHashes are created when we
9 // create the first osrfEvent, and are never freed.
11 static osrfHash* _oilsEventEvents = NULL;
12 static osrfHash* _oilsEventDescriptions = NULL;
14 oilsEvent* oilsNewEvent( const char* file, int line, const char* event ) {
15 if(!event) return NULL;
16 osrfLogInfo(OSRF_LOG_MARK, "Creating new event: %s", event);
17 if(!_oilsEventEvents) _oilsEventParseEvents();
18 oilsEvent* evt = safe_malloc(sizeof(oilsEvent));
19 evt->event = strdup(event);
24 if(file) evt->file = strdup(file);
25 else evt->file = NULL;
30 oilsEvent* oilsNewEvent2( const char* file, int line, const char* event,
31 const jsonObject* payload ) {
32 oilsEvent* evt = oilsNewEvent(file, line, event);
33 if(payload) evt->payload = jsonObjectClone(payload);
37 oilsEvent* oilsNewEvent3( const char* file, int line, const char* event,
38 const char* perm, int permloc ) {
39 oilsEvent* evt = oilsNewEvent(file, line, event);
41 evt->perm = strdup(perm);
42 evt->permloc = permloc;
47 oilsEvent* oilsNewEvent4( const char* file, int line, const char* event,
48 const char* perm, int permloc, const jsonObject* payload ) {
49 oilsEvent* evt = oilsNewEvent3( file, line, event, perm, permloc );
50 if(payload) evt->payload = jsonObjectClone(payload);
54 void oilsEventSetPermission( oilsEvent* event, const char* perm, int permloc ) {
55 if(!(event && perm)) return;
56 if(event->perm) free(event->perm);
57 event->perm = strdup(perm);
58 event->permloc = permloc;
61 void oilsEventSetPayload( oilsEvent* event, const jsonObject* payload ) {
62 if(!(event && payload)) return;
63 if(event->payload) jsonObjectFree(event->payload);
64 event->payload = jsonObjectClone(payload);
68 void oilsEventFree( oilsEvent* event ) {
73 if(event->json) jsonObjectFree(event->json);
74 /* event->json will contain a pointer to event->payload */
75 else jsonObjectFree(event->payload);
80 jsonObject* oilsEventToJSON( oilsEvent* event ) {
81 if(!event) return NULL;
82 char* code = osrfHashGet( _oilsEventEvents, event->event );
85 osrfLogError(OSRF_LOG_MARK, "No such event name: %s", event->event );
90 char* lang = "en-US"; /* assume this for now */
92 osrfHash* h = osrfHashGet(_oilsEventDescriptions, lang);
94 osrfLogDebug(OSRF_LOG_MARK, "Loaded event lang hash for %s",lang);
95 desc = osrfHashGet(h, code);
96 osrfLogDebug(OSRF_LOG_MARK, "Found event description %s", desc);
100 jsonObject* json = jsonNewObject(NULL);
101 jsonObjectSetKey( json, "ilsevent", jsonNewNumberObject(atoi(code)) );
102 jsonObjectSetKey( json, "textcode", jsonNewObject(event->event) );
103 jsonObjectSetKey( json, "desc", jsonNewObject(desc) );
104 jsonObjectSetKey( json, "pid", jsonNewNumberObject(getpid()) );
107 memset(buf, '\0', sizeof(buf));
108 snprintf(buf, sizeof(buf), "%s:%d", event->file, event->line);
109 jsonObjectSetKey( json, "stacktrace", jsonNewObject(buf) );
111 if(event->perm) jsonObjectSetKey( json, "ilsperm", jsonNewObject(event->perm) );
112 if(event->permloc != -1) jsonObjectSetKey( json, "ilspermloc", jsonNewNumberObject(event->permloc) );
113 if(event->payload) jsonObjectSetKey( json, "payload", event->payload );
115 if(event->json) jsonObjectFree(event->json);
120 /* Parses the events file */
121 static void _oilsEventParseEvents() {
123 char* xml = osrf_settings_host_value("/ils_events");
126 osrfLogError(OSRF_LOG_MARK, "Unable to find ILS Events file: %s", xml);
130 xmlDocPtr doc = xmlParseFile(xml);
133 _oilsEventEvents = osrfNewHash();
134 _oilsEventDescriptions = osrfNewHash();
137 xmlNodePtr root = xmlDocGetRootElement(doc);
139 xmlNodePtr child = root->children;
141 if( !strcmp((char*) child->name, "event") ) {
142 xmlChar* code = xmlGetProp( child, BAD_CAST "code");
143 xmlChar* textcode = xmlGetProp( child, BAD_CAST "textcode");
144 if( code && textcode ) {
145 osrfHashSet( _oilsEventEvents, code, (char*) textcode );
149 /* here we collect all of the <desc> nodes on the event
150 * element and store them based on the xml:lang attribute
152 xmlNodePtr desc = child->children;
154 if( !strcmp((char*) desc->name, "desc") ) {
155 xmlChar* lang = xmlGetProp( desc, BAD_CAST "lang");
157 osrfLogDebug(OSRF_LOG_MARK, "Loaded event lang: %s", (char*) lang);
158 osrfHash* langHash = osrfHashGet(
159 _oilsEventDescriptions, (char*) lang);
161 langHash = osrfNewHash();
162 osrfHashSet(_oilsEventDescriptions, langHash, (char*) lang);
165 if( desc->children && (content = (char*) desc->children->content) ) {
166 osrfLogDebug(OSRF_LOG_MARK, "Loaded event desc: %s", content);
167 osrfHashSet( langHash, content, (char*) code );
179 if(!success) osrfLogError(OSRF_LOG_MARK, " ! Unable to parse events file: %s", xml );