os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/CTLogger.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "ct/logger.h"
sl@0
    20
#include <f32file.h>
sl@0
    21
sl@0
    22
#if defined(_DEBUG) && defined(__CT_LOGGING__)
sl@0
    23
sl@0
    24
_LIT(KLogFilename, "\\logs\\security\\cryptotokens.txt");	
sl@0
    25
_LIT(KIndent, "  ");
sl@0
    26
_LIT8(KNewLine, "\r\n");
sl@0
    27
sl@0
    28
#define MAX_LEN 128
sl@0
    29
sl@0
    30
static TInt Indent()
sl@0
    31
	{
sl@0
    32
	return reinterpret_cast<TInt>(Dll::Tls());
sl@0
    33
	}
sl@0
    34
sl@0
    35
static void SetIndent(TInt aIndent)
sl@0
    36
	{
sl@0
    37
	Dll::SetTls(reinterpret_cast<TAny*>(aIndent));
sl@0
    38
	}
sl@0
    39
sl@0
    40
EXPORT_C void CTLogger::Log(TAny* aObject, TRefByValue<const TDesC16> aFmt, ...)
sl@0
    41
	{
sl@0
    42
	TBuf<MAX_LEN> data;
sl@0
    43
sl@0
    44
	data.AppendFormat(_L("%08x: "), aObject);
sl@0
    45
sl@0
    46
	for (TInt i = 0 ; i < Indent() ; ++i)
sl@0
    47
		{
sl@0
    48
		data.Append(KIndent);
sl@0
    49
		}
sl@0
    50
sl@0
    51
	VA_LIST args;
sl@0
    52
	VA_START(args, aFmt);
sl@0
    53
	data.AppendFormatList(aFmt, args);
sl@0
    54
	VA_END(args);
sl@0
    55
sl@0
    56
	TRAPD(err, LogL(data));
sl@0
    57
	ASSERT(err == KErrNone);
sl@0
    58
	}
sl@0
    59
sl@0
    60
EXPORT_C void CTLogger::UpdateIndent(TInt aInc)
sl@0
    61
	{
sl@0
    62
	TInt newIndent = Indent() + aInc;
sl@0
    63
	ASSERT(newIndent >= 0);
sl@0
    64
	SetIndent(newIndent);
sl@0
    65
	}
sl@0
    66
sl@0
    67
void CTLogger::LogL(const TDesC& aString)
sl@0
    68
	{
sl@0
    69
	// Open the file server and file
sl@0
    70
	RFs fs;
sl@0
    71
	User::LeaveIfError(fs.Connect());
sl@0
    72
	CleanupClosePushL(fs);
sl@0
    73
	
sl@0
    74
	// Open the file or create it if doesn't exist, create it
sl@0
    75
	RFile file;
sl@0
    76
	TDriveUnit sysDrive (fs.GetSystemDrive());
sl@0
    77
	TBuf<128> logFile (sysDrive.Name());
sl@0
    78
	logFile.Append(KLogFilename);
sl@0
    79
	
sl@0
    80
	TInt error = file.Open(fs, logFile, EFileWrite|EFileShareAny);
sl@0
    81
	if (error == KErrNotFound)
sl@0
    82
		{
sl@0
    83
		error = file.Create(fs, logFile, EFileWrite|EFileShareAny);
sl@0
    84
		}
sl@0
    85
	User::LeaveIfError(error);
sl@0
    86
	CleanupClosePushL(file);
sl@0
    87
	
sl@0
    88
	// Seek to the end of the file
sl@0
    89
	TInt tmp = 0;
sl@0
    90
	file.Seek(ESeekEnd, tmp);
sl@0
    91
sl@0
    92
	// And do some logging
sl@0
    93
	TBuf8<MAX_LEN> buf;
sl@0
    94
	buf.Copy(aString);
sl@0
    95
	file.Write(buf);
sl@0
    96
	file.Write(KNewLine);
sl@0
    97
sl@0
    98
	// Close and tidy up
sl@0
    99
	CleanupStack::PopAndDestroy(2, &fs);
sl@0
   100
	}
sl@0
   101
sl@0
   102
#else
sl@0
   103
sl@0
   104
EXPORT_C void CTLogger::Log(TAny* /*aObject*/, TRefByValue<const TDesC16> /*aFmt*/, ...)
sl@0
   105
	{
sl@0
   106
	User::Invariant();
sl@0
   107
	}
sl@0
   108
sl@0
   109
EXPORT_C void CTLogger::UpdateIndent(TInt /*aInc*/)
sl@0
   110
	{
sl@0
   111
	User::Invariant();
sl@0
   112
	}
sl@0
   113
sl@0
   114
#endif