coreapplicationuis/ATCmdPlugin/inc/debug.h
changeset 21 c4cbaa4fb734
equal deleted inserted replaced
0:2e3d3ce01487 21:c4cbaa4fb734
       
     1 
       
     2 /*
       
     3  * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4  * All rights reserved.
       
     5  * This component and the accompanying materials are made available
       
     6  * under the terms of "Eclipse Public License v1.0"
       
     7  * which accompanies this distribution, and is available
       
     8  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  * Description :tracing and debuging definitions.
       
    14  *
       
    15  */
       
    16 
       
    17 
       
    18 #ifndef DEBUGTRACE_H
       
    19 #define DEBUGTRACE_H
       
    20 
       
    21 #include <e32base.h>
       
    22 #include <e32svr.h>
       
    23 
       
    24 #ifdef _DEBUG
       
    25 
       
    26 #include <flogger.h>
       
    27 
       
    28 _LIT(KLogFile,"atcmdplugin.txt");
       
    29 _LIT(KLogDir,"atcmdplugin");
       
    30 _LIT(KATDebugFile, "log.txt" );
       
    31 _LIT(KATDebugDir, "atcmdplugin" );
       
    32 
       
    33 _LIT(KTracePrefix16, "[ATCmdPlugin] ");
       
    34 _LIT8(KTracePrefix8, "[ATCmdPlugin] ");
       
    35 _LIT8(KFuncFormat8, ">< %S");
       
    36 _LIT8(KFuncThisFormat8, ">< %S, [0x%08X]");
       
    37 _LIT8(KFuncEntryFormat8, "-> %S");
       
    38 _LIT8(KFuncEntryThisFormat8, "-> %S, [0x%08X]");
       
    39 _LIT8(KFuncEntryArgFormat8, "-> %S, (%S)");
       
    40 _LIT8(KFuncExitFormat8, "<- %S");
       
    41 _LIT(KPanicCategory, "ATCmdPlugin");
       
    42 _LIT8(KPanicPrefix8, "PANIC code ");
       
    43 _LIT8(KLeavePrefix8, "LEAVE code ");
       
    44 
       
    45 const TInt KMaxLogLineLength = 256;
       
    46 
       
    47 // Trace options
       
    48 #define KPRINTERROR		0x00000001 // Print error
       
    49 #define KPRINTINFO	    0x00000002 // Print function trace
       
    50 #define KPRINTSTATE		0x00000004 // Print state machine infos
       
    51 #define KPRINTWARNING  0x00000008 // Print warnings
       
    52 
       
    53 //#define USE_FILE_LOGGING
       
    54 
       
    55 const TInt KTraceMask = KPRINTERROR | KPRINTINFO | KPRINTSTATE | KPRINTWARNING;
       
    56 
       
    57 NONSHARABLE_CLASS(TOverflowTruncate16) : public TDes16Overflow
       
    58 	{
       
    59 public:
       
    60 	void Overflow(TDes16& /*aDes*/) {}
       
    61 	};
       
    62 
       
    63 NONSHARABLE_CLASS(TOverflowTruncate8) : public TDes8Overflow
       
    64 	{
       
    65 public:
       
    66 	void Overflow(TDes8& /*aDes*/) {}
       
    67 	};
       
    68 
       
    69 inline void Trace(TRefByValue<const TDesC16> aFmt, ...)
       
    70     {
       
    71 	VA_LIST list;
       
    72 	VA_START(list,aFmt);
       
    73     RBuf16 buf;
       
    74     buf.Create(KMaxLogLineLength);
       
    75     buf.Zero();    
       
    76 	buf.Append(KTracePrefix16);
       
    77 	TOverflowTruncate16 overflow;
       
    78 	buf.AppendFormatList(aFmt,list,&overflow);
       
    79 	RDebug::Print(buf);
       
    80 #ifdef USE_FILE_LOGGING
       
    81     RFileLogger::Write(KLogDir, KLogFile, EFileLoggingModeAppend, buf);
       
    82 #endif
       
    83 	buf.Close();
       
    84     }
       
    85 
       
    86 inline void Trace(TRefByValue<const TDesC8> aFmt, ...)
       
    87     {
       
    88     VA_LIST list;
       
    89     VA_START(list, aFmt);
       
    90 	TOverflowTruncate8 overflow;
       
    91     RBuf8 buf8;
       
    92     buf8.Create(KMaxLogLineLength);
       
    93     buf8.Zero();    
       
    94     buf8.Append(KTracePrefix8);
       
    95 	buf8.AppendFormatList(aFmt, list, &overflow);
       
    96     RDebug::RawPrint(buf8);
       
    97 #ifdef USE_FILE_LOGGING
       
    98     RFileLogger::Write(KLogDir, KLogFile, EFileLoggingModeAppend, buf8);
       
    99 #endif
       
   100     buf8.Close();
       
   101     }
       
   102 
       
   103 inline void TracePanic(
       
   104 	char* aFile, 
       
   105 	TInt aLine,
       
   106 	TInt aPanicCode,
       
   107 	const TDesC& aPanicCategory)
       
   108     {
       
   109 	TPtrC8 fullFileName((const TUint8*)aFile);
       
   110 	TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1);
       
   111     RBuf8 buf;
       
   112     buf.Create(KMaxLogLineLength);
       
   113     buf.Zero();    
       
   114 	buf.Append(KPanicPrefix8);
       
   115 	buf.AppendFormat(_L8("%d at line %d in file %S"), aPanicCode, aLine, &fileName);
       
   116 	Trace(buf);
       
   117 	buf.Close();
       
   118 	User::Panic(aPanicCategory, aPanicCode); 
       
   119     }
       
   120 
       
   121 inline void TraceLeave(char* aFile, TInt aLine, TInt aReason)
       
   122 	{
       
   123 	TPtrC8 fullFileName((const TUint8*)aFile);
       
   124 	TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1);
       
   125     RBuf8 buf;
       
   126     buf.Create(KMaxLogLineLength);
       
   127     buf.Zero();    
       
   128 	buf.Append(KLeavePrefix8);
       
   129 	buf.AppendFormat(_L8("%d at line %d in file %S"), aReason, aLine, &fileName);
       
   130 	Trace(buf);
       
   131 	buf.Close();
       
   132 	User::LeaveIfError(aReason);
       
   133 	}
       
   134 
       
   135 inline TBuf8<64> ArgFormat(TRefByValue<const TDesC8> aFmt, ...)
       
   136     {
       
   137     TOverflowTruncate8 overflow;
       
   138     TBuf8<64> buf8;
       
   139     buf8.Format(aFmt, &overflow);
       
   140     return buf8;
       
   141     }
       
   142 
       
   143 #define TRACE_INFO(p) {if(KTraceMask & KPRINTINFO) Trace p;}
       
   144 
       
   145 #define TRACE_ERROR(p) {if(KTraceMask & KPRINTERROR) Trace p;}
       
   146 
       
   147 #define TRACE_STATE(p) {if(KTraceMask & KPRINTSTATE) Trace p;}
       
   148 
       
   149 #define TRACE_WARNING(p) {if(KTraceMask & KPRINTWARNING) Trace p;}
       
   150 
       
   151 #define TRACE_INFO_SEG(p) {if(KTraceMask & KPRINTINFO) p;}
       
   152 
       
   153 #define TRACE_ASSERT(GUARD, CODE) {if (!(GUARD)) \
       
   154          TracePanic(__FILE__, __LINE__, CODE, KPanicCategory);}
       
   155 
       
   156 #define PANIC(CODE) TracePanic(__FILE__, __LINE__, CODE, KPanicCategory)
       
   157 
       
   158 #define LEAVE_IF_ERROR(REASON) {if (REASON) \
       
   159          TraceLeave(__FILE__, __LINE__, REASON);}
       
   160 
       
   161 #define LEAVE(REASON) TraceLeave(__FILE__, __LINE__, REASON)
       
   162 
       
   163 #define TRACE_FUNC_ENTRY {if(KTraceMask & KPRINTINFO) { \
       
   164                             TPtrC8 ptr8((TUint8*)__PRETTY_FUNCTION__); \
       
   165                             Trace(KFuncEntryFormat8, &ptr8);}}
       
   166 
       
   167 #define TRACE_FUNC_ENTRY_THIS {if(KTraceMask & KPRINTINFO) {\
       
   168                             TPtrC8 ptr8((TUint8*)__PRETTY_FUNCTION__); \
       
   169                             Trace(KFuncEntryThisFormat8, &ptr8, this);}}
       
   170 
       
   171 #define	 TRACE_FUNC_EXIT {if(KTraceMask & KPRINTINFO) {\
       
   172                             TPtrC8 ptr8((TUint8*)__PRETTY_FUNCTION__); \
       
   173                             Trace(KFuncExitFormat8, &ptr8);}}
       
   174 
       
   175 #define TRACE_FUNC {if(KTraceMask & KPRINTINFO) { \
       
   176                             TPtrC8 ptr8((TUint8*)__PRETTY_FUNCTION__); \
       
   177                             Trace(KFuncFormat8, &ptr8);}}
       
   178 
       
   179 #define TRACE_FUNC_THIS {if(KTraceMask & KPRINTINFO) {\
       
   180                             TPtrC8 ptr8((TUint8*)__PRETTY_FUNCTION__); \
       
   181                             Trace(KFuncThisFormat8, &ptr8, this);}}
       
   182 
       
   183 #define TRACE_FUNC_ARG(a) { if(KTraceMask & KPRINTINFO) { \
       
   184                             TPtrC8 func( (TUint8*) __PRETTY_FUNCTION__ ); \
       
   185                             TPtrC8 arg( ArgFormat a ); \
       
   186                             Trace( KFuncEntryArgFormat8, &func, &arg ); } }
       
   187 
       
   188 #define RETURN_IF_ERR(ERR) {if(ERR) {Trace(_L8(" RETURN %d at file %S line %d"), ERR, &(TPtrC8((const TUint8*)__FILE__)), __LINE__); return ERR;}}
       
   189 
       
   190 #else // ! _DEBUG
       
   191 
       
   192 #define TRACE_INFO(p)
       
   193 
       
   194 #define TRACE_ERROR(p)
       
   195 
       
   196 #define TRACE_STATE(p)
       
   197 
       
   198 #define TRACE_WARNING(p)
       
   199 
       
   200 #define TRACE_INFO_SEG(p)
       
   201 
       
   202 #define TRACE_ASSERT(GUARD, CODE)
       
   203 
       
   204 #define PANIC(CODE) {User::Panic(KPanicCategory, CODE);}
       
   205 
       
   206 #define LEAVE_IF_ERROR(REASON) {static_cast<void>(User::LeaveIfError(REASON));}
       
   207 
       
   208 #define LEAVE(REASON) {static_cast<void>(User::Leave(REASON));}
       
   209 
       
   210 #define TRACE_FUNC_ENTRY
       
   211 
       
   212 #define TRACE_FUNC_ENTRY_THIS
       
   213 
       
   214 #define TRACE_FUNC_EXIT
       
   215 
       
   216 #define TRACE_FUNC
       
   217 
       
   218 #define TRACE_FUNC_THIS
       
   219 
       
   220 #define TRACE_FUNC_ARG(a)
       
   221 
       
   222 #define RETURN_IF_ERR(ERR) {if(ERR) return ERR;}
       
   223 #endif // _DEBUG
       
   224 
       
   225 #endif // DEBUGTRACE_H
       
   226 
       
   227 // End of File