radioapp/commoninc/radiologger.h
changeset 16 f54ebcfc1b80
child 19 afea38384506
equal deleted inserted replaced
14:63aabac4416d 16:f54ebcfc1b80
       
     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 #ifndef _RADIOLOGGER_H_
       
    19 #define _RADIOLOGGER_H_
       
    20 
       
    21 // System includes
       
    22 #include <QObject>
       
    23 
       
    24 // User includes
       
    25 
       
    26 #ifdef BUILD_WRAPPER_DLL
       
    27     #define WRAPPER_DLL_EXPORT Q_DECL_EXPORT
       
    28 #else
       
    29     #define WRAPPER_DLL_EXPORT Q_DECL_IMPORT
       
    30 #endif
       
    31 
       
    32 class WRAPPER_DLL_EXPORT RadioLogger
       
    33 {
       
    34 public:
       
    35 
       
    36     enum Mode { Normal, MethodEnter, MethodExit, DecIndent };
       
    37 
       
    38     static void initCombinedLogger();
       
    39     static void releaseCombinedLogger();
       
    40 
       
    41     static void logMsg( const char* msg, Mode mode = Normal );
       
    42 
       
    43 private:
       
    44     RadioLogger();
       
    45     ~RadioLogger();
       
    46 };
       
    47 
       
    48 // ============================================================================
       
    49 // START TIMESTAMP LOGGING
       
    50 // ============================================================================
       
    51 
       
    52 #ifdef TIMESTAMP_LOGGING_ENABLED
       
    53 
       
    54 #include <QTime>
       
    55 #include <QDebug>
       
    56 
       
    57 /**
       
    58  * Timestamp logging macro.
       
    59  * When the macro is defined, timestamp logging is on
       
    60  *
       
    61  * Example. These lines in the code...
       
    62  *  LOG_TIMESTAMP( "Start operation" );
       
    63  *  ...
       
    64  *  LOG_TIMESTAMP( "End operation" );
       
    65  *
       
    66  * ... print the following log lines
       
    67  *  FMRadio: Start operation "14:13:09.042"
       
    68  *  FMRadio: End operation "14:13:09.250"
       
    69  */
       
    70 //#define LOG_TIMESTAMP(comment) do{ qDebug() << LOGMARKER << comment << QTime::currentTime().toString("hh:mm:ss.zzz"); }while(0)
       
    71 #define LOG_TIMESTAMP(comment) LOG_FORMAT( comment ## " %s", GETSTRING( QTime::currentTime().toString("hh:mm:ss.zzz") ) )
       
    72 
       
    73 #else
       
    74 
       
    75 #define LOG_TIMESTAMP(comment)
       
    76 
       
    77 #endif // TIMESTAMP_LOGGING_ENABLED
       
    78 
       
    79 // ============================================================================
       
    80 // END TIMESTAMP LOGGING
       
    81 // ============================================================================
       
    82 
       
    83 // ============================================================================
       
    84 // START FULL LOGGING
       
    85 // ============================================================================
       
    86 
       
    87 #ifdef LOGGING_ENABLED
       
    88 
       
    89 #include <QtGlobal>
       
    90 #include <QDebug>
       
    91 
       
    92 // UI logs can be combined with engine logs by making the UI feed its log prints into
       
    93 // the engine logger. This requires that we initialize the radio engine utils right here
       
    94 // because the engine won't start up until much later. This is a bit ugly since the macros
       
    95 // call Symbian code directly, but it was considered to be worth it to see UI and engine
       
    96 // traces in the same file.
       
    97 #if defined COMBINE_WITH_ENGINE_LOGGER && defined LOGGING_ENABLED && !defined BUILD_WIN32
       
    98 #   include "../../../radioengine/utils/api/mradioenginelogger.h"
       
    99 #   include "../../../radioengine/utils/api/radioengineutils.h"
       
   100 #   define WRITELOG(msg) RadioLogger::logMsg( msg );
       
   101 #   define WRITELOG_METHOD_ENTER(msg) RadioLogger::logMsg( GETSTRING( msg ), RadioLogger::MethodEnter );
       
   102 #   define WRITELOG_METHOD_EXIT(msg) RadioLogger::logMsg( GETSTRING( msg ), RadioLogger::MethodExit );
       
   103 #   define LOGGER_DEC_INDENT RadioLogger::logMsg( "", RadioLogger::DecIndent );
       
   104 #   define WRITELOG_GETSTRING(msg) WRITELOG( GETSTRING( msg ) )
       
   105 #   define INIT_COMBINED_LOGGER RadioLogger::initCombinedLogger();
       
   106 #   define RELEASE_COMBINED_LOGGER RadioLogger::releaseCombinedLogger();
       
   107 #else
       
   108 #   define WRITELOG(msg) qDebug() << LOGMARKER << msg;
       
   109 #   define WRITELOG_METHOD_ENTER(msg) WRITELOG(msg)
       
   110 #   define WRITELOG_METHOD_EXIT(msg) WRITELOG(msg)
       
   111 #   define WRITELOG_GETSTRING(msg) WRITELOG(msg)
       
   112 #   define LOGGER_INC_INDENT
       
   113 #   define LOGGER_DEC_INDENT
       
   114 #   define INIT_COMBINED_LOGGER
       
   115 #   define RELEASE_COMBINED_LOGGER
       
   116 #endif
       
   117 
       
   118 // Macro that simply logs a string
       
   119 // Example:
       
   120 // LOG( "This is a test" );
       
   121 #define LOG(string) do{ WRITELOG( string ) }while(0)
       
   122 
       
   123 // Helper macro to get a const char* out of a QString so that it can be logged. Can be used with LOG_FORMAT()
       
   124 #define GETSTRING(qstring) qstring.toAscii().constData()
       
   125 
       
   126 // Macro that logs a string with multiple parameters
       
   127 // Examples:
       
   128 // LOG_FORMAT( "This is an integer %d, and this is a float with two digits %.2f", 42, 3.14 );
       
   129 // LOG_FORMAT( "This is a QString %s", GETSTRING(someQString) );
       
   130 #define LOG_FORMAT(fmt,args...) do{ QString tmp; WRITELOG_GETSTRING( tmp.sprintf(fmt,args) ) }while(0)
       
   131 
       
   132 // Macro that logs function enter, exit and exception
       
   133 // Example (Simply put it in the beginning of a function):
       
   134 // LOG_METHOD;
       
   135 // Output:
       
   136 // -> SomeFunction(int,const char*)
       
   137 // <- SomeFunction(int,const char*)
       
   138 // <- SomeFunction(int,const char*): Exception raised!
       
   139 #define LOG_METHOD MethodLogger ___methodLogger( __PRETTY_FUNCTION__, "" )
       
   140 
       
   141 // Same as the previous function logging macro with the addition of logging the return value
       
   142 // Note! The return value can only be retrieved in the emulator.
       
   143 // Example (Simply put it in the beginning of a function):
       
   144 // LOG_METHOD_RET( "%d" );
       
   145 // Output:
       
   146 // -> SomeFunction(int,const char*)
       
   147 // <- SomeFunction(int,const char*) returning 42
       
   148 #define LOG_METHOD_RET(fmt) MethodLogger ___methodLogger( __PRETTY_FUNCTION__, fmt )
       
   149 
       
   150 // Logs function enter but does not log exit or leave. This is meant to be lighter than LOG_METHOD macro
       
   151 #define LOG_METHOD_ENTER LOG_FORMAT( "Enter: %s", __PRETTY_FUNCTION__ )
       
   152 
       
   153 // Assert macro for logging. If the condition is false, the expression is performed
       
   154 // Example:
       
   155 // LOG_ASSERT( thisMustBeTrue, LOG_FORMAT( "OMG! That was not true: %d", thisMustBeTrue ) );
       
   156 #define LOG_ASSERT(cond,expr) do{ if (!cond) { expr; } }while(0)
       
   157 
       
   158 // Macro to hide a function variable that is used only when debugging is enabled.
       
   159 // Expands to the variable name when debugging is enabled and to nothing when it is not
       
   160 #define DEBUGVAR(a) a
       
   161 
       
   162 // Macro to log slot function caller by its class name.
       
   163 #define LOG_SLOT_CALLER do { \
       
   164         QObject* caller = sender(); \
       
   165         if ( caller ) { \
       
   166             LOG_FORMAT( "SLOT %s called by %s. Objectname: %s", __PRETTY_FUNCTION__, \
       
   167                 caller->metaObject()->className(), GETSTRING( caller->objectName() ) ); \
       
   168         } else { \
       
   169             LOG_FORMAT( "SLOT %s called as regular function. ", __PRETTY_FUNCTION__ ); \
       
   170         } \
       
   171     } while (0)
       
   172 
       
   173 // Class declaration
       
   174 class WRAPPER_DLL_EXPORT MethodLogger
       
   175 {
       
   176 public:
       
   177 
       
   178     MethodLogger( const char* function, const char* format );
       
   179     ~MethodLogger();
       
   180 
       
   181 private:
       
   182 
       
   183     const char* mFunction;
       
   184     const char* mFormat;
       
   185 
       
   186 };
       
   187 
       
   188 #else // LOGGING_ENABLED
       
   189 
       
   190 #   define LOG(string)
       
   191 #   define GETSTRING(qstring)
       
   192 #   define LOG_FORMAT(fmt,args...)
       
   193 #   define LOG_METHOD
       
   194 #   define LOG_METHOD_RET(fmt)
       
   195 #   define LOG_METHOD_ENTER
       
   196 #   define LOG_ASSERT(cond,expr)
       
   197 #   define DEBUGVAR(a)
       
   198 #   define LOG_SLOT_CALLER
       
   199 #   define INIT_COMBINED_LOGGER
       
   200 #   define RELEASE_COMBINED_LOGGER
       
   201 
       
   202 
       
   203 // Dummy class
       
   204 class WRAPPER_DLL_EXPORT MethodLogger
       
   205 {
       
   206 public:
       
   207     MethodLogger( const char*, const char* );
       
   208     ~MethodLogger();
       
   209 };
       
   210 
       
   211 #endif // LOGGING_ENABLED
       
   212 
       
   213 #ifdef TRACE_TO_FILE
       
   214 
       
   215 #   define INSTALL_MESSAGE_HANDLER FileLogger::installMessageHandler(QString(TRACE_OUTPUT_FILE), FILTER_BY_LOGMARKER);
       
   216 #   define UNINSTALL_MESSAGE_HANDLER FileLogger::uninstallMessageHandler();
       
   217 
       
   218 // Class declaration
       
   219 class WRAPPER_DLL_EXPORT FileLogger
       
   220 {
       
   221 public:
       
   222 
       
   223     static void installMessageHandler( const QString& fileName, bool filterByMarker = true );
       
   224     static void uninstallMessageHandler();
       
   225 
       
   226 private:
       
   227 
       
   228     static void handleMessage( QtMsgType type, const char* msg );
       
   229 
       
   230 };
       
   231 
       
   232 #else
       
   233 #   define INSTALL_MESSAGE_HANDLER
       
   234 #   define UNINSTALL_MESSAGE_HANDLER
       
   235 #endif // TRACE_TO_FILE
       
   236 
       
   237 // ============================================================================
       
   238 // END FULL LOGGING
       
   239 // ============================================================================
       
   240 
       
   241 // ============================================================================
       
   242 // SIGNAL/SLOT CONNECTION CHECKER
       
   243 // ============================================================================
       
   244 static bool connectAndTest( const QObject* sender, const char* signal,
       
   245                             const QObject* receiver, const char* member,
       
   246                             Qt::ConnectionType type = Qt::AutoConnection )
       
   247 {
       
   248     bool connected = QObject::connect( sender, signal, receiver, member, type );
       
   249 
       
   250 #   ifdef CONNECT_TEST_MODE
       
   251 
       
   252     if ( !connected )
       
   253     {
       
   254         LOG( "Failed to make a signal-slot connection!" );
       
   255         LOG_FORMAT( "sender: %s", sender->metaObject()->className() );
       
   256         LOG_FORMAT( "signal: %s", signal );
       
   257         LOG_FORMAT( "receiver: %s", receiver->metaObject()->className() );
       
   258         LOG_FORMAT( "slot/signal: %s", signal );
       
   259 
       
   260         #if CONNECT_TEST_MODE == 2
       
   261             Q_ASSERT( false );
       
   262         #endif
       
   263 
       
   264         // ----------------------------------------------------------------
       
   265         // SIGNAL-SLOT CONNECTION FAILED!
       
   266         // ----------------------------------------------------------------
       
   267     }
       
   268 
       
   269 #   endif
       
   270 
       
   271     return connected;
       
   272 }
       
   273 
       
   274 #ifdef ENABLE_ASSERTS
       
   275 
       
   276     #define RADIO_ASSERT(cond,where,what) Q_ASSERT_X(cond,where,what)
       
   277 
       
   278 #else
       
   279 #   ifdef LOGGING_ENABLED
       
   280 #       define RADIO_ASSERT(cond,where,what) \
       
   281             do { \
       
   282                 if ( !cond ) { \
       
   283                     LOG_FORMAT( "ASSERT Failed! %s, %s", where, what ); \
       
   284                 } \
       
   285             } while ( false )
       
   286 #   else
       
   287 #       define RADIO_ASSERT(cond,where,what)
       
   288 #   endif // LOGGING_ENABLED
       
   289 #endif // ENABLE_ASSERTS
       
   290 
       
   291 #endif // _RADIOLOGGER_H_