persistentstorage/sql/OsLayer/SqliteUtil.cpp
branchRCL_3
changeset 24 cc28652e0254
parent 23 26645d81f48d
child 25 047f208ea78f
equal deleted inserted replaced
23:26645d81f48d 24:cc28652e0254
     1 // Copyright (c) 2010 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 #include <e32debug.h>
       
    16 #include "SqliteUtil.h"
       
    17 #include "OstTraceDefinitions.h"
       
    18 #ifdef OST_TRACE_COMPILER_IN_USE
       
    19 #include "SqliteUtilTraces.h"
       
    20 #endif
       
    21 #include "SqliteTraceDef.h"
       
    22 
       
    23 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    24 
       
    25 #define UNUSED_ARG(arg) arg = arg
       
    26 #define UNUSED_DES(arg) arg
       
    27 
       
    28 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    29 
       
    30 #if defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT
       
    31 
       
    32 /**
       
    33 This class has been added here to avoid the crashes when _SQLITE_RDEBUG_PRINT macro is defined but the
       
    34 data to be printed out is too big and cannot fit into the buffer with size KSqliteMaxPrnStrLen.
       
    35 @internalComponent   
       
    36 */
       
    37 class TSqliteDes16Overflow : public TDes16Overflow
       
    38     {
       
    39 public:    
       
    40     virtual void Overflow(TDes16& /*aDes*/)
       
    41         {
       
    42         }
       
    43     };
       
    44 
       
    45 //Replaces:
       
    46 // 1) "%lld" with "%ld"
       
    47 // 2) "%s" with "%S"
       
    48 //These are the differences in format specification between RDebig::Print and OST functions.
       
    49 //The new format spec length should be less or equal than the old format spec length.
       
    50 static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
       
    51 	{
       
    52 	TInt fmtLength = aFormat.Length();
       
    53 	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
       
    54     TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
       
    55     TInt pos;
       
    56     while((pos = ptr.Find(aFmtSpec)) >= 0)
       
    57     	{
       
    58 		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
       
    59 		fmtLength -= KDiff;
       
    60 		ptr.Set(ptr.MidTPtr(pos));
       
    61     	}
       
    62     aFormat.SetLength(fmtLength);
       
    63 	}
       
    64 
       
    65 void SqlitePrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
       
    66     {
       
    67     VA_LIST list;
       
    68     VA_START(list, aFormat);
       
    69     TBuf<128> format;
       
    70     _LIT(KTraceIdent, "Sqlite;");
       
    71     format.Copy(TPtrC8((const TUint8*)aFormat));
       
    72     format.Insert(0, KTraceIdent);
       
    73     format.Append(_L("\r\n"));
       
    74     _LIT(KOstI64Fmt, "%lld");
       
    75     _LIT(KDbgPrnI64Fmt, "%ld");
       
    76     ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
       
    77     _LIT(KOstDes8Fmt, "%s");
       
    78     _LIT(KDbgPrnDesFmt, "%S");
       
    79     ReplaceFmtSpec(format, KOstDes8Fmt, KDbgPrnDesFmt);
       
    80     TBuf<KSqliteMaxPrnStrLen> buf;
       
    81     TSqliteDes16Overflow overflowHandler;
       
    82     buf.AppendFormatList(format, list, &overflowHandler);
       
    83 #ifdef _SQLITE_RDEBUG_PRINT    
       
    84     RDebug::RawPrint(buf);
       
    85 #endif
       
    86     }
       
    87 
       
    88 const TDesC* SqliteDes8to16Ptr(const TDesC8& aDes)
       
    89 	{
       
    90 	const TInt KBufSize = 100;
       
    91 	static TBuf<KBufSize> buf;
       
    92 	TPtrC8 ptr(aDes.Ptr(), Min(aDes.Length(), buf.MaxLength()));
       
    93 	buf.Copy(ptr);
       
    94 	return &buf;
       
    95 	}
       
    96 
       
    97 #endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT 
       
    98 
       
    99 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   100 
       
   101 /**
       
   102 SQLite panic category.
       
   103 
       
   104 @internalComponent
       
   105 */
       
   106 _LIT(KSqlitePanicCategory, "Sqlite");
       
   107 
       
   108 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   109 
       
   110 /**
       
   111 Panics the caller with aPanicCode panic code.
       
   112 The call will terminate the thread where it is called from.
       
   113 
       
   114 @param aPanicCode Panic code.
       
   115 
       
   116 @internalComponent
       
   117 */
       
   118 static void SqlitePanic(TSqlitePanic aPanicCode)
       
   119 	{
       
   120 	User::Panic(KSqlitePanicCategory, aPanicCode);
       
   121 	}
       
   122 	
       
   123 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   124 
       
   125 /**
       
   126 The function prints out a "SQLite" panic message to the console and panics the thread where it is called from.
       
   127 It gives a useful information about the found error together with the source file name and line number where
       
   128 it occurred.
       
   129 
       
   130 Note: this function  will output information regarding the panic only if _SQLITE_PANIC_TRACE_ENABLED macro is defined  
       
   131 
       
   132 @param aFile Source file name
       
   133 @param aLine Source line number
       
   134 @param aPanicCode Panic code
       
   135 @param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
       
   136 
       
   137 @return KErrNone
       
   138 
       
   139 @internalComponent
       
   140 */  
       
   141 TInt TSqliteUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
       
   142     {
       
   143 #if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
       
   144     TPtrC fname(FileName(aFile));
       
   145     OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "SQLite-Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLITEPRNSTR(fname), aLine, __SQLITEPRNSTR(KSqlitePanicCategory), aPanicCode);
       
   146 #else
       
   147     UNUSED_ARG(aFile);
       
   148     UNUSED_ARG(aLine);
       
   149     UNUSED_ARG(aHandle);
       
   150 #endif      
       
   151     ::SqlitePanic(static_cast <TSqlitePanic> (aPanicCode));
       
   152     return KErrNone;
       
   153     }
       
   154 
       
   155 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   156 
       
   157 #if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
       
   158 
       
   159 /**
       
   160 The function creates and returns TPtrC object which points to aFile parameter.
       
   161 
       
   162 @param aFile File name
       
   163 @return TPtrC object pointing to aFile parameter.
       
   164 
       
   165 @internalComponent
       
   166 */	
       
   167 TPtrC TSqliteUtil::FileName(const TText* aFile)
       
   168 	{
       
   169 	TPtrC p(aFile);
       
   170 	TInt ix = p.LocateReverse('\\');
       
   171 	if(ix<0)
       
   172 		ix=p.LocateReverse('/');
       
   173 	if(ix>=0)
       
   174 		p.Set(p.Mid(1+ix));
       
   175 	return p;
       
   176 	}
       
   177 
       
   178 #endif //defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
       
   179 
       
   180 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////