-rw-r--r-- | libopie2/opiecore/odebug.cpp | 203 | ||||
-rw-r--r-- | libopie2/opiecore/oglobal.cpp | 9 | ||||
-rw-r--r-- | libopie2/opiecore/oglobal.h | 1 |
3 files changed, 117 insertions, 96 deletions
diff --git a/libopie2/opiecore/odebug.cpp b/libopie2/opiecore/odebug.cpp index f258faa..3bffdd0 100644 --- a/libopie2/opiecore/odebug.cpp +++ b/libopie2/opiecore/odebug.cpp @@ -70,10 +70,2 @@ namespace Core { namespace Internal { -class DebugBackend { -}; - -static DebugBackend *backEnd = 0; -} -static void clean_up_routine() { - delete Internal::backEnd; -} /*====================================================================================== @@ -97,4 +89,30 @@ enum DebugLevels { -static void oDebugBackend( unsigned short level, unsigned int area, const char *data) -{ + + +struct DebugBackend { + DebugBackend() : m_opened( false ), m_file( 0 ) ,m_port( -1 ),m_sock( 0 ) { + m_outp = OGlobalSettings::debugMode(); + } + ~DebugBackend() { + delete m_file; + delete m_sock; + } + void debug( unsigned short level, unsigned int, const QString& data ); + +private: + void debugFile( const QString&, const QString& data ); + void debugMsgB( const QString&, const QString& data ); + void debugShel( const QString&, const QString& data ); + void debugSysl( int, const QString& ); + void debugSock( const QString&, const QString& data ); + QCString line( const QString&, const QString& data ); + bool m_opened : 1; + QFile *m_file; + QHostAddress m_addr; + int m_port; + QSocketDevice *m_sock; + short m_outp; +}; + +void DebugBackend::debug(unsigned short level, unsigned int, const QString& data) { //qDebug( "oDebugBackend: Level=%d, Area=%d, Data=%s", level, area, data ); @@ -117,7 +135,5 @@ static void oDebugBackend( unsigned short level, unsigned int area, const char * - short output = OGlobalSettings::debugMode(); - if (!oApp && (output == 1)) - { + if (!oApp && (m_outp == 1)) { qDebug( "oDebugBackend: Warning: no oapplication object - can't use MsgBox" ); - output = 2; // need an application object to use MsgBox + m_outp = 2; // need an application object to use MsgBox } @@ -130,83 +146,75 @@ static void oDebugBackend( unsigned short level, unsigned int area, const char * - // Output - switch( output ) - { + switch( m_outp ) { case -1: // ignore - { return; - } case 0: // File - { - QString outputFilename = OGlobalSettings::debugOutput(); - - const int BUFSIZE = 4096; - char buf[BUFSIZE] = ""; - buf[BUFSIZE-1] = '\0'; - int nSize; - - nSize = snprintf( buf, BUFSIZE-1, "%s: %s", (const char*) areaName, data); - - QFile outputFile( outputFilename ); - if ( outputFile.open( IO_WriteOnly | IO_Append ) ) - { - if ( ( nSize == -1 ) || ( nSize >= BUFSIZE ) ) - { - outputFile.writeBlock( buf, BUFSIZE-1 ); + return debugFile( areaName, data ); + case 1: // Message Box + return debugMsgB( areaName, data ); + case 2: + return debugShel( areaName,data ); + case 3: // syslog + return debugSysl( priority, data ); + case 4: // socket + return debugSock( areaName, data ); } - else - { - outputFile.writeBlock( buf, nSize ); } + +inline void DebugBackend::debugFile(const QString& area, const QString& data) { + /* something went wrong with the file don't bother.. */ + if ( m_opened && !m_file ) + return; + else if ( !m_opened ) { + m_opened = true; + m_file = new QFile( OGlobalSettings::debugOutput() ); + if (!m_file->open( IO_WriteOnly | IO_Append ) ) { + delete m_file; m_file = 0; + qDebug( "ODebug: can't write to file '%s' (%s)", (const char*)OGlobalSettings::debugOutput(), + strerror(errno) ); + return; } - else - { - qDebug( "ODebug: can't write to file '%s' (%s)", (const char*) outputFilename, strerror(errno) ); } - break; - } // automatic close of file here - case 1: // Message Box - { - // Since we are in opiecore here, we cannot use OMsgBox and use - // QMessageBox instead + /* go to end of file */ + m_file->at( m_file->size() ); + QCString li = line( area, data ); + m_file->writeBlock(li.data(), li.length() ); +} - caption += QString("(") + areaName + ")"; - QMessageBox::warning( 0L, caption, data, ("&OK") ); // tr? - break; +void DebugBackend::debugMsgB( const QString& area, const QString& data ) { + QMessageBox::warning( 0l, "("+area+")", data, ("Ok") ); } - case 2: // Shell - { +void DebugBackend::debugShel( const QString& are, const QString& data ) { FILE *output = stderr; - fprintf( output, "%s: ", (const char*) areaName ); - fputs( data, output); - break; + fprintf( output, "%s: %s", are.local8Bit().data(), + data.local8Bit().data() ); } - case 3: // syslog - { - syslog( priority, "%s", data); - break; +void DebugBackend::debugSysl( int prio, const QString& data ) { + ::syslog( prio, "%s", data.local8Bit().data() ); } - case 4: // socket - { +void DebugBackend::debugSock( const QString& are, const QString& data ) { + if ( m_opened && !m_sock ) + return; + else if ( !m_opened ){ + m_opened = true; QString destination = OGlobalSettings::debugOutput(); - if ( destination && destination.find(":") != -1 ) - { + if ( destination && destination.find(":") != -1 ) { QString host = destination.left( destination.find(":") ); - QString port = destination.right( destination.length()-host.length()-1 ); - QHostAddress addr; - addr.setAddress( host ); - // TODO: sanity check the address - QString line; - line.sprintf( "%s: %s", (const char*) areaName, (const char*) data ); - QSocketDevice s( QSocketDevice::Datagram ); - int result = s.writeBlock( (const char*) line, line.length(), addr, port.toInt() ); - if ( result == -1 ) - { - qDebug( "ODebug: can't send to address '%s:%d' (%s)", (const char*) host, port.toInt(), strerror(errno) ); + m_port = destination.right( destination.length()-host.length()-1 ).toInt(); + m_addr.setAddress( host ); + m_sock = new QSocketDevice( QSocketDevice::Datagram ); + }else{ + m_sock = 0; + return; } } - break; + + QCString li = line( are, data ); + int result = m_sock->writeBlock(li.data(), li.length(), m_addr, m_port ); + if ( result == -1 ) { + qDebug( "ODebug: can't send to address '"+ m_addr.toString() +":%d' (%s)", + m_port, strerror(errno) ); } @@ -214,13 +222,14 @@ static void oDebugBackend( unsigned short level, unsigned int area, const char * - // check if we should abort - - /* - - if( ( nLevel == ODEBUG_FATAL ) - && ( !oDebug_data->config || oDebug_data->config->readNumEntry( "AbortFatal", 1 ) ) ) - abort(); - - */ +QCString DebugBackend::line( const QString& area, const QString& data ) { + QString str = area +":"+data; + return str.local8Bit(); } +static DebugBackend *backEnd = 0; +} +static void clean_up_routine() { + qWarning( "Clean up Debug" ); + delete Internal::backEnd; + Internal::backEnd = 0; +} /*====================================================================================== @@ -236,3 +245,3 @@ odbgstream odDebug(int area) { - return odbgstream(area, ODEBUG_INFO); + return odbgstream(area, Internal::ODEBUG_INFO); } @@ -240,3 +249,3 @@ odbgstream odDebug(bool cond, int area) { - if (cond) return odbgstream(area, ODEBUG_INFO); + if (cond) return odbgstream(area, Internal::ODEBUG_INFO); else return odbgstream(0, 0, false); @@ -246,3 +255,3 @@ odbgstream odError(int area) { - return odbgstream("ERROR: ", area, ODEBUG_ERROR); + return odbgstream("ERROR: ", area, Internal::ODEBUG_ERROR); } @@ -251,3 +260,3 @@ odbgstream odError(bool cond, int area) { - if (cond) return odbgstream("ERROR: ", area, ODEBUG_ERROR); else return odbgstream(0,0,false); + if (cond) return odbgstream("ERROR: ", area, Internal::ODEBUG_ERROR); else return odbgstream(0,0,false); } @@ -256,3 +265,3 @@ odbgstream odWarning(int area) { - return odbgstream("WARNING: ", area, ODEBUG_WARN); + return odbgstream("WARNING: ", area, Internal::ODEBUG_WARN); } @@ -261,3 +270,3 @@ odbgstream odWarning(bool cond, int area) { - if (cond) return odbgstream("WARNING: ", area, ODEBUG_WARN); else return odbgstream(0,0,false); + if (cond) return odbgstream("WARNING: ", area, Internal::ODEBUG_WARN); else return odbgstream(0,0,false); } @@ -266,3 +275,3 @@ odbgstream odFatal(int area) { - return odbgstream("FATAL: ", area, ODEBUG_FATAL); + return odbgstream("FATAL: ", area, Internal::ODEBUG_FATAL); } @@ -271,3 +280,3 @@ odbgstream odFatal(bool cond, int area) { - if (cond) return odbgstream("FATAL: ", area, ODEBUG_FATAL); else return odbgstream(0,0,false); + if (cond) return odbgstream("FATAL: ", area, Internal::ODEBUG_FATAL); else return odbgstream(0,0,false); } @@ -415,3 +424,7 @@ void odbgstream::flush() { - oDebugBackend( level, area, output.local8Bit().data() ); + if ( !Internal::backEnd ) { + Internal::backEnd = new Internal::DebugBackend; + qAddPostRoutine( clean_up_routine ); + } + Internal::backEnd->debug( level, area, output ); output = QString::null; @@ -541,5 +554,4 @@ odbgstream& odbgstream::operator<<( const QRegion& reg ) { - /* Qt2 doesn't have a QMemArray... :( *this << "[ "; - QMemArray<QRect>rs=reg.rects(); + QArray<QRect>rs=reg.rects(); for (uint i=0;i<rs.size();++i) @@ -547,3 +559,2 @@ odbgstream& odbgstream::operator<<( const QRegion& reg ) *this <<"]"; - */ return *this; diff --git a/libopie2/opiecore/oglobal.cpp b/libopie2/opiecore/oglobal.cpp index ea02058..2968a7d 100644 --- a/libopie2/opiecore/oglobal.cpp +++ b/libopie2/opiecore/oglobal.cpp @@ -78,2 +78,10 @@ OConfig* OGlobal::_qpe_config = 0; +void OGlobal::clean_up() { + qWarning( "Oglobal clean up" ); + delete OGlobal::_config; + delete OGlobal::_qpe_config; + OGlobal::_config = 0; + OGlobal::_qpe_config = 0; +} + OConfig* OGlobal::config() @@ -83,2 +91,3 @@ OConfig* OGlobal::config() // odebug classes are reading config, so can't use them here! + qAddPostRoutine( OGlobal::clean_up ); qDebug( "OGlobal::creating global configuration instance." ); diff --git a/libopie2/opiecore/oglobal.h b/libopie2/opiecore/oglobal.h index e6a6c46..d79a218 100644 --- a/libopie2/opiecore/oglobal.h +++ b/libopie2/opiecore/oglobal.h @@ -151,2 +151,3 @@ public: private: + static void clean_up(); static OConfig* _config; |