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, int facility)
 LoggerThread constructor. More...
 
 ~LoggerThread () override
 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 ()
 
 MThread (const MThread &)=delete
 
MThreadoperator= (const MThread &)=delete
 
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 p=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 (std::chrono::milliseconds time=std::chrono::milliseconds::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 &name)
 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) const
 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...
 
bool m_quiet
 silence the console (console only) More...
 
QString m_appname {QCoreApplication::applicationName()}
 Cached application name. More...
 
int m_facility
 Cached syslog facility (or -1 to disable) More...
 
pid_t m_pid
 Cached pid value. More...
 

Friends

MBASE_PUBLIC void LogPrintLine (uint64_t mask, LogLevel_t level, const char *file, int line, const char *function, QString message)
 Format and send a log message into the queue. More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from MThread
static void setTerminationEnabled (bool enabled=true)
 
static void usleep (std::chrono::microseconds time)
 
template<typename R , typename P >
static std::enable_if_t< std::chrono::treat_as_floating_point< R >::value, void > usleep (std::chrono::duration< R, P > 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 144 of file logging.h.

Constructor & Destructor Documentation

◆ LoggerThread()

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

LoggerThread constructor.

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

Definition at line 218 of file logging.cpp.

◆ ~LoggerThread()

LoggerThread::~LoggerThread ( )
override

LoggerThread destructor. Triggers the deletion of all loggers.

Definition at line 242 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 256 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 464 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 476 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 312 of file logging.cpp.

Referenced by LogPrintLine(), and run().

◆ fillItem()

void LoggerThread::fillItem ( LoggingItem item)

Definition at line 490 of file logging.cpp.

Referenced by run().

◆ Q_DISABLE_COPY()

LoggerThread::Q_DISABLE_COPY ( LoggerThread  )
private

◆ logConsole()

bool LoggerThread::logConsole ( LoggingItem item) const
protected

Process a log message, writing to the console.

Parameters
itemLoggingItem containing the log message to process

Definition at line 364 of file logging.cpp.

Referenced by LogPrintLine(), and run().

Friends And Related Function Documentation

◆ LogPrintLine

MBASE_PUBLIC void LogPrintLine ( uint64_t  mask,
LogLevel_t  level,
const char *  file,
int  line,
const char *  function,
QString  message 
)
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
messagelog message

Definition at line 528 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 160 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 164 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 168 of file logging.h.

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

◆ m_filename

QString LoggerThread::m_filename
private

Filename of debug logfile.

Definition at line 170 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 171 of file logging.h.

Referenced by logConsole().

◆ m_quiet

bool LoggerThread::m_quiet
private

silence the console (console only)

Definition at line 172 of file logging.h.

Referenced by logConsole().

◆ m_appname

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

Cached application name.

Definition at line 173 of file logging.h.

Referenced by fillItem().

◆ m_facility

int LoggerThread::m_facility
private

Cached syslog facility (or -1 to disable)

Definition at line 175 of file logging.h.

Referenced by fillItem().

◆ m_pid

pid_t LoggerThread::m_pid
private

Cached pid value.

Definition at line 176 of file logging.h.

Referenced by fillItem().


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