bluetooth/btlogger/generic/public/logger.h
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 
       
    22 #ifndef LOGGER_H
       
    23 #define LOGGER_H
       
    24 
       
    25 // Enable __UREL_RDEBUG_PRINT to allow method entry / exit logging to
       
    26 // be output for UREL builds through epocwind.out (for emulator) and
       
    27 // the debugport for hardware.
       
    28 //#define __UREL_RDEBUG_PRINT
       
    29 
       
    30 #include <e32base.h>
       
    31 #include <comms-infras/commsdebugutility.h>
       
    32 #include <bluetooth/logger/components.h> // standard component string definitions
       
    33 
       
    34 // Control function entry and exit logging using a compile-time switch.
       
    35 #ifndef __FLOG_NO_FUNC // Declare this macro in an mmp file to disable function logging
       
    36 #define __LOG_FUNCTIONS__
       
    37 #endif
       
    38 
       
    39 class TFunctionLogger;
       
    40 
       
    41 #ifdef __FLOG_ACTIVE
       
    42 #define IF_FLOGGING(a) a
       
    43 #else
       
    44 #define IF_FLOGGING(a)
       
    45 #endif
       
    46 
       
    47 // Undefine if defined by comms-infras
       
    48 #ifdef LOG
       
    49 #undef LOG
       
    50 #endif
       
    51 
       
    52 // Undefine if defined by comms-infras
       
    53 #ifdef FLOG
       
    54 #undef FLOG
       
    55 #endif
       
    56 
       
    57 _LIT8(KDefaultLogFile, "BT");
       
    58 
       
    59 #ifdef __FLOG_ACTIVE
       
    60 #define CONNECT_LOGGER					static_cast<void>(CBtLog::Connect());
       
    61 #define CLOSE_LOGGER					CBtLog::Close();
       
    62 #define LEAVEIFERRORL(a)				VerboseLeaveIfErrorL(KLogComponent, __FILE__, __LINE__, a)
       
    63 #define LEAVEL(a)						VerboseLeaveL(KLogComponent, __FILE__, __LINE__, a)
       
    64 #define PANIC(CAT, CODE) 				VerbosePanic(KLogComponent, __FILE__, __LINE__, CODE, (TText8*)#CODE, CAT)
       
    65 #define PANIC_MSG(msg, cat, code)		VerboseMsgPanic(KLogComponent, __FILE__, __LINE__, msg, cat, code);
       
    66 #define FLOG(a)							CBtLog::Write(KDefaultLogFile, a);
       
    67 
       
    68 #define FTRACE(a)						{a;}
       
    69 #define LOG(text)						CBtLog::Write(KLogComponent, text);
       
    70 #define LOG1(text, a)					CBtLog::WriteFormat(KLogComponent, text, a);
       
    71 #define LOG2(text, a, b)				CBtLog::WriteFormat(KLogComponent, text, a, b);
       
    72 #define LOG3(text, a, b, c)				CBtLog::WriteFormat(KLogComponent, text, a, b, c);
       
    73 #define LOG4(text, a, b, c, d)			CBtLog::WriteFormat(KLogComponent, text, a, b, c, d);
       
    74 #define LOG5(text, a, b, c, d, e)		CBtLog::WriteFormat(KLogComponent, text, a, b, c, d, e);
       
    75 #define LOG6(text, a, b, c, d, e, f)	CBtLog::WriteFormat(KLogComponent, text, a, b, c, d, e, f);
       
    76 #define LOGHEXDESC(desc)				CBtLog::HexDump(KLogComponent, 0, 0, desc.Ptr() , desc.Length());
       
    77 #define LOGHEXRAW(data, len)			CBtLog::HexDump(KLogComponent, 0, 0, data, len);
       
    78 #define FLOGIFERR(var,text) 			{ if (KErrNone!=var) {CBtLog::Write(KLogComponent, text);}}
       
    79 #define DUMPFRAME(text)					{LOG(text);TInt _lctr = this->Length();if(_lctr){for(TInt _ictr = 0; _ictr < _lctr; ++_ictr){LOG2(_L8("....[%d] = %02x"),_ictr,this->First()->Ptr()[_ictr]);}}}
       
    80 #define LOG_BT(aa, bb)					{TBuf8<20> addr1; addr1.Format(_L8("%02x:%02x:%02x:%02x:%02x:%02x"), bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]); LOG1(aa, &addr1); }
       
    81 #else
       
    82 #define CONNECT_LOGGER
       
    83 #define CLOSE_LOGGER
       
    84 #define LEAVEIFERRORL(a)				static_cast<void>(User::LeaveIfError(a))
       
    85 #define LEAVEL(a)						User::Leave(a)
       
    86 #define PANIC(CAT, CODE) 				User::Panic(CAT, CODE)
       
    87 #define PANIC_MSG(msg, cat, code)		msg.Panic(cat, code);
       
    88 #define FLOG(a)
       
    89 #define FTRACE(a)
       
    90 #define LOG(text)
       
    91 #define LOG1(text, a)
       
    92 #define LOG2(text, a, b)
       
    93 #define LOG3(text, a, b, c)
       
    94 #define LOG4(text, a, b, c, d)
       
    95 #define LOG5(text, a, b, c, d, e)
       
    96 #define LOG6(text, a, b, c, d, e, f)
       
    97 #define LOGHEXDESC(desc)
       
    98 #define LOGHEXRAW(data, len)
       
    99 #define FLOGIFERR(var,text) {var=var;}
       
   100 #define DUMPFRAME(text)
       
   101 #define LOG_BT(aa, bb)
       
   102 #endif // __FLOG_ACTIVE
       
   103 
       
   104 #define FORCED_LOG_FUNC					TFunctionLogger __instrument(KLogComponent, TPtrC8((TUint8*)__PRETTY_FUNCTION__), (TAny*)this);
       
   105 #define FORCED_LOG_STATIC_FUNC			TStaticFunctionLogger __instrument(KLogComponent, TPtrC8((TUint8*)__PRETTY_FUNCTION__));
       
   106 
       
   107 #if ( defined __FLOG_ACTIVE && defined __LOG_FUNCTIONS__ )
       
   108 #define LOG_LINE						CBtLog::Write(KLogComponent, KNullDesC8());
       
   109 #define LOG_FUNC						FORCED_LOG_FUNC
       
   110 #define LOG_STATIC_FUNC					FORCED_LOG_STATIC_FUNC
       
   111 #else
       
   112 #if ( defined __UREL_RDEBUG_PRINT && !defined _DEBUG )
       
   113 #include <e32debug.h>
       
   114 _LIT8(KLogComponent, "RDebugOutput");
       
   115 #define LOG_LINE						RDebug::Printf("%S", &KLogComponent());
       
   116 #define LOG_FUNC						FORCED_LOG_FUNC
       
   117 #define LOG_STATIC_FUNC					FORCED_LOG_STATIC_FUNC
       
   118 #else
       
   119 #define LOG_LINE
       
   120 #define LOG_FUNC
       
   121 #define LOG_STATIC_FUNC
       
   122 #endif
       
   123 #endif
       
   124 
       
   125 inline void __BTDEBUGGER()
       
   126 // needed to call __DEBUGGER inside an __ASSERT
       
   127 	{
       
   128 	__DEBUGGER()
       
   129 	}
       
   130 
       
   131 // Logs the TBTDevAddr x.
       
   132 #define LOGBTDEVADDR(x) 				LOG6(_L8("\t" #x " = 0x%02x%02x%02x%02x%02x%02x"),x[0],x[1],x[2],x[3],x[4],x[5])
       
   133 
       
   134 
       
   135 // For internal programming errors we want a macro which uses a file-specific 
       
   136 // panic category and __LINE__ as the panic code. 
       
   137 // To make this non-standard pattern as helpful to users as possible, we 
       
   138 // append ' line#' to the category. That means we first have to check that the 
       
   139 // category is 10 characters long or less, so that the whole thing is legible 
       
   140 // when it appears to users (panic categories are truncated to 16 characters).
       
   141 
       
   142 // We want a 10-character string (but allow for the NULL terminator).
       
   143 #define PANICCATEGORY(aaa) __ASSERT_COMPILE(sizeof(L##aaa)/2 <= 11); _LIT(KMungedPanicCat, aaa) 
       
   144 
       
   145 // A handy panic-self macro- the category is whatever has been declared with 
       
   146 // BTPANICCATEGORY, with " line#" appended. The panic code is the line number. 
       
   147 #define PANIC_LINENUM \
       
   148 	{ \
       
   149 	_LIT(KLnNo, " line#"); \
       
   150 	TBuf<KMaxExitCategoryName> cat = KMungedPanicCat(); \
       
   151 	cat.Append(KLnNo()); \
       
   152 	PANIC(cat, __LINE__); \
       
   153 	}
       
   154 
       
   155 // A handy assertion macro that panics with a locally-defined panic category 
       
   156 // and the line number.
       
   157 #define ASSERT_ALWAYS(a) \
       
   158 	{ \
       
   159 	if ( !(a) ) \
       
   160 		{ \
       
   161 		PANIC_LINENUM; \
       
   162 		} \
       
   163 	}
       
   164 
       
   165 #ifdef _DEBUG
       
   166 #define ASSERT_DEBUG(a) ASSERT_ALWAYS(a)
       
   167 #define DEBUG_PANIC_LINENUM PANIC_LINENUM
       
   168 #else
       
   169 #define ASSERT_DEBUG(a)
       
   170 #define DEBUG_PANIC_LINENUM
       
   171 #endif // _DEBUG
       
   172 
       
   173 // Undefine the e32def.h-defined ASSERT macro to make sure no-one uses it. 
       
   174 // Use the more informative ASSERT_DEBUG (defined above) or the vanilla 
       
   175 // __ASSERT_DEBUG instead!
       
   176 // #undef ASSERT
       
   177 
       
   178 
       
   179 NONSHARABLE_CLASS(CBtLog) : public CBase
       
   180 	{
       
   181 public:
       
   182 	IMPORT_C static TInt Connect();
       
   183 	IMPORT_C static void Close();
       
   184 	
       
   185 	IMPORT_C static void Write(const TDesC8& aCmpt, const TDesC8& aText);
       
   186 	IMPORT_C static void WriteFormat(const TDesC8& aCmpt, TRefByValue<const TDesC8> aFmt, ...);
       
   187 	IMPORT_C static void WriteFormat(const TDesC8& aCmpt, TRefByValue<const TDesC8> aFmt, VA_LIST& aList);
       
   188 	IMPORT_C static void Write(const TDesC8& aCmpt, const TDesC16& aText);
       
   189 	IMPORT_C static void WriteFormat(const TDesC8& aCmpt, TRefByValue<const TDesC16> aFmt, ...);
       
   190 	IMPORT_C static void WriteFormat(const TDesC8& aCmpt, TRefByValue<const TDesC16> aFmt, VA_LIST& aList);
       
   191 	IMPORT_C static void HexDump(const TDesC8& aCmpt, const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen);
       
   192 	};
       
   193 
       
   194 
       
   195 #ifndef NO_FPRINT
       
   196 inline void FPrint(const TRefByValue<const TDesC> IF_FLOGGING(aFmt), ...)
       
   197 	{
       
   198 #ifdef __FLOG_ACTIVE
       
   199 	VA_LIST list;
       
   200 	VA_START(list,aFmt);
       
   201 	CBtLog::WriteFormat(KDefaultLogFile, aFmt, list);
       
   202 #endif
       
   203 	}
       
   204 #endif
       
   205 
       
   206 
       
   207 #ifndef NO_FHEX_PTR
       
   208 inline void FHex(const TUint8* IF_FLOGGING(aPtr), TInt IF_FLOGGING(aLen))
       
   209 	{
       
   210 #ifdef __FLOG_ACTIVE
       
   211 	CBtLog::HexDump(KDefaultLogFile, 0, 0, aPtr, aLen);
       
   212 #endif
       
   213 	}
       
   214 #endif
       
   215 
       
   216 
       
   217 #ifndef NO_FHEX_DESC
       
   218 inline void FHex(const TDesC8& IF_FLOGGING(aDes))
       
   219 	{
       
   220 #ifdef __FLOG_ACTIVE
       
   221 	FHex(aDes.Ptr(), aDes.Length());
       
   222 #endif
       
   223 	}
       
   224 #endif
       
   225 
       
   226 
       
   227 IMPORT_C void VerboseLeaveIfErrorL(const TDesC8& aCpt, 
       
   228 						  char* aFile, 
       
   229 						  TInt aLine, 
       
   230 						  TInt aReason);
       
   231 
       
   232 IMPORT_C void VerboseLeaveL(const TDesC8& aCpt, 
       
   233 					char* aFile, 
       
   234 					TInt aLine, 
       
   235 					TInt aReason);
       
   236 
       
   237 IMPORT_C void VerboseMsgPanic(const TDesC8& aCpt,
       
   238 								char* aFile,
       
   239 								TInt  aLine,
       
   240 								const RMessage2& aMsg,
       
   241 								const TDesC& aCat,
       
   242 								TInt  aPanicCode);
       
   243 
       
   244 						  
       
   245 IMPORT_C void VerbosePanic(const TDesC8& aCpt, 
       
   246 				  char* aFile, 
       
   247 				  TInt aLine, 
       
   248 				  TInt aPanicCode, 
       
   249 				  TText8* aPanicName,
       
   250 				  const TDesC& aPanicCategory);
       
   251 
       
   252 static const TInt KMaxIndent = 60;
       
   253 
       
   254 NONSHARABLE_CLASS(TLoggerBase)
       
   255 	{
       
   256 protected:
       
   257 	TUint PreIncrementIndent();
       
   258 	TUint PostDecrementIndent();
       
   259 	TUint Indent();
       
   260 
       
   261 protected:
       
   262 	TBuf8<KMaxIndent> iIndentBuf;
       
   263 	TPtrC8 iCpt;
       
   264 	TPtrC8 iString;
       
   265 	};
       
   266 
       
   267 NONSHARABLE_CLASS(TFunctionLogger) : public TLoggerBase
       
   268 	{
       
   269 public:
       
   270 	IMPORT_C TFunctionLogger(const TDesC8& aCpt, const TDesC8& aString, TAny* aThis);
       
   271 	IMPORT_C ~TFunctionLogger();
       
   272 	};
       
   273 
       
   274 NONSHARABLE_CLASS(TStaticFunctionLogger) : public TLoggerBase
       
   275 	{
       
   276 public:
       
   277 	IMPORT_C TStaticFunctionLogger(const TDesC8& aCpt, const TDesC8& aString);
       
   278 	IMPORT_C ~TStaticFunctionLogger();
       
   279 	};
       
   280 
       
   281 #endif	// LOGGER_H
       
   282