1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmtestenv/mmtestfw/Source/TestFrameworkServer/LogFile.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,213 @@
1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +// EPOC includes
1.20 +#include <e32base.h>
1.21 +
1.22 +// Test system includes
1.23 +#include "LogFile.h"
1.24 +
1.25 +
1.26 +/*
1.27 + * destructor for CFileLogger.
1.28 + */
1.29 +CFileLogger::~CFileLogger()
1.30 + {
1.31 + // make sure session to file server is closed
1.32 + Close();
1.33 + }
1.34 +
1.35 +
1.36 +/**
1.37 + *
1.38 + * Static constructor for CFileLogger.
1.39 + *
1.40 + * @return "CFileLogger*"
1.41 + * The constructed CFileLogger
1.42 + *
1.43 + * @xxxx
1.44 + *
1.45 + */
1.46 +CFileLogger* CFileLogger::NewL()
1.47 + {
1.48 + CFileLogger* s = new(ELeave) CFileLogger;
1.49 + return s;
1.50 + }
1.51 +
1.52 +/**
1.53 + *
1.54 + * Create and open a log file. The file is shareable, and a mutex
1.55 + * and file seeks employed in case more than one session is using
1.56 + * the file.
1.57 + *
1.58 + * @param "const TDesC& aDir"
1.59 + * The file path of the logfile to be created. If the path
1.60 + * does not exist, the logfile will not be created.
1.61 + *
1.62 + * @param "const TDesC& aName"
1.63 + * The file name of same.
1.64 + *
1.65 + * @xxxx
1.66 + *
1.67 + */
1.68 +void CFileLogger::CreateLog(const TDesC& aDir, const TDesC& aName)
1.69 + {
1.70 + // if aDir or aName are empty, then panic
1.71 + __ASSERT_ALWAYS(aDir.Ptr() != NULL, User::Panic(_L("CFileLogger"), 0));
1.72 + __ASSERT_ALWAYS(aName.Ptr() != NULL, User::Panic(_L("CFileLogger"), 1));
1.73 +
1.74 +
1.75 + // if aDir doesn't exist, create it anyway
1.76 + TUint aAttValue;
1.77 + if (iFs.Att(aDir, aAttValue) != KErrNone)
1.78 + {
1.79 + TInt returnCode = iFs.MkDirAll(aDir);
1.80 + if (returnCode != KErrNone)
1.81 + {
1.82 + iEnabled = EFalse;
1.83 + User::Panic(_L("CFileLogger"), 2);
1.84 + }
1.85 + }
1.86 +
1.87 + iEnabled = ETrue;
1.88 + iLogName = aName;
1.89 +
1.90 + // create logfile mutex
1.91 + RMutex logMutex;
1.92 + logMutex.CreateGlobal(iLogName);
1.93 +
1.94 + // make the correct file name
1.95 + TFileName logFile;
1.96 + logFile.Format(_L("%S%S"), &aDir, &aName); // aDir will be \\-terminated
1.97 +
1.98 + // If the file does not exist, create it. If it does, truncate it
1.99 + // Open it for sharing
1.100 + TInt returnCode=iLogfile.Replace(iFs, logFile, EFileWrite | EFileStreamText | EFileShareAny);
1.101 +
1.102 + // check if open fails
1.103 + if (returnCode==KErrNone)
1.104 + {
1.105 + //file has opened ok
1.106 + //add the start to the HTML
1.107 + iLogfile.Write(_L8("<html><body><pre>\n"));
1.108 + TInt dummy;
1.109 + iLogfile.Seek(ESeekEnd, dummy);
1.110 + }
1.111 + else
1.112 + iEnabled = EFalse;
1.113 + }
1.114 +
1.115 +/**
1.116 + *
1.117 + * Close the logfile. The flag iEnabled will be clear if there
1.118 + * is no logfile to close.
1.119 + *
1.120 + * @xxxx
1.121 + *
1.122 + */
1.123 +void CFileLogger::CloseLog()
1.124 + {
1.125 +
1.126 + // this should be mutex-safe as each thread has its own handle to
1.127 + // the log file
1.128 + // if logging enabled, flush buffers
1.129 + if (iEnabled)
1.130 + {
1.131 + iLogfile.Flush();
1.132 + iLogfile.Close();
1.133 + iEnabled = EFalse;
1.134 + }
1.135 +
1.136 + // disconnect from the file server
1.137 + iFs.Close();
1.138 + }
1.139 +
1.140 +/**
1.141 + *
1.142 + * Write output to the logfile.
1.143 + *
1.144 + * @param "const TDesC& aMsg"
1.145 + * The message (string) to be written.
1.146 + *
1.147 + * @xxxx
1.148 + *
1.149 + */
1.150 +void CFileLogger::WriteLog(const TDesC& aMsg)
1.151 + {
1.152 +
1.153 + if (!iEnabled)
1.154 + return;
1.155 +
1.156 + // convert from unicode to 8 bit
1.157 + TBuf8<KMaxLogLineLength> lineBuf8;
1.158 + const TText* msgBuf = aMsg.Ptr();
1.159 + for (TInt i = 0; i < aMsg.Length() ;i++)
1.160 + lineBuf8.Append(STATIC_CAST(TText, msgBuf[i]));
1.161 +
1.162 + // wait on mutex
1.163 + RMutex logMutex;
1.164 + logMutex.OpenGlobal(iLogName);
1.165 +
1.166 + logMutex.Wait();
1.167 +
1.168 + // write to log file
1.169 + TInt dummy=0;
1.170 + iLogfile.Seek(ESeekEnd, dummy);
1.171 + iLogfile.Write(lineBuf8);
1.172 + // seek to end after write - in case another process or thread is also using the file
1.173 + dummy=0; //dummy is updated after a seek to contain the new file position. With ESeekEnd
1.174 + //it is used as an offest value to control the seek.
1.175 + iLogfile.Seek(ESeekEnd, dummy);
1.176 +
1.177 + logMutex.Signal();
1.178 + logMutex.Close();
1.179 + }
1.180 +
1.181 +/**
1.182 + *
1.183 + * Connect to the file server. Used as a helper function
1.184 + * by classes owning a CFileLogger, to ensure that a
1.185 + * file server session can be opened
1.186 + *
1.187 + * @return "TInt"
1.188 + * standard EPOC error code
1.189 + *
1.190 + * @xxxx
1.191 + *
1.192 + */
1.193 +TInt CFileLogger::Connect()
1.194 + {
1.195 + TInt err = iFs.Connect();
1.196 + if (err == KErrNone)
1.197 + iFsOpen = ETrue;
1.198 + return err;
1.199 + }
1.200 +
1.201 +/**
1.202 + *
1.203 + * Close a file server session. Used as a helper function
1.204 + * by classes owning a CFileLogger.
1.205 + *
1.206 + * @xxxx
1.207 + *
1.208 + */
1.209 +void CFileLogger::Close()
1.210 + {
1.211 + if (iFsOpen)
1.212 + {
1.213 + iFs.Close();
1.214 + iFsOpen = EFalse;
1.215 + }
1.216 + }