common/radiologger.cpp
branchRCL_3
changeset 20 93c594350b9a
parent 19 cce62ebc198e
equal deleted inserted replaced
19:cce62ebc198e 20:93c594350b9a
     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>
       
    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 && !defined BUILD_WIN32
       
    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 && !defined BUILD_WIN32
       
    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 && !defined BUILD_WIN32
       
    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 #if defined(__WINS__)
       
    95 static QString formatString( const char* format, long val )
       
    96 {
       
    97     if ( strcmp( format, "" ) == 0 ) {
       
    98         return "";
       
    99     }
       
   100     QString string;
       
   101     string.sprintf( format, val );
       
   102     return string;
       
   103 }
       
   104 #endif
       
   105 
       
   106 /*!
       
   107  *
       
   108  */
       
   109 MethodLogger::MethodLogger( const char* function, const char* format ) :
       
   110     mFunction( function ),
       
   111     mFormat( format )
       
   112 {
       
   113     QString msg;
       
   114     msg.sprintf( KLogEnter, mFunction );
       
   115     WRITELOG_METHOD_ENTER( msg );
       
   116 }
       
   117 
       
   118 /*!
       
   119  *
       
   120  */
       
   121 MethodLogger::~MethodLogger()
       
   122 {
       
   123     if ( std::uncaught_exception() ) {
       
   124         QString msg;
       
   125         msg.sprintf( KLogExitException, mFunction );
       
   126         WRITELOG_METHOD_EXIT( msg );
       
   127     } else {
       
   128         QString msg;
       
   129         if ( strlen( mFormat ) > 0 ) {
       
   130             #if defined(__WINS__)
       
   131 
       
   132                 long retVal = 0;
       
   133                 _asm( mov retVal, ebx );
       
   134                 msg.sprintf( KLogExitRet, mFunction, formatString( mFormat, retVal ) );
       
   135                 WRITELOG_METHOD_EXIT( msg );
       
   136 
       
   137             #else
       
   138                 msg.sprintf( KLogExit, mFunction );
       
   139                 WRITELOG_METHOD_EXIT( msg );
       
   140             #endif
       
   141         } else {
       
   142             msg.sprintf( KLogExit, mFunction );
       
   143             WRITELOG_METHOD_EXIT( msg );
       
   144         }
       
   145     }
       
   146 }
       
   147 #else
       
   148 
       
   149 MethodLogger::MethodLogger( const char*, const char* )
       
   150 {
       
   151 }
       
   152         
       
   153 MethodLogger::~MethodLogger()
       
   154 {
       
   155 }
       
   156 
       
   157 #endif // LOGGING_ENABLED
       
   158 
       
   159 
       
   160 #ifdef TRACE_TO_FILE
       
   161 
       
   162 /*!
       
   163  *
       
   164  */
       
   165 void FileLogger::installMessageHandler( const QString& fileName, bool filterByMarker )
       
   166 {
       
   167     mDebugFile.setFileName( fileName );
       
   168     mFilterByLogMarker = filterByMarker;
       
   169 
       
   170     mDebugFile.open( QIODevice::Text | QIODevice::WriteOnly );
       
   171 
       
   172     if ( mDebugFile.isOpen() ) {
       
   173         qInstallMsgHandler( FileLogger::handleMessage );
       
   174     }
       
   175 }
       
   176 
       
   177 /*!
       
   178  *
       
   179  */
       
   180 void FileLogger::uninstallMessageHandler()
       
   181 {
       
   182     if ( mDebugFile.isOpen() ) {
       
   183         qInstallMsgHandler( 0 );
       
   184         mDebugFile.close();
       
   185     }
       
   186 }
       
   187 
       
   188 /*!
       
   189  *
       
   190  */
       
   191 void FileLogger::handleMessage( QtMsgType type, const char* msg )
       
   192 {
       
   193     QTextStream debugStream( &mDebugFile );
       
   194     QDateTime dateTime( QDateTime::currentDateTime() );
       
   195 
       
   196     if ( type == QtDebugMsg ) {
       
   197         QString message( msg );
       
   198         if ( mFilterByLogMarker && message.contains( QString( LOGMARKER ) ) )
       
   199         {
       
   200             debugStream << dateTime.toString("yyyy-MM-dd hh:mm:ss.zzz") << " : " << message << endl;
       
   201         }
       
   202     }
       
   203 }
       
   204 #endif // TRACE_TO_FILE