persistentstorage/sql/SRC/Common/SqlUtil.cpp
branchRCL_3
changeset 23 26645d81f48d
parent 12 6b6fd149daa2
child 24 cc28652e0254
--- a/persistentstorage/sql/SRC/Common/SqlUtil.cpp	Thu Aug 19 11:36:21 2010 +0300
+++ b/persistentstorage/sql/SRC/Common/SqlUtil.cpp	Tue Aug 31 16:57:14 2010 +0300
@@ -14,18 +14,101 @@
 //
 
 #include <e32svr.h>
+#include "SqlAssert.h"
 #include <sqldb.h>		//ESqlAtRow, ESqlAtEnd, ESqlErrGeneral
-#include "SqlUtil.h"
-#include "SqlPanic.h"	//SqlPanic(), TSqlPanic
 #include "sqlite3.h"	//SQLITE_OK, SQLITE_ROW, SQLITE_DONE
-#include "UTraceSql.h"
+#include "OstTraceDefinitions.h"
+#ifdef OST_TRACE_COMPILER_IN_USE
+#include "SqlUtilTraces.h"
+#endif
+#include "SqlTraceDef.h"
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+const TInt KSqlLeavePanic = -359;//The (last-1) error code from the reserved area for the SQL component.
+
+#define UNUSED_ARG(arg) arg = arg
+#define UNUSED_DES(arg) arg
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#if defined OST_TRACE_COMPILER_IN_USE &&  defined _SQL_RDEBUG_PRINT
+
+/**
+This class has been added here to avoid the crashes when _SQL_RDEBUG_PRINT macro is defined but the
+data to be printed out is too big and cannot fit into the buffer with size KSqlMaxPrnStrLen.
+@internalComponent   
+*/
+class TSqlDes16Overflow : public TDes16Overflow
+    {
+public:    
+    virtual void Overflow(TDes16& /*aDes*/)
+        {
+        }
+    };
+
+//Replaces:
+// 1) "%lld" with "%ld"
+// 2) "%s" with "%S"
+//These are the differences in format specification between RDebig::Print and OST functions.
+//The new format spec length should be less or equal than the old format spec length.
+static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
+	{
+	TInt fmtLength = aFormat.Length();
+	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
+    TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
+    TInt pos;
+    while((pos = ptr.Find(aFmtSpec)) >= 0)
+    	{
+		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
+		fmtLength -= KDiff;
+		ptr.Set(ptr.MidTPtr(pos));
+    	}
+    aFormat.SetLength(fmtLength);
+	}
+
+void SqlPrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
+    {
+    VA_LIST list;
+    VA_START(list, aFormat);
+    TBuf<128> format;
+    _LIT(KTraceIdent, "SQL;");
+    format.Copy(TPtrC8((const TUint8*)aFormat));
+    format.Insert(0, KTraceIdent);
+    format.Append(_L("\r\n"));
+    _LIT(KOstI64Fmt, "%lld");
+    _LIT(KDbgPrnI64Fmt, "%ld");
+    ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
+    _LIT(KOstDes8Fmt, "%s");
+    _LIT(KDbgPrnDesFmt, "%S");
+    ReplaceFmtSpec(format, KOstDes8Fmt, KDbgPrnDesFmt);
+    TBuf<KSqlMaxPrnStrLen> buf;
+    TSqlDes16Overflow overflowHandler;
+    buf.AppendFormatList(format, list, &overflowHandler);
+#ifdef _SQL_RDEBUG_PRINT    
+    RDebug::RawPrint(buf);
+#endif
+    }
+
+const TDesC* SqlDes8to16Ptr(const TDesC8& aDes, TDes& aOut)
+	{
+	TPtrC8 ptr(aDes.Ptr(), Min(aDes.Length(), aOut.MaxLength()));
+	aOut.Copy(ptr);
+	return &aOut;
+	}
+
+#endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQL_RDEBUG_PRINT 
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 /**
 SQL panic category.
 
 @internalComponent
 */
