1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
/*
ELAPI
Implementation of the ELAPI logging interface.
Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
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/>.
*/
#define _GNU_SOURCE
#include <sys/types.h> /* for stat() */
#include <sys/stat.h> /* for stat() */
#include <unistd.h> /* for stat() */
#include <errno.h> /* for errors */
#include <string.h> /* for memset() and other */
#include <stdarg.h> /* for va_arg() */
#include <stdlib.h> /* for free() */
#include "elapi_priv.h"
#include "elapi_event.h"
#include "elapi_log.h"
#include "ini_config.h"
#include "trace.h"
#include "config.h"
/* Pointer to default global dispatcher */
struct elapi_dispatcher *global_dispatcher = NULL;
/* Per review I was told to hard cord this name. So be it... */
#define ELAPI_CONFIG_FILE_NAME "elapi.conf"
/* Default config file */
static char default_config_file[] = ELAPI_DEFAULT_CONFIG_DIR "/" ELAPI_CONFIG_FILE_NAME;
/* Default config dir */
static char default_config_dir[] = ELAPI_DEFAULT_CONFIG_APP_DIR;
/* Was a cleanup callback registered ? */
static int elapi_close_registered = 0;
/* Internal function to log message using args */
static int elapi_dsp_msg_with_vargs(uint32_t target,
struct elapi_dispatcher *dispatcher,
struct collection_item *template,
va_list args)
{
int error = EOK;
struct collection_item *event;
TRACE_FLOW_STRING("elapi_dsp_msg_with_vargs", "Entry");
if (!dispatcher) {
TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
return EINVAL;
}
/* Create event */
error = elapi_create_event_with_vargs(&event,
template,
NULL,
0,
args);
if (error) {
TRACE_ERROR_NUMBER("Failed to create event", error);
return error;
}
/* Now log event */
error = elapi_dsp_log(target, dispatcher, event);
/* Destroy event */
elapi_destroy_event(event);
if (error) {
TRACE_ERROR_NUMBER("Failed to log event", error);
return error;
}
TRACE_FLOW_STRING("elapi_dsp_msg_with_vargs", "Exit");
return error;
}
/********** Main functions of the interface **********/
/* Function to create a dispatcher */
int elapi_create_dispatcher_adv(struct elapi_dispatcher **dispatcher,
const char *appname,
const char *config_path,
struct elapi_async_ctx *async_ctx)
{
struct elapi_dispatcher *handle = NULL;
struct collection_item *error_set = NULL;
int error = EOK;
struct collection_item *item = NULL;
const char *config_file = NULL;
const char *config_dir = NULL;
struct stat stat_data;
TRACE_FLOW_STRING("elapi_create_dispatcher_adv", "Entry point");
/* Make sure the memory for handle is passed in */
if (dispatcher == NULL) {
TRACE_ERROR_STRING("elapi_create_dispatcher_adv", "Invalid parameter.");
return EINVAL;
}
/* Make sure we got the right constant */
TRACE_INFO_NUMBER("ELAPI_DEFAULT_APP_NAME_SIZE = ", ELAPI_DEFAULT_APP_NAME_SIZE);
if ((appname != NULL) && (strlen(appname) > ELAPI_DEFAULT_APP_NAME_SIZE)) {
TRACE_ERROR_STRING("elapi_create_dispatcher", "Application name is too long.");
return EINVAL;
}
/* FIXME: Check if context is valid */
/* Check what is passed in the config_path */
if (config_path) {
/* What is it ? */
if(stat(config_path, &stat_data)) {
error = errno;
TRACE_ERROR_NUMBER("Invalid path assume defaults. Error", error);
config_file = default_config_file;
config_dir = default_config_dir;
}
else {
if (S_ISREG(stat_data.st_mode)) {
config_file = config_path;
config_dir = NULL;
TRACE_INFO_STRING("Will use config file", config_file);
}
else if (S_ISDIR(stat_data.st_mode)) {
config_file = NULL;
config_dir = config_path;
TRACE_INFO_STRING("Will use directory", config_dir);
}
else {
config_file = default_config_file;
config_dir = default_config_dir;
}
}
}
else {
config_file = default_config_file;
config_dir = default_config_dir;
}
TRACE_INFO_STRING("FILE:", config_file);
TRACE_INFO_STRING("DIR:", config_dir);
/* Allocate memory */
handle = (struct elapi_dispatcher *) calloc(1, sizeof(struct elapi_dispatcher));
if (handle == NULL) {
TRACE_ERROR_NUMBER("Memory allocation failed. Error", ENOMEM);
return ENOMEM;
}
/* Save application name in the handle */
if (appname != NULL) handle->appname = strdup(appname);
else handle->appname = strdup(ELAPI_DEFAULT_APP_NAME);
TRACE_FLOW_STRING("Application name:", handle->appname);
/* Check error */
if (handle->appname == NULL) {
TRACE_ERROR_NUMBER("Memory allocation failed. Error", ENOMEM);
elapi_destroy_dispatcher(handle);
return ENOMEM;
}
/* Read the ELAPI configuration and store it in the dispatcher handle */
error = config_for_app(handle->appname,
config_file,
config_dir,
&(handle->ini_config),
INI_STOP_ON_ANY,
&error_set);
if (error) {
TRACE_ERROR_NUMBER("Attempt to read configuration returned error", error);
elapi_destroy_dispatcher(handle);
if (error_set) {
elapi_dump_ini_err(error_set);
free_ini_config_errors(error_set);
}
return error;
}
/* Have to clean error set anyways */
free_ini_config_errors(error_set);
/* Get target list from configuration */
error = get_config_item(ELAPI_DISPATCHER,
ELAPI_TARGETS,
handle->ini_config,
&item);
if (error) {
TRACE_ERROR_NUMBER("Attempt to read configuration returned error", error);
elapi_destroy_dispatcher(handle);
return error;
}
/* Do we have targets? */
if (item == NULL) {
/* There is no list of targets this is bad configuration - return error */
TRACE_ERROR_STRING("No targets in the config file.", "Fatal error!");
elapi_destroy_dispatcher(handle);
return ENOENT;
}
/* Get one from config but make sure we free it later */
handle->targets = get_string_config_array(item, NULL, NULL, NULL);
/* Create the list of targets */
error = elapi_tgt_mklist(handle);
if (error != EOK) {
TRACE_ERROR_NUMBER("Failed to create target list. Error", error);
elapi_destroy_dispatcher(handle);
return error;
}
/* Populate async processing data if any */
if (async_ctx) {
TRACE_INFO_STRING("Async data is present", "");
handle->async_ctx = malloc(sizeof(struct elapi_async_ctx));
if (handle->async_ctx != NULL) {
TRACE_ERROR_NUMBER("Failed to allocate async context", ENOMEM);
elapi_destroy_dispatcher(handle);
return ENOMEM;
}
/* Copy async data */
memcpy(handle->async_ctx, async_ctx, sizeof(struct elapi_async_ctx));
}
else {
TRACE_INFO_STRING("No async data present", "");
handle->async_ctx = NULL;
}
*dispatcher = handle;
TRACE_FLOW_STRING("elapi_create_dispatcher_adv", "Returning Success.");
return EOK;
}
/* Simple dispatcher */
int elapi_create_dispatcher(struct elapi_dispatcher **dispatcher,
const char *appname,
const char *config_path)
{
int error = EOK;
TRACE_FLOW_STRING("elapi_create_dispatcher", "Entry.");
/* Will have more parmeters in future */
error = elapi_create_dispatcher_adv(dispatcher,
appname,
config_path,
NULL);
TRACE_FLOW_STRING("elapi_create_dispatcher", "Exit.");
return error;
}
/* Function to clean memory associated with the dispatcher */
void elapi_destroy_dispatcher(struct elapi_dispatcher *dispatcher)
{
TRACE_FLOW_STRING("elapi_destroy_dispatcher", "Entry.");
if (dispatcher) {
TRACE_INFO_STRING("Deleting template if any...", "");
col_destroy_collection(dispatcher->default_template);
if (dispatcher->target_list) {
TRACE_INFO_STRING("Closing target list.", "");
(void)col_traverse_collection(dispatcher->target_list,
COL_TRAVERSE_ONELEVEL,
elapi_tgt_free_cb,
NULL);
TRACE_INFO_STRING("Deleting target list.", "");
col_destroy_collection(dispatcher->target_list);
}
if (dispatcher->sink_list) {
TRACE_INFO_STRING("Closing sink list.", "");
(void)col_traverse_collection(dispatcher->sink_list,
COL_TRAVERSE_ONELEVEL,
elapi_sink_free_cb,
NULL);
TRACE_INFO_STRING("Deleting target list.", "");
col_destroy_collection(dispatcher->sink_list);
}
TRACE_INFO_STRING("Freeing application name.", "");
free(dispatcher->appname);
TRACE_INFO_STRING("Freeing async context.", "");
free(dispatcher->async_ctx);
TRACE_INFO_STRING("Freeing config.", "");
free_ini_config(dispatcher->ini_config);
TRACE_INFO_STRING("Deleting targets name array.", "");
free_string_config_array(dispatcher->targets);
TRACE_INFO_STRING("Freeing dispatcher.", "");
free(dispatcher);
}
TRACE_FLOW_STRING("elapi_destroy_dispatcher", "Exit.");
}
/* Function to log an event */
int elapi_dsp_log(uint32_t target,
struct elapi_dispatcher *dispatcher,
struct collection_item *event)
{
int error = EOK;
struct elapi_tgt_data target_data;
TRACE_FLOW_STRING("elapi_dsp_log", "Entry");
if ((dispatcher == NULL) ||
(event == NULL)) {
TRACE_ERROR_STRING("elapi_dsp_log", "ERROR Invalid argument");
return EINVAL;
}
/* Wrap parameters into one argument and pass on */
target_data.handle = dispatcher;
target_data.event = event;
target_data.target_mask = target;
TRACE_INFO_NUMBER("Target mask is:", target_data.target_mask);
/* Logging an event is just iterating through the targets and calling a callback */
error = col_traverse_collection(dispatcher->target_list,
COL_TRAVERSE_ONELEVEL,
elapi_tgt_cb,
(void *)(&target_data));
TRACE_FLOW_NUMBER("elapi_dsp_log Exit. Returning", error);
return error;
}
/* Initializes default internal template */
int elapi_set_default_template(unsigned base, ...)
{
int error = EOK;
struct collection_item *tpl = NULL;
va_list args;
TRACE_FLOW_STRING("elapi_set_default_template", "Entry");
if (global_dispatcher == NULL) {
error = elapi_init(NULL, NULL);
if (error) {
TRACE_ERROR_NUMBER("Failed to init ELAPI", error);
return error;
}
}
/* Clean previous instance of the default template */
elapi_destroy_event_template(global_dispatcher->default_template);
global_dispatcher->default_template = NULL;
/* Process varible arguments */
va_start(args, base);
/* Create template out of base and args */
error = elapi_create_event_template_with_vargs(&tpl,
base,
args);
va_end(args);
if (error) {
TRACE_ERROR_NUMBER("Failed to create template. Error", error);
return error;
}
global_dispatcher->default_template = tpl;
TRACE_FLOW_STRING("elapi_set_default_template", "Exit");
return error;
}
/* There is one default template associated with the dispatcher */
int elapi_get_default_template(struct collection_item **template)
{
int error = EOK;
TRACE_FLOW_STRING("elapi_get_default_template", "Entry");
if ((global_dispatcher == NULL) ||
(global_dispatcher->default_template == NULL)) {
TRACE_INFO_STRING("Default template does not exit", "");
error = elapi_set_default_template(E_BASE_DEFV1, E_EOARG);
if (error) {
TRACE_ERROR_NUMBER("Set default template returned error", error);
return error;
}
}
*template = global_dispatcher->default_template;
TRACE_FLOW_NUMBER("elapi_get_default_template. Exit returning", error);
return error;
}
/* Function to log raw key value pairs without creating an event */
int elapi_dsp_msg(uint32_t target,
struct elapi_dispatcher *dispatcher,
struct collection_item *template,
...)
{
int error = EOK;
va_list args;
TRACE_FLOW_STRING("elapi_dsp_msg", "Entry");
va_start(args, template);
error = elapi_dsp_msg_with_vargs(target, dispatcher, template, args);
va_end(args);
TRACE_FLOW_STRING("elapi_dsp_msg.", "Exit");
return error;
}
/********** Advanced dispatcher managment functions **********/
/* Managing the sink collection */
int elapi_alter_dispatcher(struct elapi_dispatcher *dispatcher,
const char *target,
const char *sink,
int action)
{
/* FIXME: FUNCTION IS NOT IMPLEMENTED YET */
return EOK;
}
/* Get sink list */
char **elapi_get_sink_list(struct elapi_dispatcher *dispatcher, char *target)
{
/* FIXME: FUNCTION IS NOT IMPLEMENTED YET */
return NULL;
}
/* Free sink list */
void elapi_free_sink_list(char **sink_list)
{
/* FIXME: FUNCTION IS NOT IMPLEMENTED YET */
}
/* Get target list */
char **elapi_get_target_list(struct elapi_dispatcher *dispatcher)
{
/* FIXME: FUNCTION IS NOT IMPLEMENTED YET */
return NULL;
}
/* Free target list */
void elapi_free_target_list(char **target_list)
{
/* FIXME: FUNCTION IS NOT IMPLEMENTED YET */
}
/******************** High level interface ************************************/
/* This interface is not thread safe but hides the dispatcher. */
/* This function will use internal default template */
int elapi_create_simple_event(struct collection_item **event, ...)
{
int error = EOK;
struct collection_item *evt = NULL;
va_list args;
struct collection_item *template = NULL;
TRACE_FLOW_STRING("elapi_create_simple_event", "Entry");
/* Check storage */
if (event == NULL ) {
TRACE_ERROR_STRING("Event storage must be provided", "");
return EINVAL;
}
*event = NULL;
/* Get default template */
error = elapi_get_default_template(&template);
if (error) {
TRACE_ERROR_NUMBER("Failed to get default template. Error", error);
return error;
}
va_start(args, event);
/* Create event */
error = elapi_create_event_with_vargs(&evt,
template,
NULL,
0,
args);
va_end(args);
if (error) {
TRACE_ERROR_NUMBER("Failed to create event using arg list. Error", error);
col_destroy_collection(evt);
return error;
}
*event = evt;
TRACE_FLOW_STRING("elapi_create_simple_event", "Exit");
return error;
}
/* Log key value pairs */
int elapi_msg(uint32_t target, struct collection_item *template, ...)
{
int error = EOK;
va_list args;
struct collection_item *use_template;
TRACE_FLOW_STRING("elapi_msg", "Entry");
if (!template) {
/* Get default template */
error = elapi_get_default_template(&use_template);
if (error) {
TRACE_ERROR_NUMBER("Failed to get default template. Error", error);
return error;
}
}
else use_template = template;
va_start(args, template);
error = elapi_dsp_msg_with_vargs(target,
global_dispatcher,
use_template,
args);
va_end(args);
TRACE_FLOW_NUMBER("elapi_msg Exit:", error);
return error;
}
/* Log event */
int elapi_log(uint32_t target, struct collection_item *event)
{
int error;
TRACE_FLOW_STRING("elapi_log", "Entry");
/* If dispatcher was not initialized do it automatically */
if (global_dispatcher == NULL) {
error = elapi_init(NULL, NULL);
if (error) {
TRACE_ERROR_NUMBER("Failed to init ELAPI", error);
return error;
}
}
error = elapi_dsp_log(target, global_dispatcher, event);
TRACE_FLOW_NUMBER("elapi_log Exit:", error);
return error;
}
/* Get dispatcher if you want to add sink to a default dispatcher or do some advanced operations */
struct elapi_dispatcher *elapi_get_dispatcher(void)
{
TRACE_FLOW_STRING("elapi_get_dispatcher was called.", "Returning default dispatcher.");
return global_dispatcher;
}
/* Close ELAPI */
void elapi_close(void)
{
TRACE_FLOW_STRING("elapi_close","Entry");
/* Destroy global dispatcher */
elapi_destroy_dispatcher(global_dispatcher);
global_dispatcher = NULL;
TRACE_FLOW_STRING("elapi_close","Exit");
}
/* Function to initialize ELAPI library in the single threaded applications */
int elapi_init(const char *appname, const char *config_path)
{
int error = EOK;
TRACE_FLOW_STRING("elapi_init","Entry");
/* Clean the dispatcher if needed */
elapi_close();
/* Create global dispatcher */
error = elapi_create_dispatcher(&global_dispatcher, appname, config_path);
if (error) {
TRACE_ERROR_NUMBER("Failed to create default dispatcher. Error", error);
return error;
}
/* Install a cleanup callback */
if (!elapi_close_registered) {
if (atexit(elapi_close)) {
TRACE_ERROR_NUMBER("Failed to install cleanup callback. Error", ENOSYS);
/* NOTE: Could not find a better error for this case */
return ENOSYS;
}
elapi_close_registered = 1;
}
TRACE_FLOW_NUMBER("elapi_init Exit:",error);
return error;
}
|