Skip to content

Commit

Permalink
Split out the QRegExp in logging to get compiled once.
Browse files Browse the repository at this point in the history
Rather than recompiling the regular expression on each log message, we now
compile it once, and apply many times.  This should be a fair amount more
efficient, even though it necessitates a strchr to look for %, and then
conversion back to QString.
  • Loading branch information
Beirdo committed Jul 22, 2011
1 parent a56a759 commit bdb7e79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
13 changes: 11 additions & 2 deletions mythtv/libs/libmythbase/mythlogging.cpp
Expand Up @@ -55,6 +55,7 @@ QList<LoggerBase *> loggerList;

QMutex logQueueMutex;
QQueue<LoggingItem_t *> logQueue;
QRegExp logRegExp = QRegExp("[%]{1,2}");

QMutex logThreadMutex;
QHash<uint64_t, char *> logThreadHash;
Expand Down Expand Up @@ -759,7 +760,8 @@ void LogTimeStamp( struct tm *tm, uint32_t *usec )
}

void LogPrintLine( uint64_t mask, LogLevel_t level, const char *file, int line,
const char *function, const char *format, ... )
const char *function, int fromQString,
const char *format, ... )
{
va_list arguments;
char *message;
Expand All @@ -782,6 +784,14 @@ void LogPrintLine( uint64_t mask, LogLevel_t level, const char *file, int line,
if( !message )
return;

QMutexLocker qLock(&logQueueMutex);

if( fromQString && strchr(format, '%') )
{
QString string(format);
format = string.replace(logRegExp, "%%").toLocal8Bit().constData();
}

va_start(arguments, format);
vsnprintf(message, LOGLINE_MAX, format, arguments);
va_end(arguments);
Expand All @@ -795,7 +805,6 @@ void LogPrintLine( uint64_t mask, LogLevel_t level, const char *file, int line,
item->message = message;
setThreadTid(item);

QMutexLocker qLock(&logQueueMutex);
logQueue.enqueue(item);
}

Expand Down
8 changes: 4 additions & 4 deletions mythtv/libs/libmythbase/mythlogging.h
Expand Up @@ -100,18 +100,18 @@ extern "C" {
#ifdef __cplusplus
#define LOG(mask, level, string) \
LogPrintLine(mask, (LogLevel_t)level, __FILE__, __LINE__, __FUNCTION__, \
QString(string).replace(QRegExp("[%]{1,2}"), "%%") \
.toLocal8Bit().constData())
1, QString(string).toLocal8Bit().constData())
#else
#define LOG(mask, level, format, ...) \
LogPrintLine(mask, (LogLevel_t)level, __FILE__, __LINE__, __FUNCTION__, \
(const char *)format, ##__VA_ARGS__)
0, (const char *)format, ##__VA_ARGS__)
#endif

/* Define the external prototype */
MBASE_PUBLIC void LogPrintLine( uint64_t mask, LogLevel_t level,
const char *file, int line,
const char *function, const char *format, ... );
const char *function, int fromQString,
const char *format, ... );

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion mythtv/programs/mythtranscode/replex/replex.c
Expand Up @@ -2582,7 +2582,8 @@ int main(int argc, char **argv)
}

void LogPrintLine( uint64_t mask, LogLevel_t level, const char *file, int line,
const char *function, const char *format, ... )
const char *function, int fromQString,
const char *format, ... )
{
va_list arguments;

Expand Down

0 comments on commit bdb7e79

Please sign in to comment.