os/persistentdata/loggingservices/eventlogger/LogCli/src/logservcli.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/loggingservices/eventlogger/LogCli/src/logservcli.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,250 @@
     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 +#include "logservcli.h"
    1.20 +
    1.21 +// System includes
    1.22 +#include <e32math.h>
    1.23 +#include <s32mem.h>
    1.24 +
    1.25 +// User includes
    1.26 +#include "LogServShared.h"
    1.27 +
    1.28 +static TInt StartServer()
    1.29 +//
    1.30 +// Start the server process/thread which lives in an EPOCEXE object
    1.31 +//
    1.32 +	{
    1.33 +	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
    1.34 +
    1.35 +	//
    1.36 +	// EPOC is easy, we just create a new server process. Simultaneous launching
    1.37 +	// of two such processes should be detected when the second one attempts to
    1.38 +	// create the server object, failing with KErrAlreadyExists.
    1.39 +	//
    1.40 +	RProcess server;
    1.41 +	TInt r=server.Create(KLogServerName,KNullDesC,serverUid);
    1.42 +	if (r == KErrNone)
    1.43 +		server.SetPriority(EPriorityForeground);
    1.44 +		
    1.45 +	if (r!=KErrNone)
    1.46 +		return r;
    1.47 +	
    1.48 +	TRequestStatus stat;
    1.49 +	server.Rendezvous(stat);
    1.50 +	if (stat!=KRequestPending)
    1.51 +		server.Kill(0);		// abort startup
    1.52 +	else
    1.53 +		server.Resume();	// logon OK - start the server
    1.54 +	User::WaitForRequest(stat);		// wait for start or death
    1.55 +	// we can't use the 'exit reason' if the server panicked as this
    1.56 +	// is the panic 'reason' and may be '0' which cannot be distinguished
    1.57 +	// from KErrNone
    1.58 +	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
    1.59 +	server.Close();
    1.60 +	return r;
    1.61 +
    1.62 +	}
    1.63 +
    1.64 +// Initialise the operation and view ids
    1.65 +RLogSession::RLogSession()
    1.66 +: iOperationId(KLogNullOperationId + 1), iViewId(KLogNullViewId + 1)
    1.67 +	{
    1.68 +	}
    1.69 +
    1.70 +TInt RLogSession::Connect()
    1.71 +//
    1.72 +// Connect to the server, attempting to start it if necessary
    1.73 +//
    1.74 +	{
    1.75 +	TInt retry=2;
    1.76 +	for (;;)
    1.77 +		{
    1.78 +		TInt r=CreateSession(KLogServerFullName,TVersion(0,0,0));
    1.79 +		if (r!=KErrNotFound && r!=KErrServerTerminated)
    1.80 +			return r;
    1.81 +		if (--retry==0)
    1.82 +			return r;
    1.83 +		r=StartServer();
    1.84 +		if (r!=KErrNone && r!=KErrAlreadyExists)
    1.85 +			return r;
    1.86 +		}
    1.87 +	}
    1.88 +
    1.89 +
    1.90 +void RLogSession::Send(TInt aType, const TIpcArgs& aArgs, TRequestStatus& aStatus) const
    1.91 +	{
    1.92 +	SendWithRetryAsync(aType, aArgs, aStatus);
    1.93 +	}
    1.94 +
    1.95 +TInt RLogSession::Send(TInt aType, const TIpcArgs& aArgs) const
    1.96 +	{
    1.97 +	return SendWithRetry(aType, aArgs);
    1.98 +	}
    1.99 +
   1.100 +TInt RLogSession::SendWithRetry(TInt aType, const TIpcArgs& aParam) const
   1.101 +	{
   1.102 +	TInt ret = SendReceive(aType, aParam);
   1.103 +	if (ret < KErrNone)
   1.104 +		{
   1.105 +		if(ret == KErrServerTerminated || ret == KErrNotFound)
   1.106 +			{
   1.107 +			// Try to reconnect
   1.108 +			const_cast<RLogSession&>(*this).Close();
   1.109 +			ret = const_cast<RLogSession&>(*this).Connect();
   1.110 +			if(ret == KErrNone)
   1.111 +				{
   1.112 +				// Try to resend
   1.113 +				ret = SendReceive(aType, aParam);
   1.114 +				}
   1.115 +			}
   1.116 +		}
   1.117 +	return ret;
   1.118 +	}
   1.119 +
   1.120 +
   1.121 +void RLogSession::SendWithRetryAsync(TInt aType, const TIpcArgs& aParam, TRequestStatus& aStatus) const
   1.122 +	{
   1.123 +	SendReceive(aType, aParam, aStatus);
   1.124 +	}
   1.125 +
   1.126 +
   1.127 +
   1.128 +
   1.129 +
   1.130 +
   1.131 +
   1.132 +
   1.133 +
   1.134 +
   1.135 +
   1.136 +#ifdef LOGGING_ENABLED
   1.137 +
   1.138 +const TInt KLogEngLogBufferSize = 256;
   1.139 +
   1.140 +void Log::New()
   1.141 +	{
   1.142 +	_LIT(KNewLogText, "===== NEW LOG =====");
   1.143 +	//
   1.144 +	RFileLogger logger;
   1.145 +	TInt ret=logger.Connect();
   1.146 +	if	(ret==KErrNone)
   1.147 +		{
   1.148 +		logger.CreateLog(KLogFolder, KLogFileName, EFileLoggingModeOverwrite);
   1.149 +		logger.Write(KNewLogText);
   1.150 +		}
   1.151 +	logger.Close();
   1.152 +	}
   1.153 +
   1.154 +void Log::Write(const TDesC& aText)
   1.155 +	{
   1.156 +	PruneLogFile();
   1.157 +
   1.158 +	RFileLogger logger;
   1.159 +	TInt ret=logger.Connect();
   1.160 +	if (ret==KErrNone)
   1.161 +		{
   1.162 +		logger.SetDateAndTime(EFalse,EFalse);
   1.163 +		logger.CreateLog(KLogFolder, KLogFileName,EFileLoggingModeAppend);
   1.164 +		TBuf<KLogEngLogBufferSize> buf;
   1.165 +		
   1.166 +		// The debug log uses hometime rather than UTC for its timestamps. This is
   1.167 +		// purely a debugging aid.
   1.168 +		TTime now;
   1.169 +		now.HomeTime();
   1.170 +		
   1.171 +		TDateTime dateTime;
   1.172 +		dateTime = now.DateTime();
   1.173 +		buf.Format(KTimeFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
   1.174 +		buf.AppendFormat(KTextFormat,&aText);
   1.175 +
   1.176 +		logger.Write(buf);
   1.177 +		}
   1.178 +
   1.179 +	logger.Close();
   1.180 +	}
   1.181 +
   1.182 +void Log::WriteFormat(TRefByValue<const TDesC> aFmt,...)
   1.183 +	{
   1.184 +    VA_LIST list;
   1.185 +    VA_START(list,aFmt);
   1.186 +
   1.187 +	PruneLogFile();
   1.188 +
   1.189 +	TBuf<2*KLogEngLogBufferSize> buf;
   1.190 +	buf.SetMax();
   1.191 +	buf.FillZ();
   1.192 +	
   1.193 +	// The debug log uses hometime rather than UTC for its timestamps. This is
   1.194 +	// purely a debugging aid.
   1.195 +	TTime now;
   1.196 +	now.HomeTime();
   1.197 +	
   1.198 +	TDateTime dateTime;
   1.199 +	dateTime = now.DateTime();
   1.200 +	buf.Format(KTimeFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
   1.201 +	buf.AppendFormatList(aFmt, list );
   1.202 +
   1.203 +	RFileLogger logger;
   1.204 +	TInt ret=logger.Connect();
   1.205 +	if (ret==KErrNone)
   1.206 +		{
   1.207 +		logger.SetDateAndTime(EFalse,EFalse);
   1.208 +		logger.CreateLog(KLogFolder, KLogFileName,EFileLoggingModeAppend);
   1.209 +		logger.Write(buf);
   1.210 +		}
   1.211 +
   1.212 +	logger.Close();
   1.213 +	}
   1.214 +
   1.215 +void Log::PruneLogFile()
   1.216 +	{
   1.217 +	const TInt KMaxLogSize = 1024 * 500;
   1.218 +	//
   1.219 +	_LIT(KBaseFolder, "_:\\Logs\\");
   1.220 +	TFileName fileName(KBaseFolder);
   1.221 +	fileName[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
   1.222 +	fileName.Append(KLogFolder);
   1.223 +	fileName.Append(KPathDelimiter);
   1.224 +	fileName.Append(KLogFileName);
   1.225 +	//
   1.226 +	RFs fsSession;
   1.227 +	if	(fsSession.Connect() == KErrNone)
   1.228 +		{
   1.229 +		TEntry entry;
   1.230 +		if	(fsSession.Entry(fileName, entry) == KErrNone)
   1.231 +			{
   1.232 +			// Check size and delete if its too big
   1.233 +			if (entry.iSize >= KMaxLogSize)
   1.234 +				{
   1.235 +				TInt fileDeleteErr=fsSession.Delete(fileName); 
   1.236 +
   1.237 +				// If a debug build - record error
   1.238 +				#ifdef _DEBUG
   1.239 +					if (fileDeleteErr != KErrNone)
   1.240 +						{
   1.241 +						RDebug::Print(_L("Log::PruneLogFile - Failed to delete file. Error = %d"), fileDeleteErr);
   1.242 +						}
   1.243 +				#else
   1.244 +					(void)fileDeleteErr;
   1.245 +				#endif
   1.246 +				}	
   1.247 +			}
   1.248 +		}
   1.249 +	fsSession.Close();
   1.250 +	}
   1.251 +
   1.252 +#endif
   1.253 +