You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

51 lines
2.0 KiB

#include "objectMySQLLoggingService.h"
objectMySQLLoggingService::objectMySQLLoggingService(){
m_bLogging = false;
m_pDBC = NULL;
};
objectMySQLLoggingService::objectMySQLLoggingService( serverDataBinder *ptr ){
m_pDBC = ptr;
m_bLogging = ( m_pDBC != NULL );
};
void objectMySQLLoggingService::logMessage( CSTR Msg ){
if( !m_bLogging ) return;
logMessage( (const char * ) Msg );
};
/*
Runs inside the log broadcast (log mutex held), possibly on a thread that is itself inside a database
call. Writing synchronously deadlocked the server on the first MySQL error (non-recursive DB mutex on
the same thread, or DB mutex <-> log mutex between two threads). Now the line is queued and the queue
is written only while the database is free (putIntoLog never blocks); the rest goes out with a later line.
*/
void objectMySQLLoggingService::logMessage( const char *Msg ){
if( !m_bLogging || Msg == NULL ) return;
if( m_pDBC == NULL ){
m_bLogging = false;
return;
};
// Expected format: "LL YYYY-MM-DD HH:MM:SS <msg>" (shorter lines were read past their end)
if ( strlen( Msg ) < 23 ) return;
MYSQL_LOG_LINE tLine;
strncpy( tLine.type, Msg, 2 );
tLine.type[2] = (char) 0;
strncpy( tLine.ts, Msg+3, 19 );
tLine.ts[19] = (char) 0;
// The timestamp may carry a UTC offset ("...HH:MM:SS+0200"): the message starts after it
const char *tRest = Msg + 22;
while ( *tRest && *tRest != ' ' ) tRest++;
if ( *tRest == ' ' ) tRest++;
tLine.msg = std::string( tRest );
std::lock_guard<std::mutex> tGuard( m_mPending );
m_qPending.push_back( tLine );
while ( m_qPending.size() > MYSQL_LOG_MAX_PENDING ) m_qPending.pop_front(); // bounded: drop the oldest
while ( !m_qPending.empty() ) {
MYSQL_LOG_LINE &tL = m_qPending.front();
if ( !m_pDBC->putIntoLog( tL.type, tL.ts, (CSTR) tL.msg.c_str() ) ) break; // busy: retry with the next line
m_qPending.pop_front();
};
};