sl@0: /*
sl@0: * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: #include "ct/logger.h"
sl@0: #include <f32file.h>
sl@0: 
sl@0: #if defined(_DEBUG) && defined(__CT_LOGGING__)
sl@0: 
sl@0: _LIT(KLogFilename, "\\logs\\security\\cryptotokens.txt");	
sl@0: _LIT(KIndent, "  ");
sl@0: _LIT8(KNewLine, "\r\n");
sl@0: 
sl@0: #define MAX_LEN 128
sl@0: 
sl@0: static TInt Indent()
sl@0: 	{
sl@0: 	return reinterpret_cast<TInt>(Dll::Tls());
sl@0: 	}
sl@0: 
sl@0: static void SetIndent(TInt aIndent)
sl@0: 	{
sl@0: 	Dll::SetTls(reinterpret_cast<TAny*>(aIndent));
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void CTLogger::Log(TAny* aObject, TRefByValue<const TDesC16> aFmt, ...)
sl@0: 	{
sl@0: 	TBuf<MAX_LEN> data;
sl@0: 
sl@0: 	data.AppendFormat(_L("%08x: "), aObject);
sl@0: 
sl@0: 	for (TInt i = 0 ; i < Indent() ; ++i)
sl@0: 		{
sl@0: 		data.Append(KIndent);
sl@0: 		}
sl@0: 
sl@0: 	VA_LIST args;
sl@0: 	VA_START(args, aFmt);
sl@0: 	data.AppendFormatList(aFmt, args);
sl@0: 	VA_END(args);
sl@0: 
sl@0: 	TRAPD(err, LogL(data));
sl@0: 	ASSERT(err == KErrNone);
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void CTLogger::UpdateIndent(TInt aInc)
sl@0: 	{
sl@0: 	TInt newIndent = Indent() + aInc;
sl@0: 	ASSERT(newIndent >= 0);
sl@0: 	SetIndent(newIndent);
sl@0: 	}
sl@0: 
sl@0: void CTLogger::LogL(const TDesC& aString)
sl@0: 	{
sl@0: 	// Open the file server and file
sl@0: 	RFs fs;
sl@0: 	User::LeaveIfError(fs.Connect());
sl@0: 	CleanupClosePushL(fs);
sl@0: 	
sl@0: 	// Open the file or create it if doesn't exist, create it
sl@0: 	RFile file;
sl@0: 	TDriveUnit sysDrive (fs.GetSystemDrive());
sl@0: 	TBuf<128> logFile (sysDrive.Name());
sl@0: 	logFile.Append(KLogFilename);
sl@0: 	
sl@0: 	TInt error = file.Open(fs, logFile, EFileWrite|EFileShareAny);
sl@0: 	if (error == KErrNotFound)
sl@0: 		{
sl@0: 		error = file.Create(fs, logFile, EFileWrite|EFileShareAny);
sl@0: 		}
sl@0: 	User::LeaveIfError(error);
sl@0: 	CleanupClosePushL(file);
sl@0: 	
sl@0: 	// Seek to the end of the file
sl@0: 	TInt tmp = 0;
sl@0: 	file.Seek(ESeekEnd, tmp);
sl@0: 
sl@0: 	// And do some logging
sl@0: 	TBuf8<MAX_LEN> buf;
sl@0: 	buf.Copy(aString);
sl@0: 	file.Write(buf);
sl@0: 	file.Write(KNewLine);
sl@0: 
sl@0: 	// Close and tidy up
sl@0: 	CleanupStack::PopAndDestroy(2, &fs);
sl@0: 	}
sl@0: 
sl@0: #else
sl@0: 
sl@0: EXPORT_C void CTLogger::Log(TAny* /*aObject*/, TRefByValue<const TDesC16> /*aFmt*/, ...)
sl@0: 	{
sl@0: 	User::Invariant();
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void CTLogger::UpdateIndent(TInt /*aInc*/)
sl@0: 	{
sl@0: 	User::Invariant();
sl@0: 	}
sl@0: 
sl@0: #endif