77 changes: 24 additions & 53 deletions mythtv/libs/libmythbase/loggingserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ static QMutex logMsgListMutex;
static LogMessageList logMsgList;
static QWaitCondition logMsgListNotEmpty;

#define TIMESTAMP_MAX 30
#define MAX_STRING_LENGTH (LOGLINE_MAX+120)

/// \brief LoggerBase class constructor. Adds the new logger instance to the
/// loggerMap.
/// \param string a C-string of the handle for this instance (NULL if unused)
Expand Down Expand Up @@ -179,48 +176,36 @@ void FileLogger::reopen(void)
/// \param item LoggingItem containing the log message to process
bool FileLogger::logmsg(LoggingItem *item)
{
char line[MAX_STRING_LENGTH];
char usPart[9];
char timestamp[TIMESTAMP_MAX];

if (!m_opened)
return false;

time_t epoch = item->epoch();
struct tm tm {};
localtime_r(&epoch, &tm);

strftime(timestamp, TIMESTAMP_MAX-8, "%Y-%m-%d %H:%M:%S",
(const struct tm *)&tm);
snprintf( usPart, 9, ".%06d", (int)(item->usec()) );
strcat( timestamp, usPart );

char shortname = '-';
{
QMutexLocker locker(&loglevelMapMutex);
LoglevelMap::iterator it = loglevelMap.find(item->level());
if (it != loglevelMap.end())
shortname = (*it)->shortname;
}
QString timestamp = item->getTimestampUs();
QChar shortname = item->getLevelChar();

std::string line;
if( item->tid() )
{
snprintf( line, MAX_STRING_LENGTH,
"%s %c [%d/%" PREFIX64 "d] %s %s:%d (%s) - %s\n",
timestamp, shortname, item->pid(), item->tid(),
item->rawThreadName(), item->rawFile(), item->line(),
item->rawFunction(), item->rawMessage() );
line = qPrintable(QString("%1 %2 [%3/%4] %5 %6:%7 (%8) - %9\n")
.arg(timestamp, shortname,
QString::number(item->pid()),
QString::number(item->tid()),
item->threadName(), item->file(),
QString::number(item->line()),
item->function(),
item->message()));
}
else
{
snprintf( line, MAX_STRING_LENGTH,
"%s %c [%d] %s %s:%d (%s) - %s\n",
timestamp, shortname, item->pid(), item->rawThreadName(),
item->rawFile(), item->line(), item->rawFunction(),
item->rawMessage() );
line = qPrintable(QString("%1 %2 [%3] %5 %6:%7 (%8) - %9\n")
.arg(timestamp, shortname,
QString::number(item->pid()),
item->threadName(), item->file(),
QString::number(item->line()),
item->function(),
item->message()));
}

int result = write(m_fd, line, strlen(line));
int result = write(m_fd, line.data(), line.size());

if( result == -1 )
{
Expand Down Expand Up @@ -283,18 +268,11 @@ bool SyslogLogger::logmsg(LoggingItem *item)
if (!m_opened || item->facility() <= 0)
return false;

char shortname = '-';

{
QMutexLocker locker(&loglevelMapMutex);
LoglevelDef *lev = loglevelMap.value(item->level(), nullptr);
if (lev != nullptr)
shortname = lev->shortname;
}
char shortname = item->getLevelChar();
syslog(item->level() | item->facility(), "%s[%d]: %c %s %s:%d (%s) %s",
item->rawAppName(), item->pid(), shortname, item->rawThreadName(),
item->rawFile(), item->line(), item->rawFunction(),
item->rawMessage());
qPrintable(item->message()));

return true;
}
Expand Down Expand Up @@ -338,7 +316,7 @@ JournalLogger *JournalLogger::create(QMutex *mutex)
bool JournalLogger::logmsg(LoggingItem *item)
{
sd_journal_send(
"MESSAGE=%s", item->rawMessage(),
"MESSAGE=%s", qUtf8Printable(item->message()),
"PRIORITY=%d", item->level(),
"CODE_FILE=%s", item->rawFile(),
"CODE_LINE=%d", item->line(),
Expand Down Expand Up @@ -458,14 +436,7 @@ bool DatabaseLogger::logmsg(LoggingItem *item)
/// \param item LoggingItem containing the log message to insert
bool DatabaseLogger::logqmsg(MSqlQuery &query, LoggingItem *item)
{
char timestamp[TIMESTAMP_MAX];

time_t epoch = item->epoch();
struct tm tm {};
localtime_r(&epoch, &tm);

strftime(timestamp, TIMESTAMP_MAX-8, "%Y-%m-%d %H:%M:%S",
(const struct tm *)&tm);
QString timestamp = item->getTimestamp();

query.bindValue(":TID", item->tid());
query.bindValue(":THREAD", item->threadName());
Expand Down Expand Up @@ -615,7 +586,7 @@ void DBLoggerThread::run(void)
if (!item)
continue;

if (item->message()[0] != QChar('\0'))
if (!item->message().isEmpty())
{
qLock.unlock();
bool logged = m_logger->logqmsg(*query, item);
Expand Down
24 changes: 17 additions & 7 deletions mythtv/libs/libmythbase/mythlogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,42 @@ extern "C" {
// Neither of them will lock the calling thread other than momentarily to put
// the log message onto a queue.
#ifdef __cplusplus
#define LOG(_MASK_, _LEVEL_, _STRING_) \
#define LOG(_MASK_, _LEVEL_, _QSTRING_) \
do { \
if (VERBOSE_LEVEL_CHECK((_MASK_), (_LEVEL_)) && ((_LEVEL_)>=0)) \
{ \
LogPrintLine(_MASK_, _LEVEL_, \
__FILE__, __LINE__, __FUNCTION__, 1, \
qPrintable(_STRING_)); \
__FILE__, __LINE__, __FUNCTION__, \
_QSTRING_); \
} \
} while (false)
#else
#define LOG(_MASK_, _LEVEL_, _FORMAT_, ...) \
do { \
if (VERBOSE_LEVEL_CHECK((_MASK_), (_LEVEL_)) && ((_LEVEL_)>=0)) \
{ \
LogPrintLine(_MASK_, _LEVEL_, \
__FILE__, __LINE__, __FUNCTION__, 0, \
LogPrintLineC(_MASK_, _LEVEL_, \
__FILE__, __LINE__, __FUNCTION__, \
(const char *)_FORMAT_, ##__VA_ARGS__); \
} \
} while (0)
#endif

/* Define the external prototype */
#ifdef __cplusplus
MBASE_PUBLIC void LogPrintLine( uint64_t mask, LogLevel_t level,
const char *file, int line,
const char *function, int fromQString,
const char *format, ... );
const char *function,
QString message);
extern "C" {
#endif
MBASE_PUBLIC void LogPrintLineC( uint64_t mask, LogLevel_t level,
const char *file, int line,
const char *function,
const char *format, ...);
#ifdef __cplusplus
}
#endif

extern MBASE_PUBLIC LogLevel_t logLevel;
extern MBASE_PUBLIC uint64_t verboseMask;
Expand Down
5 changes: 2 additions & 3 deletions mythtv/programs/mythtranscode/external/replex/replex.c
Original file line number Diff line number Diff line change
Expand Up @@ -2660,9 +2660,8 @@ int main(int argc, char **argv)
return 0;
}

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

Expand Down