Go to the documentation of this file.
4 #include <QMutexLocker>
5 #include <QWaitCondition>
9 #include <QCoreApplication>
11 #include <QStringList>
13 #include <QSocketNotifier>
28 #if CONFIG_SYSTEMD_JOURNAL
29 #define SD_JOURNAL_SUPPRESS_LOCATION 1 // NOLINT(cppcoreguidelines-macro-usage)
30 #include <systemd/sd-journal.h>
36 #include <sys/types.h>
46 #if defined(__linux__)
47 #include <sys/syscall.h>
48 #elif defined(__FreeBSD__)
50 #include <sys/ucontext.h>
53 #elif defined(Q_OS_DARWIN)
54 #include <mach/mach.h>
111 LOG(VB_GENERAL, LOG_INFO, QString(
"Added logging to %1")
121 LOG(VB_GENERAL, LOG_INFO, QString(
"Removed logging to %1")
131 QByteArray ba =
filename.toLocal8Bit();
132 const char *
file = ba.constData();
154 m_fd = open(qPrintable(
m_handle), O_WRONLY|O_CREAT|O_APPEND, 0664);
156 LOG(VB_GENERAL, LOG_INFO, QString(
"Rolled logging on %1") .arg(
m_handle));
172 line = qPrintable(QString(
"%1 %2 [%3/%4] %5 %6:%7 (%8) - %9\n")
173 .arg(timestamp, shortname,
174 QString::number(item->
pid()),
175 QString::number(item->
tid()),
177 QString::number(item->
line()),
183 line = qPrintable(QString(
"%1 %2 [%3] %5 %6:%7 (%8) - %9\n")
184 .arg(timestamp, shortname,
185 QString::number(item->
pid()),
187 QString::number(item->
line()),
192 int result =
write(
m_fd, line.data(), line.size());
196 LOG(VB_GENERAL, LOG_ERR,
197 QString(
"Closed Log output on fd %1 due to errors").arg(
m_fd));
206 SyslogLogger::SyslogLogger(
bool open) :
213 openlog(
nullptr, LOG_NDELAY, 0 );
217 LOG(VB_GENERAL, LOG_INFO,
"Added syslogging");
221 SyslogLogger::~SyslogLogger()
223 LOG(VB_GENERAL, LOG_INFO,
"Removing syslogging");
228 SyslogLogger *SyslogLogger::create(QMutex *mutex,
bool open)
230 auto *
logger = qobject_cast<SyslogLogger *>(
loggerMap.value(
"",
nullptr));
237 logger =
new SyslogLogger(open);
248 if (!m_opened || item->
facility() <= 0)
252 syslog(item->
level() | item->
facility(),
"%s[%d]: %c %s %s:%d (%s) %s",
253 qPrintable(item->
appName()), item->
pid(), shortname,
260 #if CONFIG_SYSTEMD_JOURNAL
261 JournalLogger::JournalLogger() :
265 LOG(VB_GENERAL, LOG_INFO,
"Added journal logging");
269 JournalLogger::~JournalLogger()
271 LOG(VB_GENERAL, LOG_INFO,
"Removing journal logging");
274 JournalLogger *JournalLogger::create(QMutex *mutex)
276 auto *
logger = qobject_cast<JournalLogger *>(
loggerMap.value(
"",
nullptr));
283 logger =
new JournalLogger();
295 "MESSAGE=%s", qUtf8Printable(item->
message()),
296 "PRIORITY=%d", item->
level(),
297 "CODE_FILE=%s", qUtf8Printable(item->
file()),
298 "CODE_LINE=%d", item->
line(),
299 "CODE_FUNC=%s", qUtf8Printable(item->
function()),
300 "SYSLOG_IDENTIFIER=%s", qUtf8Printable(item->
appName()),
301 "SYSLOG_PID=%d", item->
pid(),
302 "MYTH_THREAD=%s", qUtf8Printable(item->
threadName()),
350 Qt::QueuedConnection);
352 qRegisterMetaType<QList<QByteArray> >(
"QList<QByteArray>");
356 qApp->processEvents(QEventLoop::AllEvents, 10);
357 qApp->sendPostedEvents(
nullptr, QEvent::DeferredDelete);
378 if ((processed & 127) == 0)
380 qApp->processEvents(QEventLoop::AllEvents, 10);
381 qApp->sendPostedEvents(
nullptr, QEvent::DeferredDelete);
396 while (!loggers.isEmpty())
410 LOG(VB_GENERAL, LOG_INFO,
"SIGHUP received, rolling log files.");
414 QMap<QString, LoggerBase *>::iterator it;
417 it.value()->reopen();
429 logItem->
m_itemEpoch = nowAsDuration<std::chrono::seconds>();
445 loggers->insert(0,
logger);
456 loggers->insert(0,
logger);
459 #if CONFIG_SYSTEMD_JOURNAL
461 if (facility == SYSTEMD_JOURNAL_FACILITY)
466 loggers->insert(0,
logger);
473 logItem->
m_itemEpoch = nowAsDuration<std::chrono::seconds>();
484 for (
auto *it : qAsConst(*logItem->
m_itemList))
static LoggerListItem * gLoggerList
bool m_opened
true when the logfile is opened
void logForwardStop(void)
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
static QMap< QString, LoggerBase * > loggerMap
static QMutex gLoggerListMutex
Base class for the various logging mechanisms.
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
bool logmsg(LoggingItem *item) override
Process a log message, writing to the logfile.
LogForwardThread * logForwardThread
def write(text, progress=True)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
bool logForwardStart(void)
~LoggerBase() override
LoggerBase Deconstructor.
bool m_aborted
Flag to abort the thread.
void logForwardMessage(LoggingItem *item)
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.
The logging thread that forwards received messages to the consuming loggers via ZeroMQ.
static QWaitCondition gLogItemListNotEmpty
~LogForwardThread() override
LogForwardThread destructor.
static void handleSigHup(void)
SIGHUP handler - reopen all open logfiles for logrollers.
void run(void) override
Run the log forwarding thread.
QString m_handle
semi-opaque handle for identifying instance
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
LoggerBase(const char *string)
LoggerBase Constructor.
int m_fd
contains the file descriptor for the logfile
LogForwardThread()
LogForwardThread constructor.
static QMutex gLogItemListMutex
static void forwardMessage(LoggingItem *item)
File-based logger - used for logfiles and console.
static LoggingItemList gLogItemList
void stop(void)
Stop the thread by setting the abort flag.
~FileLogger() override
FileLogger deconstructor - close the logfile.
QString getTimestampUs(const char *format="yyyy-MM-dd HH:mm:ss") const
static void logger(cdio_log_level_t level, const char *message)
This is a wrapper around QThread that does several additional things.
bool isRunning(void) const
QList< LoggingItem * > LoggingItemList
FileLogger(const char *filename)
FileLogger constructor.
void incomingSigHup(void)
void reopen(void) override
Reopen the logfile after a SIGHUP.
static FileLogger * create(const QString &filename, QMutex *mutex)
static QMutex loggerMapMutex
virtual int IncrRef(void)
Increments reference count.
std::chrono::seconds m_itemEpoch
QList< LoggerBase * > LoggerList