os/persistentdata/persistentstorage/sqlite3api/OsLayer/SqliteUtil.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
#include <e32debug.h>
sl@0
    16
#include "SqliteUtil.h"
sl@0
    17
#include "OstTraceDefinitions.h"
sl@0
    18
#ifdef OST_TRACE_COMPILER_IN_USE
sl@0
    19
#include "SqliteUtilTraces.h"
sl@0
    20
#endif
sl@0
    21
#include "SqliteTraceDef.h"
sl@0
    22
sl@0
    23
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    24
sl@0
    25
#define UNUSED_ARG(arg) arg = arg
sl@0
    26
#define UNUSED_DES(arg) arg
sl@0
    27
sl@0
    28
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    29
sl@0
    30
#if defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT
sl@0
    31
sl@0
    32
/**
sl@0
    33
This class has been added here to avoid the crashes when _SQLITE_RDEBUG_PRINT macro is defined but the
sl@0
    34
data to be printed out is too big and cannot fit into the buffer with size KSqliteMaxPrnStrLen.
sl@0
    35
@internalComponent   
sl@0
    36
*/
sl@0
    37
class TSqliteDes16Overflow : public TDes16Overflow
sl@0
    38
    {
sl@0
    39
public:    
sl@0
    40
    virtual void Overflow(TDes16& /*aDes*/)
sl@0
    41
        {
sl@0
    42
        }
sl@0
    43
    };
sl@0
    44
sl@0
    45
//Replaces:
sl@0
    46
// "%lld" with "%ld"
sl@0
    47
//These are the differences in format specification between RDebig::Print and OST functions.
sl@0
    48
//The new format spec length should be less or equal than the old format spec length.
sl@0
    49
static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
sl@0
    50
	{
sl@0
    51
	TInt fmtLength = aFormat.Length();
sl@0
    52
	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
sl@0
    53
    TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
sl@0
    54
    TInt pos;
sl@0
    55
    while((pos = ptr.Find(aFmtSpec)) >= 0)
sl@0
    56
    	{
sl@0
    57
		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
sl@0
    58
		fmtLength -= KDiff;
sl@0
    59
		ptr.Set(ptr.MidTPtr(pos));
sl@0
    60
    	}
sl@0
    61
    aFormat.SetLength(fmtLength);
sl@0
    62
	}
sl@0
    63
sl@0
    64
void SqlitePrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
sl@0
    65
    {
sl@0
    66
    VA_LIST list;
sl@0
    67
    VA_START(list, aFormat);
sl@0
    68
    TBuf<128> format;
sl@0
    69
    _LIT(KTraceIdent, "Sqlite3;");
sl@0
    70
    format.Copy(TPtrC8((const TUint8*)aFormat));
sl@0
    71
    format.Insert(0, KTraceIdent);
sl@0
    72
    format.Append(_L("\r\n"));
sl@0
    73
    _LIT(KOstI64Fmt, "%lld");
sl@0
    74
    _LIT(KDbgPrnI64Fmt, "%ld");
sl@0
    75
    ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
sl@0
    76
    TBuf<KSqliteMaxPrnStrLen> buf;
sl@0
    77
    TSqliteDes16Overflow overflowHandler;
sl@0
    78
    buf.AppendFormatList(format, list, &overflowHandler);
sl@0
    79
#ifdef _SQLITE_RDEBUG_PRINT    
sl@0
    80
    RDebug::RawPrint(buf);
sl@0
    81
#endif
sl@0
    82
    }
sl@0
    83
sl@0
    84
#endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT 
sl@0
    85
sl@0
    86
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    87
sl@0
    88
/**
sl@0
    89
SQLite panic category.
sl@0
    90
sl@0
    91
@internalComponent
sl@0
    92
*/
sl@0
    93
_LIT(KSqlitePanicCategory, "Sqlite3");
sl@0
    94
sl@0
    95
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    96
sl@0
    97
/**
sl@0
    98
Panics the caller with aPanicCode panic code.
sl@0
    99
The call will terminate the thread where it is called from.
sl@0
   100
sl@0
   101
@param aPanicCode Panic code.
sl@0
   102
sl@0
   103
@internalComponent
sl@0
   104
*/
sl@0
   105
static void SqlitePanic(TSqlitePanic aPanicCode)
sl@0
   106
	{
sl@0
   107
	User::Panic(KSqlitePanicCategory, aPanicCode);
sl@0
   108
	}
sl@0
   109
	
sl@0
   110
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   111
sl@0
   112
/**
sl@0
   113
The function prints out a "SQLite" panic message to the console and panics the thread where it is called from.
sl@0
   114
It gives a useful information about the found error together with the source file name and line number where
sl@0
   115
it occurred.
sl@0
   116
sl@0
   117
Note: this function  will output information regarding the panic only if _SQLITE_PANIC_TRACE_ENABLED macro is defined  
sl@0
   118
sl@0
   119
@param aFile Source file name
sl@0
   120
@param aLine Source line number
sl@0
   121
@param aPanicCode Panic code
sl@0
   122
@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
sl@0
   123
sl@0
   124
@return KErrNone
sl@0
   125
sl@0
   126
@internalComponent
sl@0
   127
*/  
sl@0
   128
TInt TSqliteUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
sl@0
   129
    {
sl@0
   130
#if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
sl@0
   131
    TPtrC fname(FileName(aFile));
sl@0
   132
    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLITEPRNSTR(fname), aLine, __SQLITEPRNSTR(KSqlitePanicCategory), aPanicCode);
sl@0
   133
#else
sl@0
   134
    UNUSED_ARG(aFile);
sl@0
   135
    UNUSED_ARG(aLine);
sl@0
   136
    UNUSED_ARG(aHandle);
sl@0
   137
#endif      
sl@0
   138
    ::SqlitePanic(static_cast <TSqlitePanic> (aPanicCode));
sl@0
   139
    return KErrNone;
sl@0
   140
    }
sl@0
   141
sl@0
   142
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   143
sl@0
   144
#if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
sl@0
   145
sl@0
   146
/**
sl@0
   147
The function creates and returns TPtrC object which points to aFile parameter.
sl@0
   148
sl@0
   149
@param aFile File name
sl@0
   150
@return TPtrC object pointing to aFile parameter.
sl@0
   151
sl@0
   152
@internalComponent
sl@0
   153
*/	
sl@0
   154
TPtrC TSqliteUtil::FileName(const TText* aFile)
sl@0
   155
	{
sl@0
   156
	TPtrC p(aFile);
sl@0
   157
	TInt ix = p.LocateReverse('\\');
sl@0
   158
	if(ix<0)
sl@0
   159
		ix=p.LocateReverse('/');
sl@0
   160
	if(ix>=0)
sl@0
   161
		p.Set(p.Mid(1+ix));
sl@0
   162
	return p;
sl@0
   163
	}
sl@0
   164
sl@0
   165
#endif //defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
sl@0
   166
sl@0
   167
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////