MythTV  master
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
LoggerThread Class Reference

The logging thread that consumes the logging queue and dispatches each LoggingItem. More...

#include <logging.h>

Inheritance diagram for LoggerThread:
Inheritance graph
[legend]
Collaboration diagram for LoggerThread:
Collaboration graph
[legend]

Public Member Functions

 LoggerThread (QString filename, bool progress, bool quiet, QString table, int facility)
 LoggerThread constructor. More...
 
 ~LoggerThread ()
 LoggerThread destructor. Triggers the deletion of all loggers. More...
 
void run (void) override
 Run the logging thread. More...
 
void stop (void)
 Stop the thread by setting the abort flag after waiting a second for the queue to be flushed. More...
 
bool flush (int timeoutMS=200000)
 Wait for the queue to be flushed (up to a timeout) More...
 
void fillItem (LoggingItem *item)
 
- Public Member Functions inherited from MThread
 MThread (const QString &objectName)
 Standard constructor. More...
 
 MThread (const QString &objectName, QRunnable *runnable)
 Use this constructor if you want the default run() method to run the QRunnable's run() method instead of entering the Qt event loop. More...
 
virtual ~MThread ()
 
void RunProlog (void)
 Sets up a thread, call this if you reimplement run(). More...
 
void RunEpilog (void)
 Cleans up a thread's resources, call this if you reimplement run(). More...
 
QThread * qthread (void)
 Returns the thread, this will always return the same pointer no matter how often you restart the thread. More...
 
void setObjectName (const QString &name)
 
QString objectName (void) const
 
void setPriority (QThread::Priority priority)
 
QThread::Priority priority (void) const
 
bool isFinished (void) const
 
bool isRunning (void) const
 
void setStackSize (uint stackSize)
 
uint stackSize (void) const
 
void exit (int retcode=0)
 Use this to exit from the thread if you are using a Qt event loop. More...
 
void start (QThread::Priority=QThread::InheritPriority)
 Tell MThread to start running the thread in the near future. More...
 
void terminate (void)
 Kill a thread unsafely. More...
 
void quit (void)
 calls exit(0) More...
 
bool wait (unsigned long time=ULONG_MAX)
 Wait for the MThread to exit, with a maximum timeout. More...
 

Static Public Member Functions

static void handleItem (LoggingItem *item)
 Handles each LoggingItem. More...
 
- Static Public Member Functions inherited from MThread
static void ThreadSetup (const QString &)
 This is to be called on startup in those few threads that haven't been ported to MThread. More...
 
static void ThreadCleanup (void)
 This is to be called on exit in those few threads that haven't been ported to MThread. More...
 
static void Cleanup (void)
 This will print out all the running threads, call exit(1) on each and then wait up to 5 seconds total for all the threads to exit. More...
 
static void GetAllThreadNames (QStringList &list)
 
static void GetAllRunningThreadNames (QStringList &list)
 

Protected Member Functions

bool logConsole (LoggingItem *item)
 Process a log message, writing to the console. More...
 
- Protected Member Functions inherited from MThread
int exec (void)
 Enters the qt event loop. call exit or quit to exit thread. More...
 

Private Member Functions

 Q_DISABLE_COPY (LoggerThread)
 

Private Attributes

QWaitCondition * m_waitNotEmpty {nullptr}
 Condition variable for waiting for the queue to not be empty Protected by logQueueMutex. More...
 
QWaitCondition * m_waitEmpty {nullptr}
 Condition variable for waiting for the queue to be empty Protected by logQueueMutex. More...
 
bool m_aborted {false}
 Flag to abort the thread. More...
 
QString m_filename
 Filename of debug logfile. More...
 
bool m_progress
 show only LOG_ERR and more important (console only) More...
 
int m_quiet
 silence the console (console only) More...
 
QString m_appname {QCoreApplication::applicationName()}
 Cached application name. More...
 
QString m_tablename
 Cached table name for db logging. More...
 
int m_facility
 Cached syslog facility (or -1 to disable) More...
 
pid_t m_pid
 Cached pid value. More...
 

Friends

void LogPrintLine (uint64_t, LogLevel_t, const char *, int, const char *, int, const char *,...)
 Format and send a log message into the queue. More...
 

Additional Inherited Members

- Static Public Attributes inherited from MThread
static const int kDefaultStartTimeout
 
- Static Protected Member Functions inherited from MThread
static void setTerminationEnabled (bool enabled=true)
 
static void sleep (unsigned long time)
 
static void msleep (unsigned long time)
 
static void usleep (unsigned long time)
 
- Protected Attributes inherited from MThread
MThreadInternalm_thread {nullptr}
 
QRunnable * m_runnable {nullptr}
 
bool m_prologExecuted {true}
 
bool m_epilogExecuted {true}
 

Detailed Description

The logging thread that consumes the logging queue and dispatches each LoggingItem.

Definition at line 171 of file logging.h.

Constructor & Destructor Documentation

◆ LoggerThread()

