Skip to content

Commit

Permalink
Optimized checking if event logging is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Lemstra committed Oct 31, 2017
1 parent c2744aa commit 8dd7e1f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
54 changes: 34 additions & 20 deletions MagickCore/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ static LinkedListInfo
static SemaphoreInfo
*event_semaphore = (SemaphoreInfo *) NULL,
*log_semaphore = (SemaphoreInfo *) NULL;

static MagickBooleanType
log_enabled = MagickFalse;

/*
Forward declarations.
Expand All @@ -227,6 +230,22 @@ static MagickBooleanType
LoadLogCache(LinkedListInfo *,const char *,const char *,const size_t,
ExceptionInfo *);

static void CheckLogEnabled()
{
/* We don't need locks because we only call this inside log_semaphore */
if (IsLinkedListEmpty(log_cache) != MagickFalse)
log_enabled=MagickFalse;
else
{
LogInfo
*p;

ResetLinkedListIterator(log_cache);
p=(LogInfo *) GetNextValueInLinkedList(log_cache);
log_enabled=p->event_mask != NoEvents ? MagickTrue: MagickFalse;
}
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
Expand Down Expand Up @@ -663,7 +682,10 @@ static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
ActivateSemaphoreInfo(&log_semaphore);
LockSemaphoreInfo(log_semaphore);
if (log_cache == (LinkedListInfo *) NULL)
log_cache=AcquireLogCache(LogFilename,exception);
{
log_cache=AcquireLogCache(LogFilename,exception);
CheckLogEnabled();
}
UnlockSemaphoreInfo(log_semaphore);
}
return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
Expand All @@ -690,19 +712,7 @@ static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
*/
MagickExport MagickBooleanType IsEventLogging(void)
{
const LogInfo
*log_info;

ExceptionInfo
*exception;

if ((log_cache == (LinkedListInfo *) NULL) ||
(IsLinkedListEmpty(log_cache) != MagickFalse))
return(MagickFalse);
exception=AcquireExceptionInfo();
log_info=GetLogInfo("*",exception);
exception=DestroyExceptionInfo(exception);
return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
return(log_enabled);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -891,6 +901,7 @@ MagickPrivate void LogComponentTerminus(void)
LockSemaphoreInfo(log_semaphore);
if (log_cache != (LinkedListInfo *) NULL)
log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
log_enabled=MagickFalse;
UnlockSemaphoreInfo(log_semaphore);
RelinquishSemaphoreInfo(&log_semaphore);
}
Expand Down Expand Up @@ -1251,8 +1262,9 @@ static char *TranslateFilename(const LogInfo *log_info)
return(filename);
}

MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
const char *function,const size_t line,const char *format,va_list operands)
MagickExport MagickBooleanType LogMagickEventList(const LogEventType type,
const char *module,const char *function,const size_t line,const char *format,
va_list operands)
{
char
event[MagickPathExtent],
Expand All @@ -1270,8 +1282,6 @@ MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
LogInfo
*log_info;

if (IsEventLogging() == MagickFalse)
return(MagickFalse);
exception=AcquireExceptionInfo();
log_info=(LogInfo *) GetLogInfo("*",exception);
exception=DestroyExceptionInfo(exception);
Expand Down Expand Up @@ -1380,15 +1390,18 @@ MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
return(MagickTrue);
}

MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
const char *function,const size_t line,const char *format,...)
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type,
const char *module,const char *function,const size_t line,
const char *format,...)
{
va_list
operands;

MagickBooleanType
status;

if (IsEventLogging() == MagickFalse)
return(MagickFalse);
va_start(operands,format);
status=LogMagickEventList(type,module,function,line,format,operands);
va_end(operands);
Expand Down Expand Up @@ -1737,6 +1750,7 @@ MagickExport LogEventType SetLogEventMask(const char *events)
log_info->event_mask=(LogEventType) option;
if (option == -1)
log_info->event_mask=UndefinedEvents;
CheckLogEnabled();
UnlockSemaphoreInfo(log_semaphore);
return(log_info->event_mask);
}
Expand Down
3 changes: 1 addition & 2 deletions MagickCore/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ extern MagickExport MagickBooleanType
IsEventLogging(void),
ListLogInfo(FILE *,ExceptionInfo *),
LogMagickEvent(const LogEventType,const char *,const char *,const size_t,
const char *,...)
magick_attribute((__format__ (__printf__,5,6))),
const char *,...) magick_attribute((__format__ (__printf__,5,6))),
LogMagickEventList(const LogEventType,const char *,const char *,const size_t,
const char *,va_list) magick_attribute((__format__ (__printf__,5,0)));

Expand Down
3 changes: 3 additions & 0 deletions MagickWand/wandcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ WandExport MagickBooleanType CLILogEvent(MagickCLI *cli_wand,
va_list
operands;

if (IsEventLogging() == MagickFalse)
return(MagickFalse);

/* HACK - prepend the CLI location to format string.
The better way would be add more arguments to to the 'va' oparands
list, but that does not appear to be possible! So we do some
Expand Down

0 comments on commit 8dd7e1f

Please sign in to comment.