summaryrefslogtreecommitdiffstats
path: root/sinks/file/elapi_sink_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'sinks/file/elapi_sink_file.c')
-rw-r--r--sinks/file/elapi_sink_file.c269
1 files changed, 115 insertions, 154 deletions
diff --git a/sinks/file/elapi_sink_file.c b/sinks/file/elapi_sink_file.c
index 057bb34..660aba3 100644
--- a/sinks/file/elapi_sink_file.c
+++ b/sinks/file/elapi_sink_file.c
@@ -27,17 +27,14 @@
#include "elapi_util.h"
#include "elapi_ini.h"
-/* Global variable - instance */
-static int instance = 0;
-
/* FIXME - this should be taken from the config.h generated by autotools */
#define FILE_RETRY 60
/*
-#define FILE_AUDIT_CONFIG "/etc/elapi/syslog_defaults.conf"
-#define FILE_AUDIT_DIR "/etc/elapi/syslog_defaults.d"
+#define FILE_AUDIT_CONFIG "/etc/elapi/file_defaults.conf"
+#define FILE_AUDIT_DIR "/etc/elapi/file_defaults.d"
*/
-#define FILE_AUDIT_CONFIG "/home/dpal/IPA/Code/elapi/etc/syslog_defaults.conf"
-#define FILE_AUDIT_DIR "/home/dpal/IPA/Code/elapi/etc/syslog_defaults.d"
+#define FILE_AUDIT_CONFIG "/home/dpal/IPA/Code/elapi/etc/file_defaults.conf"
+#define FILE_AUDIT_DIR "/home/dpal/IPA/Code/elapi/etc/file_defaults.d"
/* FIXME there is currently no code
@@ -49,114 +46,75 @@ static int instance = 0;
*/
-struct syslog_event {
- struct serial_data sd;
- int priority;
-};
-
-
/* Default conmfiguration for syslog */
-struct syslog_conf {
- int option;
- int facility;
- int level;
- char *ident;
+struct file_conf {
+ int keep_open;
+ char *file_name;
+ FILE *file;
};
/* Internal function to intialize configuration */
static int init_config(struct data_descriptor *dblock)
{
- struct syslog_conf *conf_data;
- struct collection_item *file_config = (struct collection_item *)(NULL);
+ struct file_conf *conf_data;
+ struct collection_item *config_from_file = (struct collection_item *)(NULL);
int found = 0;
- int *option_cfg = (int *)(NULL);
- int *facility_cfg = (int *)(NULL);
- int *level_cfg = (int *)(NULL);
- char *ident_cfg = NULL;
+ int *keep_open_cfg = (int *)(NULL);
+ char *file_name_cfg = NULL;
int error;
DEBUG_STRING("init_config","Entry");
/* Allocate configuration data */
- conf_data = (struct syslog_conf *)(malloc(sizeof(struct syslog_conf)));
- if(conf_data == (struct syslog_conf *)(NULL)) return errno;
+ conf_data = (struct file_conf *)(malloc(sizeof(struct file_conf)));
+ if(conf_data == (struct file_conf *)(NULL)) return errno;
/* Read configuration from the configuration file if any */
- (void)config_to_collection(dblock->appname, SYSLOG_AUDIT_CONFIG, SYSLOG_AUDIT_DIR, &file_config);
+ (void)config_to_collection(dblock->appname, FILE_AUDIT_CONFIG, FILE_AUDIT_DIR, &config_from_file);
- conf_data->option = LOG_ODELAY;
- conf_data->facility = LOG_USER;
- conf_data->level = LOG_INFO;
- conf_data->ident = NULL;
+ conf_data->keep_open = 1;
+ conf_data->file_name = NULL;
- DEBUG_NUMBER("Option",conf_data->option);
- DEBUG_NUMBER("Facility",conf_data->facility);
- DEBUG_NUMBER("Level",conf_data->level);
- DEBUG_STRING("Identity",conf_data->ident);
+ DEBUG_NUMBER("Keep open:",conf_data->keep_open);
+ DEBUG_STRING("File name:",conf_data->file_name);
/* Update defaults with settings from the file */
- error = get_value_from_config((void *)(&option_cfg),ELAPI_TYPE_INTEGER, INI_DEFAULT_SECTION,"option",file_config);
- if(error != EOK) {
- /* There is fundamentally something wrong */
- DEBUG_NUMBER("Attempt to get option returned error",error);
- return error;
- }
-
- /* Get the value */
- if(option_cfg != (int *)(NULL)) {
- conf_data->option = *option_cfg;
- free((void *)(option_cfg));
- }
-
- error = get_value_from_config((void *)(&facility_cfg),ELAPI_TYPE_INTEGER, INI_DEFAULT_SECTION,"facility",file_config);
- if(error != EOK) {
- /* There is fundamentally something wrong */
- DEBUG_NUMBER("Attempt to get option returned error",error);
- return error;
- }
-
- /* Get the value */
- if(facility_cfg != (int *)(NULL)) {
- conf_data->facility = *facility_cfg;
- free((void *)(facility_cfg));
- }
-
- error = get_value_from_config((void *)(&level_cfg),ELAPI_TYPE_INTEGER, INI_DEFAULT_SECTION,"level",file_config);
+ error = get_value_from_config((void *)(&keep_open_cfg),ELAPI_TYPE_INTEGER, INI_DEFAULT_SECTION,"keep_open",config_from_file);
if(error != EOK) {
/* There is fundamentally something wrong */
DEBUG_NUMBER("Attempt to get option returned error",error);
+ free((void *)(conf_data));
return error;
}
/* Get the value */
- if(level_cfg != (int *)(NULL)) {
- conf_data->level = *level_cfg;
- free((void *)(level_cfg));
+ if(keep_open_cfg != (int *)(NULL)) {
+ conf_data->keep_open = *keep_open_cfg;
+ free((void *)(keep_open_cfg));
}
- error = get_value_from_config((void *)(&ident_cfg),ELAPI_TYPE_STRING, INI_DEFAULT_SECTION,"identity",file_config);
+ error = get_value_from_config((void *)(&file_name_cfg),ELAPI_TYPE_STRING, INI_DEFAULT_SECTION,"file_name",config_from_file);
if(error != EOK) {
/* There is fundamentally something wrong */
DEBUG_NUMBER("Attempt to get option returned error",error);
+ free((void *)(conf_data));
return error;
}
/* Get the value */
- if(ident_cfg != (char *)(NULL)) {
- conf_data->ident = ident_cfg;
+ if(file_name_cfg != (char *)(NULL)) {
+ conf_data->file_name = file_name_cfg;
}
-
- DEBUG_NUMBER("Option (after conf file):",conf_data->option);
- DEBUG_NUMBER("Facility (after conf file):",conf_data->facility);
- DEBUG_NUMBER("Level (after conf file):",conf_data->level);
- DEBUG_STRING("Identity (after conf file):",conf_data->ident);
+ DEBUG_NUMBER("Keep open (after conf file):",conf_data->keep_open);
+ DEBUG_STRING("File name (after conf file):",conf_data->file_name);
dblock->config = (void *)(conf_data);
- openlog(conf_data->ident,conf_data->option,conf_data->facility);
- instance++;
- DEBUG_NUMBER("syslog instance",instance);
+ if((conf_data->file_name != NULL) && (conf_data->keep_open != 0)) {
+ file_conf->file = fopen(conf_data->file_name,"w");
+ }
+ else conf_data->file_name = stderr;
DEBUG_STRING("init_config","Entry");
return EOK;
@@ -164,23 +122,23 @@ static int init_config(struct data_descriptor *dblock)
/***** Standard functions each facility has to provide *****/
/* Initialize facility - open files, establish connnectons, etc... */
-static int syslog_sink_init(struct data_descriptor *dblock)
+static int file_sink_init(struct data_descriptor *dblock)
{
- struct syslog_event *event_storage;
+ struct serial_data *event_storage;
int error;
- DEBUG_STRING("syslog_sink_init","Entry");
+ DEBUG_STRING("file_sink_init","Entry");
/* Prepare the block where the format function will store its data */
errno = 0;
dblock->internal_data = NULL;
- event_storage = (struct syslog_event *)(malloc(sizeof(struct syslog_event)));
- if(event_storage == (struct syslog_event *)(NULL)) return errno;
+ event_storage = (struct serail_data *)(malloc(sizeof(struct serial_data)));
+ if(event_storage == (struct serail_data *)(NULL)) return errno;
- event_storage->sd.buffer = NULL;
- event_storage->sd.size = 0;
- event_storage->sd.length = 0;
- event_storage->sd.nest_level = 0;
+ event_storage->buffer = NULL;
+ event_storage->size = 0;
+ event_storage->length = 0;
+ event_storage->nest_level = 0;
dblock->internal_data = (void *)(event_storage);
@@ -195,119 +153,122 @@ static int syslog_sink_init(struct data_descriptor *dblock)
DEBUG_NUMBER("DBLOCK in init",dblock);
DEBUG_NUMBER("internal data in init",dblock->internal_data);
- DEBUG_STRING("syslog_sink_init","Exit");
+ DEBUG_STRING("file_sink_init","Exit");
return EOK;
}
/* Formatting calback */
-static int syslog_sink_format(struct data_descriptor *dblock,struct collection_item *event)
+static int file_sink_format(struct data_descriptor *dblock,
+ char *format_str,
+ struct collection_item *event)
{
- struct syslog_event *event_storage;
- struct syslog_conf *config;
- struct collection_item *item;
+ struct serial_data *serialized_data;
int error = EOK;
- DEBUG_STRING("syslog_sink_format","Entry");
+ DEBUG_STRING("file_sink_format","Entry");
DEBUG_NUMBER("DBLOCK in format",dblock);
DEBUG_NUMBER("internal data in format",dblock->internal_data);
- event_storage = (struct syslog_event *)(dblock->internal_data);
- config = (struct syslog_conf *)(dblock->config);
+ serialized_data = (struct serial_data *)(dblock->internal_data);
- event_storage->priority = config->level | config->facility;
-
- /* Get Priority */
- error = get_item(event,"priority",ELAPI_TYPE_INTEGER,ELAPI_TRAVERSE_ONELEVEL, &item);
- if(error) {
- DEBUG_NUMBER("Failed to get item from collection",error);
- /* Fall through */
- } else if((item != (struct collection_item *)(NULL)) &&
- (item->type == ELAPI_TYPE_INTEGER))
- event_storage->priority = *((int *)(item->data));
-
-
- /* FIXME */
-
-
- /* Traverse collection */
- error = traverse_collection(event,ELAPI_TRAVERSE_DEFAULT | ELAPI_TRAVERSE_END ,serialize,(void *)(&(event_storage->sd)));
+ if(format_str != NULL) {
+ /* Use format string */
+ DEBUG_STRING("Using format:",format_str);
+ error = serialize_with_format(event,format_str,serialized_data);
+ }
+ else {
+ /* Traverse collection */
+ DEBUG_STRING("Using default serialization callback","");
+ error = traverse_collection(event,ELAPI_TRAVERSE_DEFAULT | ELAPI_TRAVERSE_END ,serialize,(void *)(serialized_data));
+ }
if(error) {
- DEBUG_NUMBER("traverse_collection returned error",error);
+ DEBUG_NUMBER("Serialization returned error",error);
return error;
}
- DEBUG_STRING("syslog_sink_format","Exit");
+ DEBUG_STRING("stderr_sink_format","Exit");
return EOK;
}
/* Cleanup per event internal data after a failure */
-static void syslog_sink_cleanup(struct data_descriptor *dblock)
+static void file_sink_cleanup(struct data_descriptor *dblock)
{
- struct syslog_event *event_storage;
- struct syslog_conf *config;
+ struct serial_data *event_storage;
- DEBUG_STRING("syslog_sink_cleanup","Entry");
+ DEBUG_STRING("file_sink_cleanup","Entry");
- event_storage = (struct syslog_event *)(dblock->internal_data);
- config = (struct syslog_conf *)(dblock->config);
+ event_storage = (struct file_event *)(dblock->internal_data);
- if(event_storage->sd.buffer != NULL) {
- free(event_storage->sd.buffer);
- event_storage->sd.buffer = NULL;
- event_storage->sd.size = 0;
- event_storage->sd.length = 0;
- event_storage->sd.nest_level = 0;
- event_storage->priority = config->level | config->facility;
+ if(event_storage->buffer != NULL) {
+ free(event_storage->buffer);
+ event_storage->buffer = NULL;
+ event_storage->size = 0;
+ event_storage->length = 0;
+ event_storage->nest_level = 0;
}
- DEBUG_STRING("syslog_sink_cleanup","Exit");
+ DEBUG_STRING("file_sink_cleanup","Exit");
}
/* Close facility */
-static void syslog_sink_close(struct data_descriptor *dblock)
+static void file_sink_close(struct data_descriptor *dblock)
{
- struct syslog_conf *config;
+ struct file_conf *config;
- DEBUG_STRING("syslog_sink_close","Entry");
+ DEBUG_STRING("file_sink_close","Entry");
if(dblock->internal_data != NULL) {
- syslog_sink_cleanup(dblock);
+ file_sink_cleanup(dblock);
free(dblock->internal_data);
- config = (struct syslog_conf *)(dblock->config);
- if(config->ident != NULL) free(config->ident);
- free(dblock->config);
dblock->internal_data=NULL;
+ }
+
+ if(dblock->config != NULL) {
+ config = (struct file_conf *)(dblock->config);
+
+ if((conf_data->file_name != NULL) && (conf_data->keep_open != 0)) {
+ close(file_conf->file);
+ }
+
+ if(config->file_name != NULL) free(config->file_name);
+ free(dblock->config);
dblock->config=NULL;
}
- closelog();
- DEBUG_STRING("Closed syslog","");
- if(instance) instance--;
- DEBUG_NUMBER("syslog instance",instance);
- DEBUG_STRING("syslog_sink_close","Exit");
+ DEBUG_STRING("file_sink_close","Exit");
}
/* Logging calback */
-static int syslog_sink_submit(struct data_descriptor *dblock)
+static int file_sink_submit(struct data_descriptor *dblock)
{
- struct syslog_event *event_storage;
+ struct serial_data *event_storage;
+ struct file_conf *config;
- DEBUG_STRING("syslog_sink_submit","Entry");
+ DEBUG_STRING("file_sink_submit","Entry");
DEBUG_NUMBER("DBLOCK in submit",dblock);
DEBUG_NUMBER("internal data in submit",dblock->internal_data);
- event_storage = (struct syslog_event *)(dblock->internal_data);
+ event_storage = (struct file_event *)(dblock->internal_data);
+ config = (struct file_conf *)(dblock->config);
- DEBUG_STRING("OUTPUT:",event_storage->sd.buffer);
+ DEBUG_STRING("OUTPUT:",event_storage->buffer);
+
+ if((conf_data->file_name != NULL) && (conf_data->keep_open == 0)) {
+ file_conf->file = fopen(conf_data->file_name,"w");
+ }
- syslog(event_storage->priority, "%s", event_storage->sd.buffer);
+ fprintf(file_conf->file, "%s\n", event_storage->buffer);
+
+ if((conf_data->file_name != NULL) && (conf_data->keep_open == 0)) {
+ close(file_conf->file);
+ }
- syslog_sink_cleanup(dblock);
+ file_sink_cleanup(dblock);
- DEBUG_STRING("syslog_sink_submit","Exit");
+ DEBUG_STRING("file_sink_submit","Exit");
return EOK;
}
@@ -316,14 +277,14 @@ void get_sink_info(struct sink_capability *sink_cpb_block)
{
DEBUG_STRING("get_sink_info","Entry");
- sink_cpb_block->retry_interval = SYSLOG_RETRY;
- sink_cpb_block->flags = SINK_FLAG_LOAD_SINGLE;
- sink_cpb_block->instance = instance;
- sink_cpb_block->init_cb = syslog_sink_init;
- sink_cpb_block->cleanup_cb = syslog_sink_cleanup;
- sink_cpb_block->format_cb = syslog_sink_format;
- sink_cpb_block->submit_cb = syslog_sink_submit;
- sink_cpb_block->close_cb = syslog_sink_close;
+ sink_cpb_block->retry_interval = FILE_RETRY;
+ sink_cpb_block->flags = SINK_FLAG_NO_LIMIT;
+ sink_cpb_block->instance = 0;
+ sink_cpb_block->init_cb = file_sink_init;
+ sink_cpb_block->cleanup_cb = file_sink_cleanup;
+ sink_cpb_block->format_cb = file_sink_format;
+ sink_cpb_block->submit_cb = file_sink_submit;
+ sink_cpb_block->close_cb = file_sink_close;
DEBUG_STRING("get_sink_info","Exit");
}