persistentstorage/sqlite3api/OsLayer/SqliteUtil.cpp
changeset 31 ba1c4f4a893f
equal deleted inserted replaced
29:cce6680bbf1c 31:ba1c4f4a893f
       
     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 // "%lld" with "%ld"
       
    47 //These are the differences in format specification between RDebig::Print and OST functions.
       
    48 //The new format spec length should be less or equal than the old format spec length.
       
    49 static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
       
    50 	{
       
    51 	TInt fmtLength = aFormat.Length();
       
    52 	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
       
    53     TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
       
    54     TInt pos;
       
    55     while((pos = ptr.Find(aFmtSpec)) >= 0)
       
    56     	{
       
    57 		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
       
    58 		fmtLength -= KDiff;
       
    59 		ptr.Set(ptr.MidTPtr(pos));
       
    60     	}
       
    61     aFormat.SetLength(fmtLength);
       
    62 	}
       
    63 
       
    64 void SqlitePrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
       
    65     {
       
    66     VA_LIST list;
       
    67     VA_START(list, aFormat);
       
    68     TBuf<128> format;
       
    69     _LIT(KTraceIdent, "Sqlite3;");
       
    70     format.Copy(TPtrC8((const TUint8*)aFormat));
       
    71     format.Insert(0, KTraceIdent);
       
    72     format.Append(_L("\r\n"));
       
    73     _LIT(KOstI64Fmt, "%lld");
       
    74     _LIT(KDbgPrnI64Fmt, "%ld");
       
    75     ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
       
    76     TBuf<KSqliteMaxPrnStrLen> buf;
       
    77     TSqliteDes16Overflow overflowHandler;
       
    78     buf.AppendFormatList(format, list, &overflowHandler);
       
    79 #ifdef _SQLITE_RDEBUG_PRINT    
       
    80     RDebug::RawPrint(buf);
       
    81 #endif
       
    82     }
       
    83 
       
    84 #endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT 
       
    85 
       
    86 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    87 
       
    88 /**
       
    89 SQLite panic category.
       
    90 
       
    91 @internalComponent
       
    92 */
       
    93 _LIT(KSqlitePanicCategory, "Sqlite3");
       
    94 
       
    95 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    96 
       
    97 /**
       
    98 Panics the caller with aPanicCode panic code.
       
    99 The call will terminate the thread where it is called from.
       
   100 
       
   101 @param aPanicCode Panic code.
       
   102 
       
   103 @internalComponent
       
   104 */
       
   105 static void SqlitePanic(TSqlitePanic aPanicCode)
       
   106 	{
       
   107 	User::Panic(KSqlitePanicCategory, aPanicCode);
       
   108 	}
       
   109 	
       
   110 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   111 
       
   112 /**
       
   113 The function prints out a "SQLite" panic message to the console and panics the thread where it is called from.
       
   114 It gives a useful information about the found error together with the source file name and line number where
       
   115 it occurred.
       
   116 
       
   117 Note: this function  will output information regarding the panic only if _SQLITE_PANIC_TRACE_ENABLED macro is defined  
       
   118 
       
   119 @param aFile Source file name
       
   120 @param aLine Source line number
       
   121 @param aPanicCode Panic code
       
   122 @param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
       
   123 
       
   124 @return KErrNone
       
   125 
       
   126 @internalComponent
       
   127 */  
       
   128 TInt TSqliteUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
       
   129     {
       
   130 #if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
       
   131     TPtrC fname(FileName(aFile));
       
   132     OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLITEPRNSTR(fname), aLine, __SQLITEPRNSTR(KSqlitePanicCategory), aPanicCode);
       
   133 #else
       
   134     UNUSED_ARG(aFile);
       
   135     UNUSED_ARG(aLine);
       
   136     UNUSED_ARG(aHandle);
       
   137 #endif      
       
   138     ::SqlitePanic(static_cast <TSqlitePanic> (aPanicCode));
       
   139     return KErrNone;
       
   140     }
       
   141 
       
   142 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   143 
       
   144 #if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
       
   145 
       
   146 /**
       
   147 The function creates and returns TPtrC object which points to aFile parameter.
       
   148 
       
   149 @param aFile File name
       
   150 @return TPtrC object pointing to aFile parameter.
       
   151 
       
   152 @internalComponent
       
   153 */	
       
   154 TPtrC TSqliteUtil::FileName(const TText* aFile)
       
   155 	{
       
   156 	TPtrC p(aFile);
       
   157 	TInt ix = p.LocateReverse('\\');
       
   158 	if(ix<0)
       
   159 		ix=p.LocateReverse('/');
       
   160 	if(ix>=0)
       
   161 		p.Set(p.Mid(1+ix));
       
   162 	return p;
       
   163 	}
       
   164 
       
   165 #endif //defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
       
   166 
       
   167 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////