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