5#if QT_VERSION >= QT_VERSION_CHECK(6,5,0)
6#include <QtSystemDetection>
11#include <QMutexLocker>
12#include <QWaitCondition>
16#include <QCoreApplication>
20#include <QSocketNotifier>
23#include "mythconfig.h"
35#if CONFIG_SYSTEMD_JOURNAL
36#define SD_JOURNAL_SUPPRESS_LOCATION 1
37#include <systemd/sd-journal.h>
105 LOG(VB_GENERAL, LOG_INFO, QString(
"Added logging to %1")
115 LOG(VB_GENERAL, LOG_INFO, QString(
"Removed logging to %1")
123 QByteArray ba =
filename.toLocal8Bit();
124 const char *
file = ba.constData();
147 LOG(VB_GENERAL, LOG_INFO, QString(
"Rolled logging on %1") .arg(
m_handle));
157 std::string line = item->
toString();
163 LOG(VB_GENERAL, LOG_ERR,
164 QString(
"Closed Log output to %1 due to unrecoverable error(s).").arg(
m_handle));
174SyslogLogger::SyslogLogger(
bool open) :
179 openlog(
nullptr, LOG_NDELAY, 0 );
183 LOG(VB_GENERAL, LOG_INFO,
"Added syslogging");
187SyslogLogger::~SyslogLogger()
189 LOG(VB_GENERAL, LOG_INFO,
"Removing syslogging");
194SyslogLogger *SyslogLogger::create(QMutex *mutex,
bool open)
196 auto *
logger =
dynamic_cast<SyslogLogger *
>(
loggerMap.value(
"",
nullptr));
203 logger =
new SyslogLogger(open);
214 if (!m_opened || item->
facility() <= 0)
218 syslog(item->
level() | item->
facility(),
"%s[%d]: %c %s %s:%d (%s) %s",
219 qPrintable(item->
appName()), item->
pid(), shortname,
226#if CONFIG_SYSTEMD_JOURNAL
228JournalLogger::JournalLogger() :
231 LOG(VB_GENERAL, LOG_INFO,
"Added journal logging");
235JournalLogger::~JournalLogger()
237 LOG(VB_GENERAL, LOG_INFO,
"Removing journal logging");
240JournalLogger *JournalLogger::create(QMutex *mutex)
242 auto *
logger =
dynamic_cast<JournalLogger *
>(
loggerMap.value(
"",
nullptr));
249 logger =
new JournalLogger();
261 "MESSAGE=%s", qUtf8Printable(item->
message()),
262 "PRIORITY=%d", item->
level(),
263 "CODE_FILE=%s", qUtf8Printable(item->
file()),
264 "CODE_LINE=%d", item->
line(),
265 "CODE_FUNC=%s", qUtf8Printable(item->
function()),
266 "SYSLOG_IDENTIFIER=%s", qUtf8Printable(item->
appName()),
267 "SYSLOG_PID=%d", item->
pid(),
268 "MYTH_THREAD=%s", qUtf8Printable(item->
threadName()),
316 Qt::QueuedConnection);
318 qRegisterMetaType<QList<QByteArray> >(
"QList<QByteArray>");
322 qApp->processEvents(QEventLoop::AllEvents, 10);
323 qApp->sendPostedEvents(
nullptr, QEvent::DeferredDelete);
344 if ((processed & 127) == 0)
346 qApp->processEvents(QEventLoop::AllEvents, 10);
347 qApp->sendPostedEvents(
nullptr, QEvent::DeferredDelete);
362 while (!loggers.isEmpty())
376 LOG(VB_GENERAL, LOG_INFO,
"SIGHUP received, rolling log files.");
380 QMap<QString, LoggerBase *>::iterator it;
383 it.value()->reopen();
395 logItem->
m_itemEpoch = nowAsDuration<std::chrono::seconds>();
405 QString logfile = item->
logFile();
406 if (!logfile.isEmpty())
411 loggers->insert(0,
logger);
422 loggers->insert(0,
logger);
425#if CONFIG_SYSTEMD_JOURNAL
427 if (facility == SYSTEMD_JOURNAL_FACILITY)
432 loggers->insert(0,
logger);
439 logItem->
m_itemEpoch = nowAsDuration<std::chrono::seconds>();
450 for (
auto *it : std::as_const(*logItem->
m_itemList))
466 std::this_thread::sleep_for(10ms);
494#include "moc_loggingserver.cpp"
static void logger(cdio_log_level_t level, const char *message)
File-based logger - used for logfiles and console.
static FileLogger * create(const QString &filename, QMutex *mutex)
std::ofstream m_ofstream
Output file stream for the log file.
bool logmsg(LoggingItem *item) override
Process a log message, writing to the logfile.
~FileLogger() override
FileLogger deconstructor - close the logfile.
void reopen(void) override
Reopen the logfile after a SIGHUP.
FileLogger(const char *filename)
FileLogger constructor.
The logging thread that forwards received messages to the consuming loggers via ZeroMQ.
void run(void) override
Run the log forwarding thread.
static void handleSigHup(void)
SIGHUP handler - reopen all open logfiles for logrollers.
static void forwardMessage(LoggingItem *item)
LogForwardThread()
LogForwardThread constructor.
void stop(void)
Stop the thread by setting the abort flag.
~LogForwardThread() override
LogForwardThread destructor.
bool m_aborted
Flag to abort the thread.
void incomingSigHup(void)
Base class for the various logging mechanisms.
virtual ~LoggerBase()
LoggerBase Deconstructor.
QString m_handle
semi-opaque handle for identifying instance
LoggerBase(const char *string)
LoggerBase Constructor.
The logging items that are generated by LOG() and are sent to the console.
char getLevelChar(void)
Get the message log level as a single character.
std::string toString()
Long format to string.
This is a wrapper around QThread that does several additional things.
bool isRunning(void) const
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
virtual int IncrRef(void)
Increments reference count.
void logForwardMessage(LoggingItem *item)
QList< LoggerBase * > LoggerList
static LoggerListItem * gLoggerList
bool logForwardStart(void)
static QMap< QString, LoggerBase * > loggerMap
static QMutex loggerMapMutex
static QMutex gLoggerListMutex
static LoggingItemList gLogItemList
static QWaitCondition gLogItemListNotEmpty
static QMutex gLogItemListMutex
void logForwardStop(void)
LogForwardThread * logForwardThread
QList< LoggingItem * > LoggingItemList
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
std::chrono::seconds m_itemEpoch