From 91122e11da8a3d595beb413e5086ce7b481c9034 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Tue, 4 Nov 2014 17:13:27 +0100 Subject: [PATCH] Improve log collection API: - Allow setting a prefix for the log collection filenames - Allow defining the max file size of the log collection files --- coreapi/linphonecore.c | 122 ++++++++++++++++++++++++++++++++--------- coreapi/linphonecore.h | 42 ++++++++++++++ 2 files changed, 139 insertions(+), 25 deletions(-) diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 661604ab0..ef81ef8c1 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -56,7 +56,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #endif #ifdef HAVE_ZLIB -#define COMPRESSED_LOG_COLLECTION_FILENAME "linphone_log.gz" +#define COMPRESSED_LOG_COLLECTION_EXTENSION "gz" #ifdef WIN32 #include #include @@ -66,8 +66,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #endif #include #else -#define COMPRESSED_LOG_COLLECTION_FILENAME "linphone_log.txt" +#define COMPRESSED_LOG_COLLECTION_EXTENSION "txt" #endif +#define LOG_COLLECTION_DEFAULT_PATH "." +#define LOG_COLLECTION_DEFAULT_PREFIX "linphone" +#define LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE (10 * 1024 * 1024) + /*#define UNSTANDART_GSM_11K 1*/ @@ -81,8 +85,10 @@ static const char *liblinphone_version= #endif ; static OrtpLogFunc liblinphone_log_func = NULL; -static bool_t liblinphone_log_collection_enabled = FALSE; +static LinphoneLogCollectionState liblinphone_log_collection_state = LinphoneLogCollectionDisabled; static char * liblinphone_log_collection_path = NULL; +static char * liblinphone_log_collection_prefix = NULL; +static int liblinphone_log_collection_max_file_size = LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE; static ortp_mutex_t liblinphone_log_collection_mutex; static bool_t liblinphone_serialize_logs = FALSE; static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime); @@ -164,8 +170,6 @@ void linphone_core_set_log_level(OrtpLogLevel loglevel) { } } -#define LOGFILE_MAXSIZE (10 * 1024 * 1024) - static void linphone_core_log_collection_handler(OrtpLogLevel level, const char *fmt, va_list args) { const char *lname="undef"; char *msg; @@ -205,16 +209,20 @@ static void linphone_core_log_collection_handler(OrtpLogLevel level, const char } msg = ortp_strdup_vprintf(fmt, args); - log_filename1 = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone1.log"); - log_filename2 = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone2.log"); + log_filename1 = ortp_strdup_printf("%s/%s1.log", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX); + log_filename2 = ortp_strdup_printf("%s/%s2.log", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX); ortp_mutex_lock(&liblinphone_log_collection_mutex); log_file = fopen(log_filename1, "a"); fstat(fileno(log_file), &statbuf); - if (statbuf.st_size > LOGFILE_MAXSIZE) { + if (statbuf.st_size > liblinphone_log_collection_max_file_size) { fclose(log_file); log_file = fopen(log_filename2, "a"); fstat(fileno(log_file), &statbuf); - if (statbuf.st_size > LOGFILE_MAXSIZE) { + if (statbuf.st_size > liblinphone_log_collection_max_file_size) { fclose(log_file); unlink(log_filename1); rename(log_filename2, log_filename1); @@ -232,6 +240,13 @@ static void linphone_core_log_collection_handler(OrtpLogLevel level, const char ortp_free(msg); } +const char * linphone_core_get_log_collection_path(void) { + if (liblinphone_log_collection_path != NULL) { + return liblinphone_log_collection_path; + } + return LOG_COLLECTION_DEFAULT_PATH; +} + void linphone_core_set_log_collection_path(const char *path) { if (liblinphone_log_collection_path != NULL) { ms_free(liblinphone_log_collection_path); @@ -242,6 +257,31 @@ void linphone_core_set_log_collection_path(const char *path) { } } +const char * linphone_core_get_log_collection_prefix(void) { + if (liblinphone_log_collection_prefix != NULL) { + return liblinphone_log_collection_prefix; + } + return LOG_COLLECTION_DEFAULT_PREFIX; +} + +void linphone_core_set_log_collection_prefix(const char *prefix) { + if (liblinphone_log_collection_prefix != NULL) { + ms_free(liblinphone_log_collection_prefix); + liblinphone_log_collection_prefix = NULL; + } + if (prefix != NULL) { + liblinphone_log_collection_prefix = ms_strdup(prefix); + } +} + +int linphone_core_get_log_collection_max_file_size(void) { + return liblinphone_log_collection_max_file_size; +} + +void linphone_core_set_log_collection_max_file_size(int size) { + liblinphone_log_collection_max_file_size = size; +} + const char *linphone_core_get_log_collection_upload_server_url(LinphoneCore *core) { return lp_config_get_string(core->config, "misc", "log_collection_upload_server_url", NULL); } @@ -250,14 +290,18 @@ void linphone_core_set_log_collection_upload_server_url(LinphoneCore *core, cons lp_config_set_string(core->config, "misc", "log_collection_upload_server_url", server_url); } +LinphoneLogCollectionState linphone_core_log_collection_enabled(void) { + return liblinphone_log_collection_state; +} + void linphone_core_enable_log_collection(LinphoneLogCollectionState state) { /* at first call of this function, set liblinphone_log_func to the current * ortp log function */ if( liblinphone_log_func == NULL ){ liblinphone_log_func = ortp_logv_out; } - if ((state != LinphoneLogCollectionDisabled) && (liblinphone_log_collection_enabled == FALSE)) { - liblinphone_log_collection_enabled = TRUE; + liblinphone_log_collection_state = state; + if (state != LinphoneLogCollectionDisabled) { ortp_mutex_init(&liblinphone_log_collection_mutex, NULL); if (state == LinphoneLogCollectionEnabledWithoutPreviousLogHandler) { liblinphone_log_func = NULL; @@ -266,13 +310,15 @@ void linphone_core_enable_log_collection(LinphoneLogCollectionState state) { } ortp_set_log_handler(linphone_core_log_collection_handler); } else { - liblinphone_log_collection_enabled = FALSE; ortp_set_log_handler(liblinphone_log_func); } } static void delete_log_collection_upload_file(void) { - char *filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", COMPRESSED_LOG_COLLECTION_FILENAME); + char *filename = ms_strdup_printf("%s/%s_log.%s", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX, + COMPRESSED_LOG_COLLECTION_EXTENSION); unlink(filename); ms_free(filename); } @@ -307,7 +353,10 @@ static int log_collection_upload_on_send_body(belle_sip_user_body_handler_t *bh, /* If we've not reach the end of file yet, fill the buffer with more data */ if (offset < core->log_collection_upload_information->size) { - char *log_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", COMPRESSED_LOG_COLLECTION_FILENAME); + char *log_filename = ms_strdup_printf("%s/%s_log.%s", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX, + COMPRESSED_LOG_COLLECTION_EXTENSION); #ifdef HAVE_ZLIB FILE *log_file = fopen(log_filename, "rb"); #else @@ -453,17 +502,22 @@ static int prepare_log_collection_file_to_upload(const char *filename) { int ret = 0; ortp_mutex_lock(&liblinphone_log_collection_mutex); - output_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", filename); + output_filename = ms_strdup_printf("%s/%s", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, filename); output_file = COMPRESS_OPEN(output_filename, "w"); if (output_file == NULL) goto error; - input_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone1.log"); + input_filename = ms_strdup_printf("%s/%s1.log", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX); input_file = fopen(input_filename, "r"); if (input_file == NULL) goto error; ret = compress_file(input_file, output_file); if (ret < 0) goto error; fclose(input_file); ms_free(input_filename); - input_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone2.log"); + input_filename = ms_strdup_printf("%s/%s2.log", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX); input_file = fopen(input_filename, "r"); if (input_file != NULL) { ret = compress_file(input_file, output_file); @@ -481,7 +535,8 @@ error: static size_t get_size_of_file_to_upload(const char *filename) { struct stat statbuf; - char *output_filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", filename); + char *output_filename = ms_strdup_printf("%s/%s", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, filename); FILE *output_file = fopen(output_filename, "rb"); fstat(fileno(output_file), &statbuf); fclose(output_file); @@ -490,7 +545,7 @@ static size_t get_size_of_file_to_upload(const char *filename) { } void linphone_core_upload_log_collection(LinphoneCore *core) { - if ((core->log_collection_upload_information == NULL) && (linphone_core_get_log_collection_upload_server_url(core) != NULL) && (liblinphone_log_collection_enabled == TRUE)) { + if ((core->log_collection_upload_information == NULL) && (linphone_core_get_log_collection_upload_server_url(core) != NULL) && (liblinphone_log_collection_state != LinphoneLogCollectionDisabled)) { /* open a transaction with the server and send an empty request(RCS5.1 section 3.5.4.8.3.1) */ belle_http_request_listener_callbacks_t cbs = { 0 }; belle_http_request_listener_t *l; @@ -506,7 +561,9 @@ void linphone_core_upload_log_collection(LinphoneCore *core) { core->log_collection_upload_information->type = "text"; core->log_collection_upload_information->subtype = "plain"; #endif - core->log_collection_upload_information->name = COMPRESSED_LOG_COLLECTION_FILENAME; + core->log_collection_upload_information->name = ms_strdup_printf("%s_log.%s", + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX, + COMPRESSED_LOG_COLLECTION_EXTENSION); if (prepare_log_collection_file_to_upload(core->log_collection_upload_information->name) < 0) return; core->log_collection_upload_information->size = get_size_of_file_to_upload(core->log_collection_upload_information->name); uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core)); @@ -520,19 +577,34 @@ void linphone_core_upload_log_collection(LinphoneCore *core) { } char * linphone_core_compress_log_collection(LinphoneCore *core) { - if (liblinphone_log_collection_enabled == FALSE) return NULL; - if (prepare_log_collection_file_to_upload(COMPRESSED_LOG_COLLECTION_FILENAME) < 0) return NULL; - return ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", COMPRESSED_LOG_COLLECTION_FILENAME); + char *filename = NULL; + if (liblinphone_log_collection_state == LinphoneLogCollectionDisabled) return NULL; + filename = ms_strdup_printf("%s_log.%s", + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX, + COMPRESSED_LOG_COLLECTION_EXTENSION); + if (prepare_log_collection_file_to_upload(filename) < 0) { + ms_free(filename); + return NULL; + } + ms_free(filename); + return ms_strdup_printf("%s/%s_log.%s", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX, + COMPRESSED_LOG_COLLECTION_EXTENSION); } void linphone_core_reset_log_collection(LinphoneCore *core) { char *filename; ortp_mutex_lock(&liblinphone_log_collection_mutex); delete_log_collection_upload_file(); - filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone1.log"); + filename = ms_strdup_printf("%s/%s1.log", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX); unlink(filename); ms_free(filename); - filename = ms_strdup_printf("%s/%s", liblinphone_log_collection_path ? liblinphone_log_collection_path : ".", "linphone2.log"); + filename = ms_strdup_printf("%s/%s2.log", + liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, + liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX); unlink(filename); ms_free(filename); ortp_mutex_unlock(&liblinphone_log_collection_mutex); diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index a909191ee..edb390570 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -1786,6 +1786,13 @@ typedef enum _LinphoneLogCollectionState { LinphoneLogCollectionEnabledWithoutPreviousLogHandler } LinphoneLogCollectionState; +/** + * Tells whether the linphone core log collection is enabled. + * @ingroup misc + * @returns The state of the linphone core log collection. + */ +LINPHONE_PUBLIC LinphoneLogCollectionState linphone_core_log_collection_enabled(void); + /** * Enable the linphone core log collection to upload logs on a server. * @ingroup misc @@ -1793,6 +1800,13 @@ typedef enum _LinphoneLogCollectionState { */ LINPHONE_PUBLIC void linphone_core_enable_log_collection(LinphoneLogCollectionState state); +/** + * Get the path where the log files will be written for log collection. + * @ingroup misc + * @returns The path where the log files will be written. + */ +LINPHONE_PUBLIC const char * linphone_core_get_log_collection_path(void); + /** * Set the path where the log files will be written for log collection. * @ingroup misc @@ -1800,6 +1814,34 @@ LINPHONE_PUBLIC void linphone_core_enable_log_collection(LinphoneLogCollectionSt */ LINPHONE_PUBLIC void linphone_core_set_log_collection_path(const char *path); +/** + * Get the prefix of the filenames that will be used for log collection. + * @ingroup misc + * @returns The prefix of the filenames used for log collection. + */ +LINPHONE_PUBLIC const char * linphone_core_get_log_collection_prefix(void); + +/** + * Set the prefix of the filenames that will be used for log collection. + * @ingroup misc + * @param[in] prefix The prefix to use for the filenames for log collection. + */ +LINPHONE_PUBLIC void linphone_core_set_log_collection_prefix(const char *prefix); + +/** + * Get the max file size in bytes of the files used for log collection. + * @ingroup misc + * @returns The max file size in bytes of the files used for log collection. + */ +LINPHONE_PUBLIC int linphone_core_get_log_collection_max_file_size(void); + +/** + * Set the max file size in bytes of the files used for log collection. + * @ingroup misc + * @param[in] size The max file size in bytes of the files used for log collection. + */ +LINPHONE_PUBLIC void linphone_core_set_log_collection_max_file_size(int size); + /** * Set the url of the server where to upload the collected log files. * @ingroup misc