Selaa lähdekoodia

file_out: Prefix can use PVs

Xenofon Karamanos 1 vuosi sitten
vanhempi
commit
06d0596ef3
3 muutettua tiedostoa jossa 67 lisäystä ja 5 poistoa
  1. 50 3
      src/modules/file_out/file_out.c
  2. 16 2
      src/modules/file_out/types.c
  3. 1 0
      src/modules/file_out/types.h

+ 50 - 3
src/modules/file_out/file_out.c

@@ -62,6 +62,8 @@ static int fo_add_filename(modparam_t type, void *val);
 static int fo_parse_filename_params(str input);
 
 /* Default parameters */
+static int buf_size = 4096;
+
 char *fo_base_folder = "/var/log/kamailio/file_out";
 char *fo_base_filename[FO_MAX_FILES] = {""};
 char *fo_extension[FO_MAX_FILES] = {".out"};
@@ -69,6 +71,9 @@ char *fo_prefix[FO_MAX_FILES] = {""};
 int fo_interval_seconds[FO_MAX_FILES] = {10 * 60};
 int fo_worker_usleep = 10000;
 
+pv_elem_t *fo_prefix_pvs[FO_MAX_FILES] = {NULL};
+char *fo_prefix_buf = NULL;
+
 /* Shared variables */
 fo_queue_t *fo_queue = NULL;
 int *fo_number_of_files = NULL;
@@ -123,6 +128,25 @@ static int mod_init(void)
 	/* Count the given files */
 	*fo_number_of_files = fo_count_assigned_files();
 
+	/* Fixup the prefix */
+	for(int i = 0; i < *fo_number_of_files; i++) {
+		str s;
+		s.s = fo_prefix[i];
+		s.len = strlen(s.s);
+
+		if(pv_parse_format(&s, &fo_prefix_pvs[i]) < 0) {
+			LM_ERR("wrong format[%s]\n", s.s);
+			return -1;
+		}
+		LM_ERR("prefix_pvs = %s\n", fo_prefix_pvs[i]->text.s);
+
+		fo_prefix_buf = (char *)pkg_malloc((buf_size + 1) * sizeof(char));
+		if(fo_prefix_buf == NULL) {
+			pkg_free(fo_prefix_buf);
+			PKG_MEM_ERROR;
+			return -1;
+		}
+	}
 	/* Initialize per process vars */
 	for(int i = 0; i < *fo_number_of_files; i++) {
 		fo_stored_timestamp[i] = time(NULL);
@@ -186,6 +210,13 @@ static void destroy(void)
 		}
 	}
 
+	if(fo_prefix_buf)
+		pkg_free(fo_prefix_buf);
+
+	for(int i = 0; i < *fo_number_of_files; i++) {
+		if(fo_prefix_pvs[i])
+			pv_elem_free_all(fo_prefix_pvs[i]);
+	}
 	/* Free allocated mem */
 	if(fo_number_of_files != NULL) {
 		shm_free(fo_number_of_files);
@@ -214,8 +245,10 @@ static void fo_log_writer_process(int rank)
 		}
 
 		/* Get prefix for the file */
-		if(fo_prefix[log_message.dest_file] != NULL) {
-			if(fprintf(out, "%s", fo_prefix[log_message.dest_file]) < 0) {
+		if(log_message.prefix != NULL && log_message.prefix->len > 0) {
+			if(fprintf(out, "%.*s", log_message.prefix->len,
+					   log_message.prefix->s)
+					< 0) {
 				LM_ERR("Failed to write prefix to file with err {%s}\n",
 						strerror(errno));
 			}
@@ -528,8 +561,22 @@ static int fo_write_to_file(sip_msg_t *msg, char *index, char *log_message)
 		return -1;
 	}
 
+	str fo_prefix_str, fo_prefix_val;
+	fo_prefix_str.s = fo_prefix_buf;
+	fo_prefix_str.len = buf_size;
+	if(pv_printf(msg, fo_prefix_pvs[file_index], fo_prefix_str.s,
+			   &fo_prefix_str.len)
+					== 0
+			&& fo_prefix_str.len > 0) {
+		fo_prefix_val.s = fo_prefix_str.s;
+		fo_prefix_val.len = fo_prefix_str.len;
+	}
+
+	LM_ERR("fo_prefix_val = %.*s\n", fo_prefix_val.len, fo_prefix_val.s);
+
 	/* Add the logging string to the global gueue */
-	fo_log_message_t logMessage = {0, 0};
+	fo_log_message_t logMessage = {0, 0, 0};
+	logMessage.prefix = &fo_prefix_val;
 	logMessage.message = &value;
 	logMessage.dest_file = file_index;
 	fo_enqueue(fo_queue, logMessage);

+ 16 - 2
src/modules/file_out/types.c

@@ -39,18 +39,32 @@ int fo_enqueue(fo_queue_t *q, fo_log_message_t data)
 	Copy the contents of data.message
     */
 	str *message_copy = 0;
+	str *prefix_copy = 0;
+	/*
+	 * Allocate memory for the message and prefix
+	 */
 	message_copy = (str *)shm_malloc(sizeof(str));
 	if(message_copy == 0) {
 		SHM_MEM_ERROR;
 		return -1;
 	}
-
 	if(shm_str_dup(message_copy, data.message) < 0) {
 		LM_ERR("Failed to duplicate message\n");
 		return -1;
 	}
-
 	data.message = message_copy;
+
+	prefix_copy = (str *)shm_malloc(sizeof(str));
+	if(prefix_copy == 0) {
+		SHM_MEM_ERROR;
+		return -1;
+	}
+	if(shm_str_dup(prefix_copy, data.prefix) < 0) {
+		LM_ERR("Failed to duplicate prefix\n");
+		return -1;
+	}
+	data.prefix = prefix_copy;
+
 	fo_node_t *temp = fo_new_node(data);
 
 	lock_get(&(q->lock));

+ 1 - 0
src/modules/file_out/types.h

@@ -22,6 +22,7 @@
 
 typedef struct log_message
 {
+	str *prefix;
 	str *message;
 	int dest_file;
 } fo_log_message_t;