-_LIT(KPanicCategory,"SqlDb");
+_LIT(KPanicCategory, "SqlDb");
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 /**
 Panics the caller with aPanicCode panic code.
@@ -35,11 +118,13 @@
 
 @internalComponent
 */
-void SqlPanic(TSqlPanic aPanicCode)
+static void SqlPanic(TSqlPanic aPanicCode)
 	{
 	User::Panic(KPanicCategory, aPanicCode);
 	}
 	
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
 /**
 Panics the client with aPanicCode panic code.
 This function is used by the SQL server to panic the caller (the client).
@@ -53,13 +138,158 @@
 
 @internalComponent
 */
-TInt SqlPanicClientL(const RMessage2& aMessage, TSqlPanic aPanicCode)
+static TInt SqlPanicClientL(const RMessage2& aMessage, TSqlPanic aPanicCode)
 	{
 	aMessage.Panic(KPanicCategory, aPanicCode);
-	__SQLLEAVE(KSqlLeavePanic);
+	__SQLLEAVE2(KSqlLeavePanic);
 	return KErrNone;
-	}
-	
+	}	
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+The function prints out a "SQL panic" message to the console and panics the thread where it is called from.
+It gives a useful information about the found error together with the source file name and line number where
+it occurred.
+
+Note: this function  will output information regarding the panic only if _SQL_PANIC_TRACE_ENABLED macro is defined  
+
+@param aFile Source file name
+@param aLine Source line number
+@param aPanicCode Panic code
+@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
+
+@return KErrNone
+
+@internalComponent
+*/  
+TInt TSqlUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
+    {
+#if defined OST_TRACE_COMPILER_IN_USE && defined _SQL_PANIC_TRACE_ENABLED
+    TPtrC fname(FileName(aFile));
+    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLPRNSTR(fname), aLine, __SQLPRNSTR(KPanicCategory), aPanicCode);
+#else
+    UNUSED_ARG(aFile);
+    UNUSED_ARG(aLine);
+    UNUSED_ARG(aHandle);
+#endif      
+    ::SqlPanic(static_cast <TSqlPanic> (aPanicCode));
+    return KErrNone;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+The function prints out a "SQL leave" message to the console and leaves with aError error code.
+It gives a usefull information about the found error together with the source file name and line number where
+it occured.
+
+Note: this function  will output information regarding the panic only if _SQL_LEAVE_TRACE_ENABLED macro is defined  
+
+@param aFile Source file name
+@param aLine Source line number
+@param aError Error code
+@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
+
+@internalComponent
+*/  
+void TSqlUtil::Leave(const TText* aFile, TInt aLine, TInt aError, TUint aHandle)
+    {
+#if defined OST_TRACE_COMPILER_IN_USE && defined _SQL_LEAVE_TRACE_ENABLED     
+    TPtrC fname(FileName(aFile));
+    OstTraceExt4(TRACE_ERROR, TSQLUTIL_LEAVE, "Leave;0x%X;%S;%d;Error=%d", aHandle, __SQLPRNSTR(fname), aLine, aError);
+#else
+    UNUSED_ARG(aFile);
+    UNUSED_ARG(aLine);
+    UNUSED_ARG(aHandle);
+#endif
+    User::Leave(aError);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+The function prints out a "SQL leave" message to the console and leaves with aError error code, if it is 
+negative.
+It gives a usefull information about the found error together with the source file name and line number where
+it occured.
+
+Note: this function  will output information regarding the panic only if _SQL_LEAVE_TRACE_ENABLED macro is defined  
+
+@param aFile Source file name
+@param aLine Source line number
+@param aError Error code
+
+@internalComponent
+*/  
+TInt TSqlUtil::LeaveIfError(const TText* aFile, TInt aLine, TInt aError, TUint aHandle)
+    {
+    if(aError < 0)
+        {
+        TSqlUtil::Leave(aFile, aLine, aError, aHandle);
+        }
+    return aError;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+The function prints out a "SQL leave" message to the console and leaves with KErrNoMemory if 
+aPtr parameter is NULL.
+
+Note: this function  will output information regarding the panic only if _SQL_LEAVE_TRACE_ENABLED macro is defined  
+
+@param aFile Source file name
+@param aLine Source line number
+@param aPtr The pointer to be tested against NULL value.
+
+@internalComponent
+*/  
+void* TSqlUtil::LeaveIfNull(const TText* aFile, TInt aLine, void* aPtr, TUint aHandle)
+    {
+    if(!aPtr)
+        {
+        TSqlUtil::Leave(aFile, aLine, KErrNoMemory, aHandle);
+        }
+    return aPtr;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+The function is used by the SQL server.
+It prints out a "SQL panic" message to the console and panic the client.
+It gives a usefull information about the found error together with the source file name and line number where
+it occured.
+
+Note: this function  will output information regarding the panic only if _SQL_PANIC_TRACE_ENABLED macro is defined  
+
+@param aFile Source file name
+@param aLine Source line number
+@param aMessage The client message, which processing caused the panic.
+@param aPanicCode Error code
+
+@leave KSqlLeavePanic
+
+@return KErrNone;
+
+@internalComponent
+*/  
+TInt TSqlUtil::PanicClientL(const TText* aFile, TInt aLine, const RMessage2& aMessage, TInt aPanicCode, TUint aHandle)
+    {
+#if defined OST_TRACE_COMPILER_IN_USE && defined  _SQL_PANIC_TRACE_ENABLED
+    TPtrC fname(FileName(aFile));
+    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANICCLIENTL, "Panic;%X;%S;%d;%S;%d", aHandle, __SQLPRNSTR(fname), aLine, __SQLPRNSTR(KPanicCategory), aPanicCode);
+#else
+    UNUSED_ARG(aFile);
+    UNUSED_ARG(aLine);
+    UNUSED_ARG(aHandle);
+#endif      
+    return ::SqlPanicClientL(aMessage, static_cast <TSqlPanic> (aPanicCode));
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
 /**
 Processes SQL database error code and OS error code and returns unified error code.
 If aSqlError == SQLITE_ROW then the function returns KSqlAtRow.
@@ -80,7 +310,8 @@
 */
 TInt Sql2OsErrCode(TInt aSqlError, TInt aOsError)
 	{
-	__SQLASSERT(aSqlError >= SQLITE_OK && aOsError <= KErrNone, ESqlPanicBadArgument);
+
+	__ASSERT_DEBUG(aSqlError >= SQLITE_OK && aOsError <= KErrNone, __SQLPANIC2(ESqlPanicBadArgument));
 	TInt err = KErrNone;
 	if(aOsError == KErrDiskFull)
 		{//Whatever is the aSqlError value, even SQLITE_OK, never ignore KErrDiskFull errors
@@ -114,243 +345,19 @@
 	return err;
 	}
 
-////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////   class Util   ////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////
-#if defined _LOGGING || defined SYMBIAN_TRACE_SQL_ERR
-
-/**
-This function is used to log the message "msg" containing the "err" error code.
-The message "msg" should contain the format specifier %d.
-
-The function is used when _LOGGING or SYMBIAN_TRACE_SQL_ERR is defined.
-
-@param aMsg Error message
-@param aErr Error code
-
-@internalComponent
-*/	
-void Util::ErrorPrint(const TDesC& aMsg, TInt aErr)
-	{
-	SYMBIAN_TRACE_SQL_ERR_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EError), aMsg, aErr));
-	RDebug::Print(aMsg, aErr);
-	}
-
-/**
-This macro should be used to log the message "msg" containing the "str" string.
-The message "msg" should contain the format specifier %S.
-
-The function is used when _LOGGING or SYMBIAN_TRACE_SQL_ERR is defined.
-
-@param aMsg Error message
-@param aErr Error code
-
-@internalComponent
-*/	
-void Util::ErrorPrint(const TDesC& aMsg, const TDesC& aStr)
-	{
-	SYMBIAN_TRACE_SQL_ERR_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EError), aMsg, &aStr));
-	RDebug::Print(aMsg, &aStr);
-	}
-
-#endif  //_LOGGING || SYMBIAN_TRACE_SQL_ERR
-
-#if defined _ASSERTIONS
-
-/**
-The function prints out a "SQL panic" message to the console and panics the thread where it is called from.
-It gives a useful information about the found error together with the source file name and line number where
-it occurred.
-
-The function is used when _ASSERTIONS is defined.
-
-@param aFile Source file name
-@param aLine Source line number
-@param aPanicCode Panic code
-
-@return KErrNone
-
-@internalComponent
-*/	
-TInt Util::Assert(const TText* aFile, TInt aLine, TInt aPanicCode)
-	{
-	TBuf<16> tbuf;
-	Util::GetTimeStr(tbuf);
-	TBuf<80> buf;
-	_LIT(KFormat,"**%S* SQL panic %d, at %S(%d)");
-	TPtrC fname(Filename(aFile));
-	SYMBIAN_TRACE_SQL_ERR_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EError), KSqlPanic, aPanicCode, &fname, aLine));
-	buf.Format(KFormat, &tbuf, aPanicCode, &fname, aLine);
-	RDebug::Print(buf);
-	::SqlPanic(static_cast <TSqlPanic> (aPanicCode));
-	return KErrNone;
-	}
-
-#else //_ASSERTIONS
-
-/**
-The function panics the thread where it is called from.
-
-The function is used when _ASSERTIONS is not defined.
-
-@param Not used
-@param Not used
-@param aPanicCode Panic code
-
-@return KErrNone
-
-@internalComponent
-*/	
-TInt Util::Assert(const TText*, TInt, TInt aPanicCode)
-	{
-	::SqlPanic(static_cast <TSqlPanic> (aPanicCode));
-	return KErrNone;
-	}
-	
-#endif //_ASSERTIONS
-
-#if defined _NOTIFY || defined SYMBIAN_TRACE_SQL_ERR
-	
-/**
-The function prints out a "SQL leave" message to the console and leaves with aError error code.
-It gives a usefull information about the found error together with the source file name and line number where
-it occured.
-
-The function is used when _NOTIFY is defined.
-
-@param aFile Source file name
-@param aLine Source line number
-@param aError Error code
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-@internalComponent
-*/	
-void Util::Leave(const TText* aFile, TInt aLine, TInt aError)
-	{
-	SYMBIAN_TRACE_SQL_ERR_ONLY(TPtrC filename(Filename(aFile)));
-	SYMBIAN_TRACE_SQL_ERR_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EError), KSqlLeave, aError, &filename, aLine));	
-
-#ifdef _NOTIFY
-	TBuf<16> tbuf;
-	Util::GetTimeStr(tbuf);
-	TPtrC f(Filename(aFile));
-	TBuf<80> buf;
-	_LIT(KFormat,"**%S* SQL leave, error=%d at %S(%d)\r\n");
-	buf.Format(KFormat, &tbuf, aError, &f, aLine);
-	RDebug::Print(buf);
-#endif //_NOTIFY
-	User::Leave(aError);
-	}
-	
-/**
-The function prints out a "SQL leave" message to the console and leaves with aError error code, if it is 
-negative.
-It gives a usefull information about the found error together with the source file name and line number where
-it occured.
-
-The function is used when _NOTIFY is defined.
-
-@param aFile Source file name
-@param aLine Source line number
-@param aError Error code
-
-@internalComponent
-*/	
-TInt Util::LeaveIfError(const TText* aFile, TInt aLine, TInt aError)
-	{
-	if(aError<0)
-		Util::Leave(aFile,aLine,aError);
-	return aError;
-	}
-
-/**
-The function prints out a "SQL leave" message to the console and leaves with KErrNoMemory if 
-aPtr parameter is NULL.
-
-The function is used when _NOTIFY is defined.
-
-@param aFile Source file name
-@param aLine Source line number
-@param aPtr The pointer to be tested against NULL value.
+#if defined OST_TRACE_COMPILER_IN_USE && (defined _SQL_PANIC_TRACE_ENABLED || defined _SQL_LEAVE_TRACE_ENABLED) 
 
-@internalComponent
-*/	
-const void* Util::LeaveIfNull(const TText* aFile, TInt aLine, const void* aPtr)
-	{
-	if(!aPtr)
-		{
-		Util::Leave(aFile, aLine, KErrNoMemory);
-		}
-	return aPtr;
-	}
-
-/**
-The function is used by the SQL server.
-It prints out a "SQL panic" message to the console and panic the client.
-It gives a usefull information about the found error together with the source file name and line number where
-it occured.
-
-The function is used when _NOTIFY is defined.
-
-@param aFile Source file name
-@param aLine Source line number
-@param aMessage The client message, which processing caused the panic.
-@param aPanicCode Error code
-
-@leave KSqlLeavePanic
-
-@return KErrNone;
-
-@internalComponent
-*/	
-TInt Util::PanicClientL(const TText* aFile, TInt aLine, const RMessage2& aMessage, TInt aPanicCode)
-	{
-	SYMBIAN_TRACE_SQL_ERR_ONLY(TPtrC filename(Filename(aFile)));
-	SYMBIAN_TRACE_SQL_ERR_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EError), KSqlPanicClient, aPanicCode, &filename, aLine));	
-	
-#ifdef _NOTIFY
-	TBuf<16> tbuf;
-	Util::GetTimeStr(tbuf);
-	TPtrC fname(Filename(aFile));
-	TBuf<80> buf;
-	_LIT(KFormat,"**%S* SQL panic=%d at %S(%d)\r\n");
-	buf.Format(KFormat, &tbuf, aPanicCode, &fname, aLine);
-	RDebug::Print(buf);
-#endif
-	return ::SqlPanicClientL(aMessage, static_cast <TSqlPanic> (aPanicCode));
-	}
-
-#endif//defined _NOTIFY || SYMBIAN_TRACE_SQL_ERR
-
-#if defined _ASSERTIONS || defined _NOTIFY ||defined SYMBIAN_TRACE_SQL_ERR
-
-/**
-Formats the current time into aWhere descriptor.
-
-@param aWhere Output parameter. The current time will be formatted there. The buffer length should be at least 16 characters.
-
-The function is used when _ASSERT or _NOTIFY or SYMBIAN_TRACE_SQL_ERR is defined.
-
-@internalComponent
-*/
-void Util::GetTimeStr(TDes& aWhere)
-	{
-	TTime time;
-	time.HomeTime();
-	TDateTime dt = time.DateTime();
-	aWhere.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
-	};
-	
 /**
 The function creates and returns TPtrC object which points to aFile parameter.
 
 @param aFile File name
 @return TPtrC object pointing to aFile parameter.
 
-The function is used when _ASSERT or _NOTIFY or SYMBIAN_TRACE_SQL_ERR is defined.
-
 @internalComponent
 */	
-TPtrC Util::Filename(const TText* aFile)
+TPtrC TSqlUtil::FileName(const TText* aFile)
 	{
 	TPtrC p(aFile);
 	TInt ix = p.LocateReverse('\\');
@@ -361,4 +368,4 @@
 	return p;
 	}
 
-#endif//defined _ASSERTIONS || defined _NOTIFY || SYMBIAN_TRACE_SQL_ERR
+#endif //defined OST_TRACE_COMPILER_IN_USE && (defined _SQL_PANIC_TRACE_ENABLED || defined _SQL_LEAVE_TRACE_ENABLED)