summaryrefslogtreecommitdiffstats
path: root/sinks/stderr/elapi_sink_stderr.c
blob: 7bd5bf87057b4fbd5ecf22801b55b75174a42424 (plain)
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
/*
    ELAPI

    Implemenation of the stderr sink.

    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/>.
*/


#include <stdio.h>
#include <errno.h>
#include <malloc.h>
#include "elapi_sink.h"
#include "elapi_collection.h"
#include "elapi_debug.h"
#include "elapi_util.h"


/***** Standard functions each facility has to provide *****/
/* Initialize facility - open files, establish connnectons, etc... */
static int stderr_sink_init(struct data_descriptor *dblock)
{
    struct serial_data *serialized_data;

    DEBUG_STRING("stderr_sink_init","Entry");

    /* Prepare the block where the format function will store its data */
    errno = 0;
    dblock->internal_data = NULL;
    serialized_data = malloc(sizeof(struct serial_data));
    if(serialized_data == NULL) return errno;

    serialized_data->buffer = NULL;
    serialized_data->size = 0;
    serialized_data->length = 0;
    serialized_data->nest_level = 0;

    dblock->internal_data = (void *)(serialized_data);


    DEBUG_NUMBER("DBLOCK in init",dblock);
    DEBUG_NUMBER("internal data in init",dblock->internal_data);
    DEBUG_STRING("Application name in init:",dblock->appname);

    DEBUG_STRING("stderr_sink_init","Exit");
    return EOK;
}


/* Formatting calback */ 
static int stderr_sink_format(struct data_descriptor *dblock,struct collection_item *event)
{
    struct serial_data *serialized_data;
    int error = EOK;

    DEBUG_STRING("stderr_sink_format","Entry");
    DEBUG_NUMBER("DBLOCK in format",dblock);
    DEBUG_NUMBER("internal data in format",dblock->internal_data);

    serialized_data = (struct serial_data *)(dblock->internal_data);

    /* Traverse collection */
    error = traverse_collection(event,ELAPI_TRAVERSE_DEFAULT | ELAPI_TRAVERSE_END ,serialize,(void *)(serialized_data));
    if(error) {
        DEBUG_NUMBER("traverse_collection returned error",error);
        return error;
    }

    DEBUG_STRING("stderr_sink_format","Exit");
    return EOK;
}


/* Cleanup per event internal data after a failure */
static void stderr_sink_cleanup(struct data_descriptor *dblock)
{
    struct serial_data *serialized_data;
    DEBUG_STRING("stderr_sink_cleanup","Entry");

    serialized_data = (struct serial_data *)(dblock->internal_data);

    if(serialized_data->buffer != NULL) {
        free(serialized_data->buffer);
        serialized_data->buffer = NULL;
        serialized_data->size = 0;
        serialized_data->length = 0;
        serialized_data->nest_level = 0;
    }

    DEBUG_STRING("stderr_sink_cleanup","Exit");
}

/* Close facility */ 
static void stderr_sink_close(struct data_descriptor *dblock)
{
    DEBUG_STRING("stderr_sink_close","Entry");

    if(dblock->internal_data != NULL) {
        stderr_sink_cleanup(dblock);
        free(dblock->internal_data);
        dblock->internal_data=NULL;
    }

    DEBUG_STRING("stderr_sink_close","Exit");
}


/* Logging calback */ 
static int stderr_sink_submit(struct data_descriptor *dblock)
{
    struct serial_data *serialized_data;
    DEBUG_STRING("stderr_sink_submit","Entry");
    DEBUG_NUMBER("DBLOCK in submit",dblock);
    DEBUG_NUMBER("internal data in submit",dblock->internal_data);

    serialized_data = (struct serial_data *)(dblock->internal_data);

    DEBUG_STRING("OUTPUT:",serialized_data->buffer);

    fprintf(stderr,"%s\n",serialized_data->buffer);

    stderr_sink_cleanup(dblock);

    DEBUG_STRING("stderr_sink_submit","Exit");
    return EOK;
}

/* Return a filled in structure */
void get_sink_info(struct sink_capability *sink_cpb_block)
{
    DEBUG_STRING("get_sink_info","Entry");

    sink_cpb_block->retry_interval = SINK_NEVER_RETRY;
    sink_cpb_block->flags = SINK_FLAG_NO_LIMIT;
    /* Instance never changes for this sink so one can load as many as he needs */
    sink_cpb_block->instance = 0;
    sink_cpb_block->init_cb = stderr_sink_init;
    sink_cpb_block->cleanup_cb = stderr_sink_cleanup;
    sink_cpb_block->format_cb = stderr_sink_format;
    sink_cpb_block->submit_cb = stderr_sink_submit;
    sink_cpb_block->close_cb = stderr_sink_close;


    DEBUG_STRING("get_sink_info","Exit");    
}