diff -r 2dc6caa42ec3 -r 9ba4404ef423 emailservices/emaildebug/inc/emailtrace.h --- a/emailservices/emaildebug/inc/emailtrace.h Fri May 14 15:41:10 2010 +0300 +++ b/emailservices/emaildebug/inc/emailtrace.h Thu May 27 12:43:55 2010 +0300 @@ -1,309 +1,190 @@ /* -* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). -* All rights reserved. -* This component and the accompanying materials are made available -* under the terms of "Eclipse Public License v1.0" -* which accompanies this distribution, and is available -* at the URL "http://www.eclipse.org/legal/epl-v10.html". -* -* Initial Contributors: -* Nokia Corporation - initial contribution. -* -* Contributors: -* -* Description: Header file describing trace utilities for commonemail -* -*/ + * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). + * All rights reserved. + * This component and the accompanying materials are made available + * under the terms of "Eclipse Public License v1.0" + * which accompanies this distribution, and is available + * at the URL "http://www.eclipse.org/legal/epl-v10.html". + * + * Initial Contributors: + * Nokia Corporation - initial contribution. + * + * Contributors: + * + * Description: + * + */ #ifndef EMAILTRACE_H #define EMAILTRACE_H -#include "emailtraceconfig.hrh" +#include +#include +#include -#ifdef TRACE_INTO_FILE -#include // RFileLogger -#else -#include // RDebug -#endif +/* + * The macros COMMENT_TRACES, ERROR_TRACES, and FUNCTION_TRACES control which + * trace messages are printed. The trace message logging is controlled with + * the LOG_TO_FILE macro, whereas the LOG_FILE macro defines which file is to + * be used in logging. The print_trace() helper function implements printing. + * If LOG_TO_FILE is zero or the LOG_FILE cannot be opened, the messages are + * printed to qDebug(). + */ +#if defined(DEBUG) || defined(_DEBUG) -/** -* Constants -*/ -#ifdef TRACE_INTO_FILE +#define COMMENT_TRACES 1 +#define ERROR_TRACES 1 +#define FUNCTION_TRACES 1 + +#if COMMENT_TRACES || ERROR_TRACES || FUNCTION_TRACES + +#define LOG_TO_FILE 0 +#define LOG_FILE "c:/logs/nmail_trace.log" - _LIT( KEmailDir, "email" ); - _LIT( KEmailTraceFile, "email.txt" ); +inline void print_trace(const QString& msg) +{ + QFile out(LOG_FILE); + if (LOG_TO_FILE && out.open(QIODevice::Append | QIODevice::Text)) { + QDebug(&out) << "[Nmail]" << msg; + out.putChar('\n'); + out.close(); + } else { + qDebug() << "[Nmail]" << msg; + } +} #endif -//----------------------------------------------------------------------------- -// Error trace macros -//----------------------------------------------------------------------------- -// -#ifdef ERROR_TRACE - - /** - * Error trace definitions. Does not automatically log the error code! - */ - #ifdef TRACE_INTO_FILE +#endif /* DEBUG */ - #define ERROR( aErr, aMsg )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::Write( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg );\ - }\ - } - #define ERROR_1( aErr, aMsg, aP1 )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1 );\ - }\ - } - #define ERROR_2( aErr, aMsg, aP1, aP2 )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2 );\ - }\ - } - #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2, aP3 );\ - }\ - } - #define ERROR_GEN( aMsg ) ERROR( KErrGeneral, aMsg ) - #define ERROR_GEN_1( aMsg, aP1 ) ERROR_1( KErrGeneral, aMsg, aP1 ) - - #else//TRACE_INTO_FILE not defined +/* + * The function NM_COMMENT() prints a trace message. The INFO macros are + * provided for legacy compatibility. They are deprecated and should not be + * used. If sprintf() type of formatting is desired, consider using QString:: + * arg() or QTextStream. + */ +#if COMMENT_TRACES - #define ERROR( aErr, aMsg )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg );\ - }\ - } - #define ERROR_1( aErr, aMsg, aP1 )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1 );\ - }\ - } - #define ERROR_2( aErr, aMsg, aP1, aP2 )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2 );\ - }\ - } - #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\ - {\ - if( aErr != KErrNone )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2, aP3 );\ - }\ - } - #define ERROR_GEN( aMsg ) ERROR( KErrGeneral, aMsg ) - #define ERROR_GEN_1( aMsg, aP1 ) ERROR_1( KErrGeneral, aMsg, aP1 ) - - #endif//TRACE_INTO_FILE - -#else//ERROR_TRACE not defined +inline void NM_COMMENT(const QString& msg) +{ + print_trace("COMMENT : " + msg); +} +#define INFO(msg) NM_COMMENT(msg) +#define INFO_1(msg,arg1)\ +do {\ + QString __msg;\ + __msg.sprintf(msg,arg1);\ + NM_COMMENT(__msg);\ +} while (0) +#define INFO_2(msg,arg1,arg2)\ +do {\ + QString __msg;\ + __msg.sprintf(msg,arg1,arg2);\ + NM_COMMENT(__msg);\ +} while (0) +#define INFO_3(msg,arg1,arg2,arg3)\ +do {\ + QString __msg;\ + __msg.sprintf(msg,arg1,arg2,arg3);\ + NM_COMMENT(__msg);\ +} while (0) - #define ERROR( aErr, aMsg ) - #define ERROR_1( aErr, aMsg, aP1 ) - #define ERROR_2( aErr, aMsg, aP1, aP2 ) - #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 ) - #define ERROR_GEN( aMsg ) - #define ERROR_GEN_1( aMsg, aP1 ) - -#endif//ERROR_TRACE +#else -//----------------------------------------------------------------------------- -// Info trace macros -//----------------------------------------------------------------------------- -// -#ifdef INFO_TRACE - - /** - * Info log message definitions. - */ - #ifdef TRACE_INTO_FILE +#define NM_COMMENT(msg) +#define INFO(msg) +#define INFO_1(msg,arg1) +#define INFO_2(msg,arg1,arg2) +#define INFO_3(msg,arg1,arg2,arg3) - #define INFO( aMsg )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::Write( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg );\ - } - #define INFO_1( aMsg, aP1 )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1 );\ - } - #define INFO_2( aMsg, aP1, aP2 )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2 );\ - } - #define INFO_3( aMsg, aP1, aP2, aP3 )\ - {\ - _LIT( KMsg, aMsg );\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1, aP2, aP3 );\ - } +#endif /* COMMENT_TRACES */ - #else//TRACE_INTO_FILE not defined +/* + * The function NM_ERROR() prints its second argument if the first argument + * is non-zero. The ERROR macros are provided for legacy compatibility. They + * are deprecated and should not be used. If sprintf() type of formatting is + * desired, consider using QString::arg() or QTextStream. + */ +#if ERROR_TRACES - #define INFO( aMsg )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg );\ - } - #define INFO_1( aMsg, aP1 )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1 );\ - } - #define INFO_2( aMsg, aP1, aP2 )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2 );\ - } - #define INFO_3( aMsg, aP1, aP2, aP3 )\ - {\ - _LIT( KMsg, aMsg ); RDebug::Print( KMsg, aP1, aP2, aP3 );\ - } - - #endif//TRACE_INTO_FILE - -#else//INFO_TRACE not defined - - #define INFO( aMsg ) - #define INFO_1( aMsg, aP1 ) - #define INFO_2( aMsg, aP1, aP2 ) - #define INFO_3( aMsg, aP1, aP2, aP3 ) - -#endif//INFO_TRACE - -//----------------------------------------------------------------------------- -// Function trace macros -//----------------------------------------------------------------------------- -// -#ifdef FUNC_TRACE +inline void NM_ERROR(int err, const QString& msg) +{ + if (err) { + print_trace("ERROR : " + msg); + } +} +#define ERROR(err,msg) NM_ERROR(err,msg) +#define ERROR_1(err,msg,arg1)\ +do {\ + QString __msg;\ + __msg.sprintf(msg,arg1);\ + NM_ERROR(err,__msg);\ +} while (0) +#define ERROR_2(err,msg,arg1,arg2)\ +do {\ + QString __msg;\ + __msg.sprintf(msg,arg1,arg2);\ + NM_ERROR(err,__msg);\ +} while(0) +#define ERROR_3(err,msg,arg1,arg2,arg3)\ +do {\ + QString __msg;\ + __msg.sprintf(msg,arg1,srg2,arg3);\ + NM_ERROR(err,__msg);\ +} while(0) +#define ERROR_GEN(msg) ERROR(KErrGeneral,msg) +#define ERROR_GEN_1(msg,arg1) ERROR_1(KErrGeneral,msg,arg1) - // Constants - _LIT8( KEllipse, "(" ); - - /** - * Function logging definitions. - */ - #ifdef TRACE_INTO_FILE - - #define FUNC( aMsg, aP1 )\ - {\ - _LIT8( KMsg, aMsg ); RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KMsg, aP1 );\ - }\ - - #else//TRACE_INTO_FILE not defined +#else - #define FUNC( aMsg, aP1 )\ - {\ - RDebug::Printf( aMsg, aP1 );\ - }\ - - #endif//TRACE_INTO_FILE +#define NM_ERROR(err,msg) +#define ERROR(err,msg) +#define ERROR_1(err,msg,arg1) +#define ERROR_2(err,msg,arg1,arg2) +#define ERROR_3(err,msg,arg1,arg2,arg3) +#define ERROR_GEN(msg) +#define ERROR_GEN_1(msg,arg1) - /** - * Function trace helper class. - */ - class TFuncLog - { - public: - inline TFuncLog( const char* aFunc ): iFunc( (TUint8*)aFunc ) - { - TInt pos = iFunc.Find( KEllipse ); - if( pos != KErrNotFound ) - { - iFunc.Set( iFunc.Left( iFunc.Find( KEllipse ) ) ); - } - #ifdef TRACE_INTO_FILE +#endif /* ERROR_TRACES */ - //"CMAIL" string is added in the beginning of every trace - //line for filtering purposes - FUNC( "CMAIL %S <", &iFunc ); - - #else//TRACE_INTO_FILE notdefined - - FUNC( "CMAIL %s <", iFunc.Ptr() ); - - #endif//TRACE_INTO_FILE - } - inline ~TFuncLog() - { - #ifdef TRACE_INTO_FILE - - FUNC( "CMAIL %S >", &iFunc ); - - #else//TRACE_INTO_FILE not defined - - FUNC( "CMAIL %s >", iFunc.Ptr() ); - - #endif//TRACE_INTO_FILE - } - TPtrC8 iFunc; - }; - - #define FUNC_LOG TFuncLog _fl( __PRETTY_FUNCTION__ ); - -#else//FUNC_TRACE not defined - - #define FUNC_LOG - -#endif//FUNC_TRACE +/* + * The macro NM_FUNCTION, when used inside a function body, enables tracing + * for a function. Trace messages with labels ENTER and RETURN are printed + * when entering into and returning from a function, respectively. In case of + * an exception or a Symbian leave, a message with label UNWIND is printed + * (UNWIND stands for stack unwinding). The FUNC_LOG macro is provided for + * legacy compatibility. It is deprecated and should not be used. + */ +#if FUNCTION_TRACES -//----------------------------------------------------------------------------- -// Timestamp trace macros -//----------------------------------------------------------------------------- -// -#ifdef TIMESTAMP_TRACE - - #ifdef TRACE_INTO_FILE - - #define TIMESTAMP( aCaption )\ - {\ - TTime t;\ - t.HomeTime();\ - TDateTime dt = t.DateTime();\ - _LIT( KMsg, aCaption );\ - _LIT( KFormat, "[TIMESTAMP] %S %d:%02d:%02d.%d us");\ - RFileLogger::WriteFormat( KEmailDir, KEmailTraceFile, EFileLoggingModeAppend, KFormat,\ - &KMsg, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond() );\ - } - - #else//TRACE_INTO_FILE not defined +class __ftracer +{ +public: + __ftracer(const QString& _fn) + : fn(_fn) + { + print_trace("ENTER : " + fn); + } + ~__ftracer() + { + if (std::uncaught_exception()) { + print_trace("UNWIND : " + fn); + } else { + print_trace("RETURN : " + fn); + } + } +private: + QString fn; +}; - #define TIMESTAMP( aCaption )\ - {\ - TTime t;\ - t.HomeTime();\ - TDateTime dt = t.DateTime();\ - _LIT( KMsg, aCaption );\ - _LIT( KFormat, "[TIMESTAMP] %S %d:%02d:%02d.%d us");\ - RDebug::Print( KFormat,\ - &KMsg, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond() );\ - } +#define NM_FUNCTION __ftracer __ft(__PRETTY_FUNCTION__); +#define FUNC_LOG NM_FUNCTION + +#else - #endif//TRACE_INTO_FILE - -#else//TIMESTAMP_TRACE not defined +#define NM_FUNCTION +#define FUNC_LOG - #define TIMESTAMP( aCaption ) +#endif /* FUNCTION TRACES */ -#endif//TIMESTAMP_TRACE - -#endif // EMAILTRACE_H +#endif /* EMAILTRACE_H */