LoggerThread::LoggerThread ( QString  filename,
bool  progress,
bool  quiet,
QString  table,
int  facility 
)

LoggerThread constructor.

Enables debugging of thread registration and deregistration if the VERBOSE_THREADS environment variable is set.

Definition at line 235 of file logging.cpp.

◆ ~LoggerThread()

LoggerThread::~LoggerThread ( )

LoggerThread destructor. Triggers the deletion of all loggers.

Definition at line 260 of file logging.cpp.

Member Function Documentation

◆ run()

void LoggerThread::run ( void  )
overridevirtual

Run the logging thread.

This thread reads from the logging queue, and handles distributing the LoggingItems to each logger instance. The thread will not exit until the logging queue is emptied completely, ensuring that all logging is flushed.

Reimplemented from MThread.

Definition at line 274 of file logging.cpp.

◆ stop()

void LoggerThread::stop ( void  )

Stop the thread by setting the abort flag after waiting a second for the queue to be flushed.

Definition at line 504 of file logging.cpp.

Referenced by logStop(), and ~LoggerThread().

◆ flush()

bool LoggerThread::flush ( int  timeoutMS = 200000)

Wait for the queue to be flushed (up to a timeout)

Parameters
timeoutMSThe number of ms to wait for the queue to flush
Returns
true if the queue is empty, false otherwise

Definition at line 516 of file logging.cpp.

Referenced by LogPrintLine(), and stop().

◆ handleItem()

void LoggerThread::handleItem ( LoggingItem item)
static

Handles each LoggingItem.

There is a special case for thread registration and deregistration which are also included in the logging queue to keep the thread names in sync with the log messages.

Parameters
itemThe LoggingItem to be handled

Definition at line 331 of file logging.cpp.

Referenced by LogPrintLine(), and run().

◆ fillItem()

void LoggerThread::fillItem ( LoggingItem item)

Definition at line 530 of file logging.cpp.

Referenced by run().

◆ Q_DISABLE_COPY()

LoggerThread::Q_DISABLE_COPY ( LoggerThread  )
private

◆ logConsole()

bool LoggerThread::logConsole ( LoggingItem item)
protected

Process a log message, writing to the console.

Parameters
itemLoggingItem containing the log message to process

Definition at line 403 of file logging.cpp.

Referenced by LogPrintLine(), and run().

Friends And Related Function Documentation

◆ LogPrintLine

void LogPrintLine ( uint64_t  mask,
LogLevel_t  level,
const char *  file,
int  line,
const char *  function,
int  fromQString,
const char *  format,
  ... 
)
friend

Format and send a log message into the queue.

This is called from the LOG() macro. The intention is minimal blocking of the caller.

Parameters
maskVerbosity mask of the message (VB_*)
levelLog level of this message (LOG_* - matching syslog levels)
fileFilename of source code logging the message
lineLine number within the source of log message source
functionFunction name of the log message source
fromQStringtrue if this message originated from QString
formatprintf format string (when not from QString), log message (when from QString)
...printf arguments (when not from QString)

Definition at line 584 of file logging.cpp.

Member Data Documentation

◆ m_waitNotEmpty

QWaitCondition* LoggerThread::m_waitNotEmpty {nullptr}
private

Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.

Definition at line 188 of file logging.h.

Referenced by flush(), run(), stop(), and ~LoggerThread().

◆ m_waitEmpty

QWaitCondition* LoggerThread::m_waitEmpty {nullptr}
private

Condition variable for waiting for the queue to be empty Protected by logQueueMutex.

Definition at line 192 of file logging.h.

Referenced by flush(), run(), and ~LoggerThread().

◆ m_aborted

bool LoggerThread::m_aborted {false}
private

Flag to abort the thread.

Protected by logQueueMutex

Definition at line 196 of file logging.h.

Referenced by flush(), run(), and stop().

◆ m_filename

QString LoggerThread::m_filename
private

Filename of debug logfile.

Definition at line 198 of file logging.h.

Referenced by fillItem().

◆ m_progress

bool LoggerThread::m_progress
private

show only LOG_ERR and more important (console only)

Definition at line 199 of file logging.h.

Referenced by logConsole().

◆ m_quiet

int LoggerThread::m_quiet
private

silence the console (console only)

Definition at line 200 of file logging.h.

Referenced by logConsole().

◆ m_appname

QString LoggerThread::m_appname {QCoreApplication::applicationName()}
private

Cached application name.

Definition at line 201 of file logging.h.

Referenced by fillItem().

◆ m_tablename

QString LoggerThread::m_tablename
private

Cached table name for db logging.

Definition at line 203 of file logging.h.

Referenced by fillItem().

◆ m_facility

int LoggerThread::m_facility
private

Cached syslog facility (or -1 to disable)

Definition at line 204 of file logging.h.

Referenced by fillItem().

◆ m_pid

pid_t LoggerThread::m_pid
private

Cached pid value.

Definition at line 205 of file logging.h.

Referenced by fillItem().


The documentation for this class was generated from the following files: