loggingservices/eventlogger/LogCli/src/logservcli.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "logservcli.h"
       
    17 
       
    18 // System includes
       
    19 #include <e32math.h>
       
    20 #include <s32mem.h>
       
    21 
       
    22 // User includes
       
    23 #include "LogServShared.h"
       
    24 
       
    25 static TInt StartServer()
       
    26 //
       
    27 // Start the server process/thread which lives in an EPOCEXE object
       
    28 //
       
    29 	{
       
    30 	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
       
    31 
       
    32 	//
       
    33 	// EPOC is easy, we just create a new server process. Simultaneous launching
       
    34 	// of two such processes should be detected when the second one attempts to
       
    35 	// create the server object, failing with KErrAlreadyExists.
       
    36 	//
       
    37 	RProcess server;
       
    38 	TInt r=server.Create(KLogServerName,KNullDesC,serverUid);
       
    39 	if (r == KErrNone)
       
    40 		server.SetPriority(EPriorityForeground);
       
    41 		
       
    42 	if (r!=KErrNone)
       
    43 		return r;
       
    44 	
       
    45 	TRequestStatus stat;
       
    46 	server.Rendezvous(stat);
       
    47 	if (stat!=KRequestPending)
       
    48 		server.Kill(0);		// abort startup
       
    49 	else
       
    50 		server.Resume();	// logon OK - start the server
       
    51 	User::WaitForRequest(stat);		// wait for start or death
       
    52 	// we can't use the 'exit reason' if the server panicked as this
       
    53 	// is the panic 'reason' and may be '0' which cannot be distinguished
       
    54 	// from KErrNone
       
    55 	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
       
    56 	server.Close();
       
    57 	return r;
       
    58 
       
    59 	}
       
    60 
       
    61 // Initialise the operation and view ids
       
    62 RLogSession::RLogSession()
       
    63 : iOperationId(KLogNullOperationId + 1), iViewId(KLogNullViewId + 1)
       
    64 	{
       
    65 	}
       
    66 
       
    67 TInt RLogSession::Connect()
       
    68 //
       
    69 // Connect to the server, attempting to start it if necessary
       
    70 //
       
    71 	{
       
    72 	TInt retry=2;
       
    73 	for (;;)
       
    74 		{
       
    75 		TInt r=CreateSession(KLogServerFullName,TVersion(0,0,0));
       
    76 		if (r!=KErrNotFound && r!=KErrServerTerminated)
       
    77 			return r;
       
    78 		if (--retry==0)
       
    79 			return r;
       
    80 		r=StartServer();
       
    81 		if (r!=KErrNone && r!=KErrAlreadyExists)
       
    82 			return r;
       
    83 		}
       
    84 	}
       
    85 
       
    86 
       
    87 void RLogSession::Send(TInt aType, const TIpcArgs& aArgs, TRequestStatus& aStatus) const
       
    88 	{
       
    89 	SendWithRetryAsync(aType, aArgs, aStatus);
       
    90 	}
       
    91 
       
    92 TInt RLogSession::Send(TInt aType, const TIpcArgs& aArgs) const
       
    93 	{
       
    94 	return SendWithRetry(aType, aArgs);
       
    95 	}
       
    96 
       
    97 TInt RLogSession::SendWithRetry(TInt aType, const TIpcArgs& aParam) const
       
    98 	{
       
    99 	TInt ret = SendReceive(aType, aParam);
       
   100 	if (ret < KErrNone)
       
   101 		{
       
   102 		if(ret == KErrServerTerminated || ret == KErrNotFound)
       
   103 			{
       
   104 			// Try to reconnect
       
   105 			const_cast<RLogSession&>(*this).Close();
       
   106 			ret = const_cast<RLogSession&>(*this).Connect();
       
   107 			if(ret == KErrNone)
       
   108 				{
       
   109 				// Try to resend
       
   110 				ret = SendReceive(aType, aParam);
       
   111 				}
       
   112 			}
       
   113 		}
       
   114 	return ret;
       
   115 	}
       
   116 
       
   117 
       
   118 void RLogSession::SendWithRetryAsync(TInt aType, const TIpcArgs& aParam, TRequestStatus& aStatus) const
       
   119 	{
       
   120 	SendReceive(aType, aParam, aStatus);
       
   121 	}
       
   122 
       
   123 
       
   124 
       
   125 
       
   126 
       
   127 
       
   128 
       
   129 
       
   130 
       
   131 
       
   132 
       
   133 #ifdef LOGGING_ENABLED
       
   134 
       
   135 const TInt KLogEngLogBufferSize = 256;
       
   136 
       
   137 void Log::New()
       
   138 	{
       
   139 	_LIT(KNewLogText, "===== NEW LOG =====");
       
   140 	//
       
   141 	RFileLogger logger;
       
   142 	TInt ret=logger.Connect();
       
   143 	if	(ret==KErrNone)
       
   144 		{
       
   145 		logger.CreateLog(KLogFolder, KLogFileName, EFileLoggingModeOverwrite);
       
   146 		logger.Write(KNewLogText);
       
   147 		}
       
   148 	logger.Close();
       
   149 	}
       
   150 
       
   151 void Log::Write(const TDesC& aText)
       
   152 	{
       
   153 	PruneLogFile();
       
   154 
       
   155 	RFileLogger logger;
       
   156 	TInt ret=logger.Connect();
       
   157 	if (ret==KErrNone)
       
   158 		{
       
   159 		logger.SetDateAndTime(EFalse,EFalse);
       
   160 		logger.CreateLog(KLogFolder, KLogFileName,EFileLoggingModeAppend);
       
   161 		TBuf<KLogEngLogBufferSize> buf;
       
   162 		
       
   163 		// The debug log uses hometime rather than UTC for its timestamps. This is
       
   164 		// purely a debugging aid.
       
   165 		TTime now;
       
   166 		now.HomeTime();
       
   167 		
       
   168 		TDateTime dateTime;
       
   169 		dateTime = now.DateTime();
       
   170 		buf.Format(KTimeFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
       
   171 		buf.AppendFormat(KTextFormat,&aText);
       
   172 
       
   173 		logger.Write(buf);
       
   174 		}
       
   175 
       
   176 	logger.Close();
       
   177 	}
       
   178 
       
   179 void Log::WriteFormat(TRefByValue<const TDesC> aFmt,...)
       
   180 	{
       
   181     VA_LIST list;
       
   182     VA_START(list,aFmt);
       
   183 
       
   184 	PruneLogFile();
       
   185 
       
   186 	TBuf<2*KLogEngLogBufferSize> buf;
       
   187 	buf.SetMax();
       
   188 	buf.FillZ();
       
   189 	
       
   190 	// The debug log uses hometime rather than UTC for its timestamps. This is
       
   191 	// purely a debugging aid.
       
   192 	TTime now;
       
   193 	now.HomeTime();
       
   194 	
       
   195 	TDateTime dateTime;
       
   196 	dateTime = now.DateTime();
       
   197 	buf.Format(KTimeFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
       
   198 	buf.AppendFormatList(aFmt, list );
       
   199 
       
   200 	RFileLogger logger;
       
   201 	TInt ret=logger.Connect();
       
   202 	if (ret==KErrNone)
       
   203 		{
       
   204 		logger.SetDateAndTime(EFalse,EFalse);
       
   205 		logger.CreateLog(KLogFolder, KLogFileName,EFileLoggingModeAppend);
       
   206 		logger.Write(buf);
       
   207 		}
       
   208 
       
   209 	logger.Close();
       
   210 	}
       
   211 
       
   212 void Log::PruneLogFile()
       
   213 	{
       
   214 	const TInt KMaxLogSize = 1024 * 500;
       
   215 	//
       
   216 	_LIT(KBaseFolder, "_:\\Logs\\");
       
   217 	TFileName fileName(KBaseFolder);
       
   218 	fileName[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
       
   219 	fileName.Append(KLogFolder);
       
   220 	fileName.Append(KPathDelimiter);
       
   221 	fileName.Append(KLogFileName);
       
   222 	//
       
   223 	RFs fsSession;
       
   224 	if	(fsSession.Connect() == KErrNone)
       
   225 		{
       
   226 		TEntry entry;
       
   227 		if	(fsSession.Entry(fileName, entry) == KErrNone)
       
   228 			{
       
   229 			// Check size and delete if its too big
       
   230 			if (entry.iSize >= KMaxLogSize)
       
   231 				{
       
   232 				TInt fileDeleteErr=fsSession.Delete(fileName); 
       
   233 
       
   234 				// If a debug build - record error
       
   235 				#ifdef _DEBUG
       
   236 					if (fileDeleteErr != KErrNone)
       
   237 						{
       
   238 						RDebug::Print(_L("Log::PruneLogFile - Failed to delete file. Error = %d"), fileDeleteErr);
       
   239 						}
       
   240 				#else
       
   241 					(void)fileDeleteErr;
       
   242 				#endif
       
   243 				}	
       
   244 			}
       
   245 		}
       
   246 	fsSession.Close();
       
   247 	}
       
   248 
       
   249 #endif
       
   250