summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Watson <watsonmw@gmail.com>2022-12-20 07:01:26 -0800
committerGitHub <noreply@github.com>2022-12-20 16:01:26 +0100
commit833aa08b0809da3ef68829e9a45c403dae8b63d5 (patch)
treea720c2ed6b6479670e366f2beb3d6e282af53dd2
parentdd8d66a05f2dd0e8a682f72611107785afa18090 (diff)
downloadlibgphoto2-833aa08b0809da3ef68829e9a45c403dae8b63d5.tar.gz
Only format log messages when a log function is registered for given log level. (#871)
This speeds up large file downloads considerably, as the data log string generation is skipped.
-rw-r--r--libgphoto2_port/libgphoto2_port/gphoto2-port-log.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c b/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c
index 7cc7614f8..3bca65742 100644
--- a/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c
+++ b/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c
@@ -80,6 +80,7 @@ typedef struct {
static LogFunc *log_funcs = NULL;
static unsigned int log_funcs_count = 0;
+static GPLogLevel log_max_level = 0;
/**
* \brief Add a function to get logging information
@@ -110,6 +111,9 @@ gp_log_add_func (GPLogLevel level, GPLogFunc func, void *data)
log_funcs[log_funcs_count - 1].func = func;
log_funcs[log_funcs_count - 1].data = data;
+ if (level > log_max_level)
+ log_max_level = level;
+
return logfuncid;
}
@@ -126,15 +130,21 @@ int
gp_log_remove_func (int id)
{
unsigned int i;
+ GPLogLevel new_max_log_level = 0;
+ int status = GP_ERROR_BAD_PARAMETERS;
+ /* Remove log function from list and recalculate current most detailed log level needed */
for (i=0;i<log_funcs_count;i++) {
if (log_funcs[i].id == id) {
memmove (log_funcs + i, log_funcs + i + 1, sizeof(LogFunc) * (log_funcs_count - i - 1));
log_funcs_count--;
- return GP_OK;
+ status = GP_OK;
}
+ if (new_max_log_level < log_funcs[i].level)
+ new_max_log_level = log_funcs[i].level;
}
- return GP_ERROR_BAD_PARAMETERS;
+ log_max_level = new_max_log_level;
+ return status;
}
/**
@@ -200,6 +210,10 @@ gp_log_data (const char *domain, const char *data, unsigned int size, const char
unsigned int index, original_size = size;
unsigned char value;
+ /* No logger currently at the data log level */
+ if (log_max_level < GP_LOG_DATA)
+ return;
+
va_start (args, format);
msg = gpi_vsnprintf(format, args);
va_end (args);
@@ -290,7 +304,7 @@ gp_logv (GPLogLevel level, const char *domain, const char *format,
unsigned int i;
char *str = 0;
- if (!log_funcs_count)
+ if (!log_funcs_count || level > log_max_level)
return;
str = gpi_vsnprintf(format, args);
@@ -320,6 +334,9 @@ gp_log (GPLogLevel level, const char *domain, const char *format, ...)
{
va_list args;
+ if (!log_funcs_count || level > log_max_level)
+ return;
+
va_start (args, format);
gp_logv (level, domain, format, args);
va_end (args);
@@ -329,11 +346,14 @@ void
gp_log_with_source_location(GPLogLevel level, const char *file, int line, const char *func, const char *format, ...)
{
va_list args;
- char domain[100];
+ char domain[100];
+
+ if (!log_funcs_count || level > log_max_level)
+ return;
- /* Only display filename without any path/directory part */
- file = strrchr(file, '/') ? strrchr(file, '/') + 1 : file;
- snprintf(domain, sizeof(domain), "%s [%s:%d]", func, file, line);
+ /* Only display filename without any path/directory part */
+ file = strrchr(file, '/') ? strrchr(file, '/') + 1 : file;
+ snprintf(domain, sizeof(domain), "%s [%s:%d]", func, file, line);
va_start (args, format);
gp_logv (level, domain, format, args);