tsrc/xmltestharness/xmlclient/src/threadedlogger.cpp
changeset 0 0e4a32b9112d
equal deleted inserted replaced
-1:000000000000 0:0e4a32b9112d
       
     1 /*
       
     2 * Copyright (c) 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 "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 #include "threadedlogger.h"
       
    19 
       
    20 CThreadedLogger* CThreadedLogger::NewLC(MOmxScriptTestLogger& aRealLogger)
       
    21 	{
       
    22 	CThreadedLogger* self = new(ELeave) CThreadedLogger(aRealLogger);
       
    23 	CleanupStack::PushL(self);
       
    24 	self->ConstructL();
       
    25 	return self;
       
    26 	}
       
    27 
       
    28 CThreadedLogger::CThreadedLogger(MOmxScriptTestLogger& aRealLogger):
       
    29 CActive(EPriorityHigh),
       
    30 iRealLogger(aRealLogger)
       
    31 	{
       
    32 	iCreatorThreadId = iCreatorThread.Id();
       
    33 	}
       
    34 
       
    35 void CThreadedLogger::ConstructL()
       
    36 	{
       
    37 	User::LeaveIfError(iCreatorThread.Open(iCreatorThreadId));
       
    38 	User::LeaveIfError(iMutex.CreateLocal());
       
    39 	User::LeaveIfError(iSemaphore.CreateLocal(0));
       
    40 	CActiveScheduler::Add(this);
       
    41 	iStatus = KRequestPending;
       
    42 	SetActive();
       
    43 	}
       
    44 
       
    45 CThreadedLogger::~CThreadedLogger()
       
    46 	{
       
    47 	Cancel();
       
    48 	iSemaphore.Close();
       
    49 	iMutex.Close();
       
    50 	iCreatorThread.Close();
       
    51 	}
       
    52 
       
    53 void CThreadedLogger::Log(const TText8* aFile, TInt aLine, TOmxScriptSeverity aSeverity, const TDes& aMessage)
       
    54 	{
       
    55 	if(RThread().Id() == iCreatorThreadId)
       
    56 		{
       
    57 		iRealLogger.Log(aFile, aLine, aSeverity, aMessage);
       
    58 		}
       
    59 	else
       
    60 		{
       
    61 		// proxy the log to creator thread
       
    62 		
       
    63 		// mutex prevents multiple threads to proxy at the same time
       
    64 		iMutex.Wait();
       
    65 		
       
    66 		// const_cast is regrettable but all we do is pass the params onto a another MOmxScriptTestLogger which will
       
    67 		// also treat the args as const
       
    68 		iFile = const_cast<TText8*>(aFile);
       
    69 		iLine = aLine;
       
    70 		iSeverity = aSeverity;
       
    71 		iMessage = &const_cast<TDes&>(aMessage);
       
    72 		
       
    73 		// signal the creator thread (waking the Active Object)
       
    74 		TRequestStatus* statusPtr = &iStatus;
       
    75 		iCreatorThread.RequestComplete(statusPtr, KErrNone);
       
    76 		
       
    77 		// wait for creator thread to signal back
       
    78 		
       
    79 		iSemaphore.Wait();
       
    80 		iMutex.Signal();
       
    81 		}
       
    82 	}
       
    83 
       
    84 void CThreadedLogger::RunL()
       
    85 	{
       
    86 	if(iStatus.Int() == KErrCancel)
       
    87 		{
       
    88 		return;
       
    89 		}
       
    90 	iRealLogger.Log(iFile, iLine, iSeverity, *iMessage);
       
    91 	iFile = NULL;
       
    92 	iLine = 0;
       
    93 	iSeverity = (TOmxScriptSeverity) 0;
       
    94 	iMessage = NULL;
       
    95 	iStatus = KRequestPending;
       
    96 	SetActive();
       
    97 	iSemaphore.Signal();	// signal the requestor thread
       
    98 	}
       
    99 
       
   100 void CThreadedLogger::DoCancel()
       
   101 	{
       
   102 	TRequestStatus* statusPtr = &iStatus;
       
   103 	User::RequestComplete(statusPtr, KErrCancel);
       
   104 	}