obex/obexprotocol/obextransport/src/logengine.cpp
changeset 57 f6055a57ae18
parent 0 d0791faffa3f
equal deleted inserted replaced
54:4dc88a4ac6f4 57:f6055a57ae18
       
     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 #include <e32base.h>
       
    23 #include <comms-infras/commsdebugutility.h>
       
    24 #include "logger.h"
       
    25 
       
    26 
       
    27 #ifdef __FLOG_ACTIVE
       
    28 _LIT8(KSubsystem, "obex");
       
    29 _LIT8(KLogCmpt, "logengine");
       
    30 #endif
       
    31 
       
    32 
       
    33 NONSHARABLE_CLASS(TLogData)
       
    34 	{
       
    35 	public:
       
    36 #ifdef __FLOG_ACTIVE
       
    37 		TLogData();
       
    38 		
       
    39 		void SetLogTags(const TDesC8& aCmpt);
       
    40 
       
    41 		TInt iAccessCount;
       
    42 
       
    43 		RFileLogger iLogEngine;
       
    44 		TBuf8<KMaxTagLength> iCurrentComponent;
       
    45 #endif
       
    46 	};
       
    47 
       
    48 
       
    49 #ifdef __FLOG_ACTIVE
       
    50 TLogData::TLogData()
       
    51 	: iAccessCount(0), iCurrentComponent(KNullDesC8)
       
    52 	{}
       
    53 
       
    54 void TLogData::SetLogTags(const TDesC8& aCmpt)
       
    55 	{
       
    56 	if (aCmpt != iCurrentComponent)
       
    57 		{
       
    58 		iLogEngine.SetLogTags(KSubsystem, aCmpt.Left(KMaxTagLength));
       
    59 		iCurrentComponent = aCmpt.Left(KMaxTagLength);
       
    60 		}
       
    61 	}
       
    62 #endif
       
    63 
       
    64 #define GETLOG TLogData* __logger = static_cast<TLogData*>(Dll::Tls());
       
    65 
       
    66 
       
    67 
       
    68 EXPORT_C /*static*/ TInt CObexLog::Connect()
       
    69 	{
       
    70 #ifdef __FLOG_ACTIVE
       
    71 	GETLOG;
       
    72 	
       
    73 	if (!__logger)
       
    74 		{
       
    75 		CObexLog::Write(KLogCmpt, _L8("Opening new logger connection"));
       
    76 		__logger = new TLogData();
       
    77 		if (!__logger)
       
    78 			{
       
    79 			CObexLog::Write(KLogCmpt, _L8("Opening logger connection failed, no memory"));
       
    80 			return KErrNoMemory;
       
    81 			}
       
    82 		
       
    83 		__logger->iLogEngine.Connect();
       
    84 		Dll::SetTls(__logger);
       
    85 		}
       
    86 	
       
    87 	__logger->iAccessCount++;
       
    88 	CObexLog::WriteFormat(KLogCmpt, _L8("Opening -- %d instances now open"), __logger->iAccessCount);
       
    89 		
       
    90 	return KErrNone;
       
    91 #else
       
    92 	return KErrNotSupported;
       
    93 #endif
       
    94 	}
       
    95 
       
    96 
       
    97 EXPORT_C /*static*/ void CObexLog::Close()
       
    98 	{
       
    99 #ifdef __FLOG_ACTIVE
       
   100 	GETLOG;
       
   101 	
       
   102 	if (__logger)
       
   103 		{
       
   104 		TInt& count = __logger->iAccessCount;
       
   105 		
       
   106 		if (count)
       
   107 			{
       
   108 			count--;
       
   109 			CObexLog::WriteFormat(KLogCmpt, _L8("Closing -- %d instance(s) left open"), count);
       
   110 			if (!count)
       
   111 				{
       
   112 				__logger->iLogEngine.Close();
       
   113 				delete __logger;
       
   114 				Dll::SetTls(NULL);
       
   115 				CObexLog::Write(KLogCmpt, _L8("Fully closed and deleted, now flogging statically."));
       
   116 				}
       
   117 			}
       
   118 		else
       
   119 			{
       
   120 			CObexLog::Write(KLogCmpt, _L8("Not closing -- not opened"));
       
   121 			}
       
   122 		}
       
   123 #endif
       
   124 	}
       
   125 
       
   126 
       
   127 
       
   128 EXPORT_C /*static*/ void CObexLog::Write(const TDesC8& IF_FLOGGING(aCmpt), const TDesC8& IF_FLOGGING(aText))
       
   129 	{
       
   130 #ifdef __FLOG_ACTIVE
       
   131 	GETLOG;
       
   132 	
       
   133 	if (__logger)
       
   134 		{
       
   135 		__logger->SetLogTags(aCmpt);
       
   136 		__logger->iLogEngine.Write(aText);
       
   137 		}
       
   138 	else
       
   139 		{
       
   140 		RFileLogger::Write(KSubsystem, aCmpt, aText);
       
   141 		}
       
   142 #endif
       
   143 	}
       
   144 
       
   145 
       
   146 EXPORT_C /*static*/ void CObexLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC8> IF_FLOGGING(aFmt), ...)
       
   147 	{
       
   148 #ifdef __FLOG_ACTIVE
       
   149 	VA_LIST list;
       
   150 	VA_START(list, aFmt);
       
   151 
       
   152 	GETLOG;
       
   153 	
       
   154 	if (__logger)
       
   155 		{
       
   156 		__logger->SetLogTags(aCmpt);
       
   157 		__logger->iLogEngine.WriteFormat(aFmt, list);
       
   158 		}
       
   159 	else
       
   160 		{
       
   161 		RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, list);
       
   162 		}
       
   163 #endif
       
   164 	}
       
   165 
       
   166 
       
   167 EXPORT_C /*static*/ void CObexLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC8> IF_FLOGGING(aFmt), VA_LIST& IF_FLOGGING(aList))
       
   168 	{
       
   169 #ifdef __FLOG_ACTIVE
       
   170 	GETLOG;
       
   171 	
       
   172 	if (__logger)
       
   173 		{
       
   174 		__logger->SetLogTags(aCmpt);
       
   175 		__logger->iLogEngine.WriteFormat(aFmt, aList);
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, aList);
       
   180 		}
       
   181 #endif
       
   182 	}
       
   183 
       
   184 
       
   185 EXPORT_C /*static*/ void CObexLog::Write(const TDesC8& IF_FLOGGING(aCmpt), const TDesC16& IF_FLOGGING(aText))
       
   186 	{
       
   187 #ifdef __FLOG_ACTIVE
       
   188 	GETLOG;
       
   189 	
       
   190 	if (__logger)
       
   191 		{
       
   192 		__logger->SetLogTags(aCmpt);
       
   193 		__logger->iLogEngine.Write(aText);
       
   194 		}
       
   195 	else
       
   196 		{
       
   197 		RFileLogger::WriteFormat(KSubsystem, aCmpt, aText);
       
   198 		}
       
   199 #endif
       
   200 	}
       
   201 
       
   202 
       
   203 EXPORT_C /*static*/ void CObexLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC16> IF_FLOGGING(aFmt), ...)
       
   204 	{
       
   205 #ifdef __FLOG_ACTIVE
       
   206 	VA_LIST list;
       
   207 	VA_START(list, aFmt);
       
   208 
       
   209 	GETLOG;
       
   210 	
       
   211 	if (__logger)
       
   212 		{
       
   213 		__logger->SetLogTags(aCmpt);
       
   214 		__logger->iLogEngine.WriteFormat(aFmt, list);
       
   215 		}
       
   216 	else
       
   217 		{
       
   218 		RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, list);
       
   219 		}
       
   220 #endif
       
   221 	}
       
   222 
       
   223 
       
   224 EXPORT_C /*static*/ void CObexLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC16> IF_FLOGGING(aFmt), VA_LIST& IF_FLOGGING(aList))
       
   225 	{
       
   226 #ifdef __FLOG_ACTIVE
       
   227 	GETLOG;
       
   228 	
       
   229 	if (__logger)
       
   230 		{
       
   231 		__logger->SetLogTags(aCmpt);
       
   232 		__logger->iLogEngine.WriteFormat(aFmt, aList);
       
   233 		}
       
   234 	else
       
   235 		{
       
   236 		RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, aList);
       
   237 		}
       
   238 #endif
       
   239 	}
       
   240 
       
   241 
       
   242 EXPORT_C /*static*/ void CObexLog::HexDump(const TDesC8& IF_FLOGGING(aCmpt), const TText* IF_FLOGGING(aHeader), const TText* IF_FLOGGING(aMargin), const TUint8* IF_FLOGGING(aPtr), TInt IF_FLOGGING(aLen))
       
   243 	{
       
   244 #ifdef __FLOG_ACTIVE
       
   245 	GETLOG;
       
   246 	
       
   247 	if (__logger)
       
   248 		{
       
   249 		__logger->SetLogTags(aCmpt);
       
   250 		__logger->iLogEngine.HexDump(aHeader, aMargin, aPtr, aLen);
       
   251 		}
       
   252 	else
       
   253 		{
       
   254 		RFileLogger::HexDump(KSubsystem, aCmpt, TPtrC8(aPtr, aLen), KNullDesC8);
       
   255 		}
       
   256 #endif
       
   257 	}
       
   258 
       
   259 
       
   260 
       
   261 
       
   262 
       
   263 
       
   264 
       
   265 /**
       
   266 Leave (if error) verbosely- log name of file and line number just before 
       
   267 leaving.
       
   268 @param aFile The file we're leaving from.
       
   269 @param aLine The line number we're leaving from.
       
   270 @param aReason The leave code.
       
   271 */
       
   272 EXPORT_C void VerboseLeaveIfErrorL(const TDesC8& IF_FLOGGING(aCpt), 
       
   273 						  char* IF_FLOGGING(aFile), 
       
   274 						  TInt IF_FLOGGING(aLine), 
       
   275 						  TInt IF_FLOGGING(aReason))
       
   276 	{
       
   277 #ifdef __FLOG_ACTIVE
       
   278 	// only leave if an error value
       
   279 	if ( aReason >= KErrNone )
       
   280 		{
       
   281 		return;
       
   282 		}
       
   283 
       
   284 	_LIT8(KLeavePrefix, "LEAVE: ");
       
   285 
       
   286 	TPtrC8 fullFileName((const TUint8*)aFile);
       
   287 	TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1);
       
   288 
       
   289 	TBuf8<256> buf;
       
   290 	buf.Append(KLeavePrefix);
       
   291 	buf.AppendFormat(_L8("aReason = %d [file %S, line %d]"), aReason, &fileName, 
       
   292 		aLine);
       
   293 	CObexLog::Write(aCpt, buf);
       
   294 
       
   295 	// finally
       
   296 	User::Leave(aReason);
       
   297 #endif
       
   298 	}
       
   299 
       
   300 /**
       
   301 Panic verbosely- log name of file and line number just before panicking.
       
   302 @param aFile The file that's panicking.
       
   303 @param aLine The line number that's panicking.
       
   304 @param aReason The panic code.
       
   305 @param aPanicName The text of the panic code.
       
   306 @param aPanicCategory The panic category.
       
   307 */
       
   308 EXPORT_C void VerbosePanic(const TDesC8& IF_FLOGGING(aCpt), 
       
   309 				  char* IF_FLOGGING(aFile), 
       
   310 				  TInt IF_FLOGGING(aLine), 
       
   311 				  TInt IF_FLOGGING(aPanicCode), 
       
   312 				  TText8* IF_FLOGGING(aPanicName),
       
   313 				  const TDesC& IF_FLOGGING(aPanicCategory))
       
   314 	{
       
   315 #ifdef __FLOG_ACTIVE
       
   316 	_LIT8(KPanicPrefix, "PANIC: code ");
       
   317 	
       
   318 	TPtrC8 fullFileName((const TUint8*)aFile);
       
   319 	TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1);
       
   320 
       
   321 	TBuf8<256> buf;
       
   322 	buf.Append(KPanicPrefix);
       
   323 	buf.AppendFormat(_L8("%d = %s [file %S, line %d]"), 
       
   324 		aPanicCode, 
       
   325 		aPanicName, 
       
   326 		&fileName, 
       
   327 		aLine);
       
   328 	CObexLog::Write(aCpt, buf);
       
   329 
       
   330 	// finally
       
   331 	User::Panic(aPanicCategory, aPanicCode);
       
   332 #endif
       
   333 	}
       
   334 
       
   335 #ifdef __FLOG_ACTIVE
       
   336 _LIT8(KInstrumentIn, ">>%S this = [0x%08x]");
       
   337 _LIT8(KInstrumentOut, "<<%S");
       
   338 #endif
       
   339 
       
   340 EXPORT_C TFunctionLogger::TFunctionLogger(const TDesC8& IF_FLOGGING(aCpt), const TDesC8& IF_FLOGGING(aString), TAny* IF_FLOGGING(aThis))
       
   341 	{
       
   342 #ifdef __FLOG_ACTIVE
       
   343 	iCpt.Set(aCpt);
       
   344 	iString.Set(aString);
       
   345 	CObexLog::WriteFormat(iCpt, KInstrumentIn, &iString, aThis);
       
   346 #endif
       
   347 	}
       
   348 
       
   349 EXPORT_C TFunctionLogger::~TFunctionLogger()
       
   350 	{
       
   351 #ifdef __FLOG_ACTIVE
       
   352 	CObexLog::WriteFormat(iCpt, KInstrumentOut, &iString);
       
   353 #endif
       
   354 	}
       
   355