emailservices/emaildebug/inc/emailtrace.h
changeset 40 2c62ef3caffd
parent 18 578830873419
child 30 759dc5235cdb
equal deleted inserted replaced
39:b0b89ca206b5 40:2c62ef3caffd
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  Header file describing trace utilities for commonemail
    14  * Description:
    15 *
    15  *
    16 */
    16  */
    17 
    17 
    18 #ifndef EMAILTRACE_H
    18 #ifndef EMAILTRACE_H
    19 #define EMAILTRACE_H
    19 #define EMAILTRACE_H
    20 
    20 
    21 #include "emailtraceconfig.hrh"
    21 #include <e32debug.h>
       
    22 #include <qdebug.h>
       
    23 #include <qfile.h>
    22 
    24 
    23 #ifdef TRACE_INTO_FILE
    25 /*
    24 #include <flogger.h> // RFileLogger
    26  * The macros COMMENT_TRACES, ERROR_TRACES, and FUNCTION_TRACES control which
    25 #else
    27  * trace messages are printed. The trace message logging is controlled with
    26 #include <e32debug.h> // RDebug
    28  * the LOG_TO_FILE macro, whereas the LOG_FILE macro defines which file is to
    27 #endif
    29  * be used in logging. The print_trace() helper function implements printing.
       
    30  * If LOG_TO_FILE is zero or the LOG_FILE cannot be opened, the messages are
       
    31  * printed to qDebug().
       
    32  */
       
    33 #if defined(DEBUG) || defined(_DEBUG)
    28 
    34 
    29 /**
    35 #define COMMENT_TRACES  1
    30 * Constants
    36 #define ERROR_TRACES    1
    31 */
    37 #define FUNCTION_TRACES 1
    32 #ifdef TRACE_INTO_FILE
       
    33 
    38 
    34     _LIT(  KEmailDir, "email" );
    39 #if COMMENT_TRACES || ERROR_TRACES || FUNCTION_TRACES
    35     _LIT(  KEmailTraceFile, "email.txt" );
    40 
       
    41 #define LOG_TO_FILE     0
       
    42 #define LOG_FILE        "c:/logs/nmail_trace.log"
       
    43 
       
    44 inline void print_trace(const QString& msg)
       
    45 {
       
    46     QFile out(LOG_FILE);
       
    47     if (LOG_TO_FILE && out.open(QIODevice::Append | QIODevice::Text)) {
       
    48         QDebug(&out) << "[Nmail]" << msg;
       
    49         out.putChar('\n');
       
    50         out.close();
       
    51     } else {
       
    52         qDebug() << "[Nmail]" << msg;
       
    53     }
       
    54 }
    36 
    55 
    37 #endif
    56 #endif
    38 
    57 
    39 //-----------------------------------------------------------------------------
    58 #endif /* DEBUG */
    40 // Error trace macros
       
    41 //-----------------------------------------------------------------------------
       
    42 //
       
    43 #ifdef ERROR_TRACE
       
    44 
    59 
    45     /**
    60 /*
    46     * Error trace definitions. Does not automatically log the error code!
    61  * The function NM_COMMENT() prints a trace message. The INFO macros are
    47     */
    62  * provided for legacy compatibility. They are deprecated and should not be
    48     #ifdef TRACE_INTO_FILE
    63  * used. If sprintf() type of formatting is desired, consider using QString::
       
    64  * arg() or QTextStream.
       
    65  */
       
    66 #if COMMENT_TRACES
    49 
    67 
    50         #define ERROR( aErr, aMsg )\
    68 inline void NM_COMMENT(const QString& msg)
    51             {\
    69 {
    52             if( aErr != KErrNone )\
    70     print_trace("COMMENT : " + msg);
    53                 {\
    71 }
    54                 _LIT( KMsg, aMsg );\
    72 #define INFO(msg) NM_COMMENT(msg)
    55                 RFileLogger::Write(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg );\
    73 #define INFO_1(msg,arg1)\
    56                 }\
    74 do {\
    57             }
    75     QString __msg;\
    58         #define ERROR_1( aErr, aMsg, aP1 )\
    76     __msg.sprintf(msg,arg1);\
    59             {\
    77     NM_COMMENT(__msg);\
    60             if( aErr != KErrNone )\
    78 } while (0)
    61                 {\
    79 #define INFO_2(msg,arg1,arg2)\
    62                 _LIT( KMsg, aMsg );\
    80 do {\
    63                 RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1 );\
    81     QString __msg;\
    64                 }\
    82     __msg.sprintf(msg,arg1,arg2);\
    65             }
    83     NM_COMMENT(__msg);\
    66         #define ERROR_2( aErr, aMsg, aP1, aP2 )\
    84 } while (0)
    67             {\
    85 #define INFO_3(msg,arg1,arg2,arg3)\
    68             if( aErr != KErrNone )\
    86 do {\
    69                 {\
    87     QString __msg;\
    70                 _LIT( KMsg, aMsg );\
    88     __msg.sprintf(msg,arg1,arg2,arg3);\
    71                 RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2 );\
    89     NM_COMMENT(__msg);\
    72                 }\
    90 } while (0)
    73             }
       
    74         #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\
       
    75             {\
       
    76             if( aErr != KErrNone )\
       
    77                 {\
       
    78                 _LIT( KMsg, aMsg );\
       
    79                 RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2, aP3 );\
       
    80                 }\
       
    81             }
       
    82         #define ERROR_GEN( aMsg ) ERROR( KErrGeneral, aMsg )
       
    83         #define ERROR_GEN_1( aMsg, aP1 ) ERROR_1( KErrGeneral, aMsg, aP1 )
       
    84 
    91 
    85     #else//TRACE_INTO_FILE not defined
    92 #else
    86 
    93 
    87         #define ERROR( aErr, aMsg )\
    94 #define NM_COMMENT(msg)
    88             {\
    95 #define INFO(msg)
    89             if( aErr != KErrNone )\
    96 #define INFO_1(msg,arg1)
    90                 {\
    97 #define INFO_2(msg,arg1,arg2)
    91                 _LIT( KMsg, aMsg ); RDebug::Print( KMsg );\
    98 #define INFO_3(msg,arg1,arg2,arg3)
    92                 }\
       
    93             }
       
    94         #define ERROR_1( aErr, aMsg, aP1 )\
       
    95             {\
       
    96             if( aErr != KErrNone )\
       
    97                 {\
       
    98                 _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1 );\
       
    99                 }\
       
   100             }
       
   101         #define ERROR_2( aErr, aMsg, aP1, aP2 )\
       
   102             {\
       
   103             if( aErr != KErrNone )\
       
   104                 {\
       
   105                 _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2 );\
       
   106                 }\
       
   107             }
       
   108         #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\
       
   109             {\
       
   110             if( aErr != KErrNone )\
       
   111                 {\
       
   112                 _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2, aP3 );\
       
   113                 }\
       
   114             }
       
   115         #define ERROR_GEN( aMsg ) ERROR( KErrGeneral, aMsg )
       
   116         #define ERROR_GEN_1( aMsg, aP1 ) ERROR_1( KErrGeneral, aMsg, aP1 )
       
   117 
    99 
   118     #endif//TRACE_INTO_FILE
   100 #endif /* COMMENT_TRACES */
   119 
   101 
   120 #else//ERROR_TRACE not defined
   102 /*
       
   103  * The function NM_ERROR() prints its second argument if the first argument
       
   104  * is non-zero. The ERROR macros are provided for legacy compatibility. They
       
   105  * are deprecated and should not be used. If sprintf() type of formatting is
       
   106  * desired, consider using QString::arg() or QTextStream.
       
   107  */
       
   108 #if ERROR_TRACES
   121 
   109 
   122     #define ERROR( aErr, aMsg )
   110 inline void NM_ERROR(int err, const QString& msg)
   123     #define ERROR_1( aErr, aMsg, aP1 )
   111 {
   124     #define ERROR_2( aErr, aMsg, aP1, aP2 )
   112     if (err) {
   125     #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )
   113         print_trace("ERROR : " + msg);
   126     #define ERROR_GEN( aMsg )
   114     }
   127     #define ERROR_GEN_1( aMsg, aP1 )
   115 }
       
   116 #define ERROR(err,msg) NM_ERROR(err,msg)
       
   117 #define ERROR_1(err,msg,arg1)\
       
   118 do {\
       
   119     QString __msg;\
       
   120     __msg.sprintf(msg,arg1);\
       
   121     NM_ERROR(err,__msg);\
       
   122 } while (0)
       
   123 #define ERROR_2(err,msg,arg1,arg2)\
       
   124 do {\
       
   125     QString __msg;\
       
   126     __msg.sprintf(msg,arg1,arg2);\
       
   127     NM_ERROR(err,__msg);\
       
   128 } while(0)
       
   129 #define ERROR_3(err,msg,arg1,arg2,arg3)\
       
   130 do {\
       
   131     QString __msg;\
       
   132     __msg.sprintf(msg,arg1,srg2,arg3);\
       
   133     NM_ERROR(err,__msg);\
       
   134 } while(0)
       
   135 #define ERROR_GEN(msg) ERROR(KErrGeneral,msg)
       
   136 #define ERROR_GEN_1(msg,arg1) ERROR_1(KErrGeneral,msg,arg1)
   128 
   137 
   129 #endif//ERROR_TRACE
   138 #else
   130 
   139 
   131 //-----------------------------------------------------------------------------
   140 #define NM_ERROR(err,msg)
   132 // Info trace macros
   141 #define ERROR(err,msg)
   133 //-----------------------------------------------------------------------------
   142 #define ERROR_1(err,msg,arg1)
   134 //
   143 #define ERROR_2(err,msg,arg1,arg2)
   135 #ifdef INFO_TRACE
   144 #define ERROR_3(err,msg,arg1,arg2,arg3)
       
   145 #define ERROR_GEN(msg)
       
   146 #define ERROR_GEN_1(msg,arg1)
   136 
   147 
   137     /**
   148 #endif /* ERROR_TRACES */
   138     * Info log message definitions.
       
   139     */
       
   140     #ifdef TRACE_INTO_FILE
       
   141 
   149 
   142         #define INFO( aMsg )\
   150 /*
   143             {\
   151  * The macro NM_FUNCTION, when used inside a function body, enables tracing
   144             _LIT( KMsg, aMsg );\
   152  * for a function. Trace messages with labels ENTER and RETURN are printed
   145             RFileLogger::Write(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg );\
   153  * when entering into and returning from a function, respectively. In case of
   146             }
   154  * an exception or a Symbian leave, a message with label UNWIND is printed
   147         #define INFO_1( aMsg, aP1 )\
   155  * (UNWIND stands for stack unwinding). The FUNC_LOG macro is provided for
   148             {\
   156  * legacy compatibility. It is deprecated and should not be used.
   149             _LIT( KMsg, aMsg );\
   157  */
   150             RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1 );\
   158 #if FUNCTION_TRACES
   151             }
       
   152         #define INFO_2( aMsg, aP1, aP2 )\
       
   153             {\
       
   154             _LIT( KMsg, aMsg );\
       
   155             RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2 );\
       
   156             }
       
   157         #define INFO_3( aMsg, aP1, aP2, aP3 )\
       
   158             {\
       
   159             _LIT( KMsg, aMsg );\
       
   160             RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2, aP3 );\
       
   161             }
       
   162 
   159 
   163     #else//TRACE_INTO_FILE not defined
   160 class __ftracer
       
   161 {
       
   162 public:
       
   163     __ftracer(const QString& _fn)
       
   164     : fn(_fn)
       
   165     {
       
   166         print_trace("ENTER : " + fn);
       
   167     }
       
   168     ~__ftracer()
       
   169     {
       
   170         if (std::uncaught_exception()) {
       
   171             print_trace("UNWIND : " + fn);
       
   172         } else {
       
   173             print_trace("RETURN : " + fn);
       
   174         }
       
   175     }
       
   176 private:
       
   177     QString fn;
       
   178 };
   164 
   179 
   165         #define INFO( aMsg )\
   180 #define NM_FUNCTION __ftracer __ft(__PRETTY_FUNCTION__);
   166             {\
   181 #define FUNC_LOG NM_FUNCTION
   167             _LIT( KMsg, aMsg ); RDebug::Print( KMsg );\
       
   168             }
       
   169         #define INFO_1( aMsg, aP1 )\
       
   170             {\
       
   171             _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1 );\
       
   172             }
       
   173         #define INFO_2( aMsg, aP1, aP2 )\
       
   174             {\
       
   175             _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2 );\
       
   176             }
       
   177         #define INFO_3( aMsg, aP1, aP2, aP3 )\
       
   178             {\
       
   179             _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2, aP3 );\
       
   180             }
       
   181 
   182 
   182     #endif//TRACE_INTO_FILE
   183 #else
   183 
   184 
   184 #else//INFO_TRACE not defined
   185 #define NM_FUNCTION
       
   186 #define FUNC_LOG
   185 
   187 
   186     #define INFO( aMsg )
   188 #endif /* FUNCTION TRACES */
   187     #define INFO_1( aMsg, aP1 )
       
   188     #define INFO_2( aMsg, aP1, aP2 )
       
   189     #define INFO_3( aMsg, aP1, aP2, aP3 )
       
   190 
   189 
   191 #endif//INFO_TRACE
   190 #endif /* EMAILTRACE_H */
   192 
       
   193 //-----------------------------------------------------------------------------
       
   194 // Function trace macros
       
   195 //-----------------------------------------------------------------------------
       
   196 //
       
   197 #ifdef FUNC_TRACE
       
   198 
       
   199     // Constants
       
   200     _LIT8( KEllipse, "(" );
       
   201 
       
   202     /**
       
   203     * Function logging definitions.
       
   204     */
       
   205     #ifdef TRACE_INTO_FILE
       
   206 
       
   207         #define FUNC( aMsg, aP1 )\
       
   208             {\
       
   209             _LIT8( KMsg, aMsg ); RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1 );\
       
   210             }\
       
   211 
       
   212     #else//TRACE_INTO_FILE not defined
       
   213 
       
   214         #define FUNC( aMsg, aP1 )\
       
   215             {\
       
   216             RDebug::Printf( aMsg, aP1 );\
       
   217             }\
       
   218 
       
   219     #endif//TRACE_INTO_FILE
       
   220 
       
   221     /**
       
   222     * Function trace helper class.
       
   223     */
       
   224     class TFuncLog
       
   225         {
       
   226         public:
       
   227             inline TFuncLog( const char* aFunc ): iFunc( (TUint8*)aFunc )
       
   228                 {
       
   229                 TInt pos = iFunc.Find( KEllipse );
       
   230                 if( pos != KErrNotFound )
       
   231                     {
       
   232                     iFunc.Set( iFunc.Left( iFunc.Find( KEllipse ) ) );
       
   233                     }
       
   234                 #ifdef TRACE_INTO_FILE
       
   235 
       
   236                     //"CMAIL" string is added in the beginning of every trace
       
   237                     //line for filtering purposes
       
   238                     FUNC( "CMAIL %S <", &iFunc );
       
   239 
       
   240                 #else//TRACE_INTO_FILE notdefined
       
   241 
       
   242                     FUNC( "CMAIL %s <", iFunc.Ptr() );
       
   243 
       
   244                 #endif//TRACE_INTO_FILE
       
   245                 }
       
   246             inline ~TFuncLog()
       
   247                 {
       
   248                 #ifdef TRACE_INTO_FILE
       
   249 
       
   250                     FUNC( "CMAIL %S >", &iFunc );
       
   251 
       
   252                 #else//TRACE_INTO_FILE not defined
       
   253 
       
   254                     FUNC( "CMAIL %s >", iFunc.Ptr() );
       
   255 
       
   256                 #endif//TRACE_INTO_FILE
       
   257                 }
       
   258             TPtrC8 iFunc;
       
   259         };
       
   260 
       
   261     #define FUNC_LOG TFuncLog _fl( __PRETTY_FUNCTION__ );
       
   262 
       
   263 #else//FUNC_TRACE not defined
       
   264 
       
   265     #define FUNC_LOG
       
   266 
       
   267 #endif//FUNC_TRACE
       
   268 
       
   269 //-----------------------------------------------------------------------------
       
   270 // Timestamp trace macros
       
   271 //-----------------------------------------------------------------------------
       
   272 //
       
   273 #ifdef TIMESTAMP_TRACE
       
   274 
       
   275     #ifdef TRACE_INTO_FILE
       
   276 
       
   277         #define TIMESTAMP( aCaption )\
       
   278             {\
       
   279             TTime t;\
       
   280             t.HomeTime();\
       
   281             TDateTime dt = t.DateTime();\
       
   282             _LIT( KMsg, aCaption );\
       
   283             _LIT( KFormat, "[TIMESTAMP] %S %d:%02d:%02d.%d us");\
       
   284             RFileLogger::WriteFormat(  KEmailDir,  KEmailTraceFile, EFileLoggingModeAppend, KFormat,\
       
   285                 &KMsg, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond() );\
       
   286             }
       
   287 
       
   288     #else//TRACE_INTO_FILE not defined
       
   289 
       
   290         #define TIMESTAMP( aCaption )\
       
   291             {\
       
   292             TTime t;\
       
   293             t.HomeTime();\
       
   294             TDateTime dt = t.DateTime();\
       
   295             _LIT( KMsg, aCaption );\
       
   296             _LIT( KFormat, "[TIMESTAMP] %S %d:%02d:%02d.%d us");\
       
   297             RDebug::Print( KFormat,\
       
   298                 &KMsg, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond() );\
       
   299             }
       
   300 
       
   301     #endif//TRACE_INTO_FILE
       
   302 
       
   303 #else//TIMESTAMP_TRACE not defined
       
   304 
       
   305     #define TIMESTAMP( aCaption )
       
   306 
       
   307 #endif//TIMESTAMP_TRACE
       
   308 
       
   309 #endif // EMAILTRACE_H