radioapp/radioenginewrapper/src/radiologger.cpp
changeset 13 46974bebc798
child 14 63aabac4416d
equal deleted inserted replaced
0:f3d95d9c00ab 13:46974bebc798
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <qdebug.h>
       
    20 
       
    21 // User includes
       
    22 #include "radiologger.h"
       
    23 
       
    24 /*!
       
    25  *
       
    26  */
       
    27 void RadioLogger::initCombinedLogger()
       
    28 {
       
    29 #if defined LOGGING_ENABLED && defined COMBINE_WITH_ENGINE_LOGGER
       
    30     TRAP_IGNORE( RadioEngineUtils::InitializeL() );
       
    31     if ( !MRadioEngineLogger::Logger() ) {
       
    32         qDebug() << "FMRadioUi: Cannot combine logs with engine. Engine logger not active";
       
    33     }
       
    34 #endif // LOGGING_ENABLED
       
    35 }
       
    36 
       
    37 /*!
       
    38  *
       
    39  */
       
    40 void RadioLogger::releaseCombinedLogger()
       
    41 {
       
    42 #if defined LOGGING_ENABLED && defined COMBINE_WITH_ENGINE_LOGGER
       
    43     RadioEngineUtils::Release();
       
    44 #endif // LOGGING_ENABLED
       
    45 }
       
    46 
       
    47 /*!
       
    48  *    enum Mode { Normal, MethodEnter, MethodExit, DecIndent };
       
    49  */
       
    50 void RadioLogger::logMsg( const char* msg, Mode mode )
       
    51 {
       
    52     Q_UNUSED( msg );
       
    53     Q_UNUSED( mode );
       
    54 #if defined LOGGING_ENABLED && defined COMBINE_WITH_ENGINE_LOGGER
       
    55     MRadioEngineLogger* logger = MRadioEngineLogger::Logger();
       
    56     if ( logger ) {
       
    57         if ( mode == RadioLogger::Normal ) {
       
    58             logger->AddIndent(KMarkerUi()).Add( msg ).Commit();
       
    59         } else if ( mode == RadioLogger::MethodEnter ) {
       
    60             logger->AddIndentClear( KMarkerUi() ).Add( msg ).Commit();
       
    61             logger->IncIndent();
       
    62         } else if ( mode == RadioLogger::MethodExit ) {
       
    63             logger->DecIndent();
       
    64             logger->AddIndentClear( KMarkerUi() ).Add( msg ).Commit();
       
    65         } else if ( mode == RadioLogger::DecIndent ) {
       
    66             logger->DecIndent();
       
    67         } else {}
       
    68     }
       
    69 #endif // LOGGING_ENABLED
       
    70 }
       
    71 
       
    72 
       
    73 #ifdef LOGGING_ENABLED
       
    74 
       
    75 #include <qstring>
       
    76 #include <qfile>
       
    77 #include <qtextstream>
       
    78 #include <qdatetime>
       
    79 
       
    80 #ifdef TRACE_TO_FILE
       
    81     QFile mDebugFile;
       
    82     bool mFilterByLogMarker = false;
       
    83 #endif
       
    84 
       
    85 const char* KLogEnter = "\\ %s";
       
    86 const char* KLogExit = "/ %s";
       
    87 const char* KLogExitRet = "/ %s, Returning %s";
       
    88 const char* KLogExitException = "/ %s, Exception raised";
       
    89 const char* KLogLine = "| ";
       
    90 
       
    91 /*!
       
    92  *
       
    93  */
       
    94 static QString formatString( const char* format, long val )
       
    95 {
       
    96     if ( strcmp( format, "" ) == 0 ) {
       
    97         return "";
       
    98     }
       
    99     QString string;
       
   100     string.sprintf( format, val );
       
   101     return string;
       
   102 }
       
   103 
       
   104 /*!
       
   105  *
       
   106  */
       
   107 MethodLogger::MethodLogger( const char* function, const char* format ) :
       
   108     mFunction( function ),
       
   109     mFormat( format )
       
   110 {
       
   111     QString msg;
       
   112     msg.sprintf( KLogEnter, mFunction );
       
   113     WRITELOG_METHOD_ENTER( msg );
       
   114 }
       
   115 
       
   116 /*!
       
   117  *
       
   118  */
       
   119 MethodLogger::~MethodLogger()
       
   120 {
       
   121     if ( std::uncaught_exception() ) {
       
   122         QString msg;
       
   123         msg.sprintf( KLogExitException, mFunction );
       
   124         WRITELOG_METHOD_EXIT( msg );
       
   125     } else {
       
   126         QString msg;
       
   127         if ( strlen( mFormat ) > 0 ) {
       
   128             #if defined(__WINS__)
       
   129 
       
   130                 long retVal = 0;
       
   131                 _asm( mov retVal, ebx );
       
   132                 msg.sprintf( KLogExitRet, mFunction, formatString( mFormat, retVal ) );
       
   133                 WRITELOG_METHOD_EXIT( msg );
       
   134 
       
   135             #else
       
   136                 msg.sprintf( KLogExit, mFunction );
       
   137                 WRITELOG_METHOD_EXIT( msg );
       
   138             #endif
       
   139         } else {
       
   140             msg.sprintf( KLogExit, mFunction );
       
   141             WRITELOG_METHOD_EXIT( msg );
       
   142         }
       
   143     }
       
   144 }
       
   145 #endif // LOGGING_ENABLED
       
   146 
       
   147 
       
   148 #ifdef TRACE_TO_FILE
       
   149 
       
   150 /*!
       
   151  *
       
   152  */
       
   153 void FileLogger::installMessageHandler( const QString& fileName, bool filterByMarker )
       
   154 {
       
   155     mDebugFile.setFileName( fileName );
       
   156     mFilterByLogMarker = filterByMarker;
       
   157 
       
   158     mDebugFile.open( QIODevice::Text | QIODevice::WriteOnly );
       
   159 
       
   160     if ( mDebugFile.isOpen() ) {
       
   161         qInstallMsgHandler( FileLogger::handleMessage );
       
   162     }
       
   163 }
       
   164 
       
   165 /*!
       
   166  *
       
   167  */
       
   168 void FileLogger::uninstallMessageHandler()
       
   169 {
       
   170     if ( mDebugFile.isOpen() ) {
       
   171         qInstallMsgHandler( 0 );
       
   172         mDebugFile.close();
       
   173     }
       
   174 }
       
   175 
       
   176 /*!
       
   177  *
       
   178  */
       
   179 void FileLogger::handleMessage( QtMsgType type, const char* msg )
       
   180 {
       
   181     QTextStream debugStream( &mDebugFile );
       
   182     QDateTime dateTime( QDateTime::currentDateTime() );
       
   183 
       
   184     if ( type == QtDebugMsg ) {
       
   185         QString message( msg );
       
   186         if ( mFilterByLogMarker && message.contains( QString( LOGMARKER ) ) )
       
   187         {
       
   188             debugStream << dateTime.toString("yyyy-MM-dd hh:mm:ss.zzz") << " : " << message << endl;
       
   189         }
       
   190     }
       
   191 }
       
   192 #endif // TRACE_TO_FILE