sysanadatacapture/piprofiler/piprofiler/plugins/DiskWriterPlugin/src/DiskWriterPlugin.cpp
changeset 2 6a82cd05fb1e
parent 1 3ff3fecb12fe
equal deleted inserted replaced
1:3ff3fecb12fe 2:6a82cd05fb1e
     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 
       
    19 //  Include Files  
       
    20 
       
    21 #include "DiskWriterPlugin.h"	// CDiskWriterPlugin
       
    22 #include <e32base.h>
       
    23 #include <sysutil.h>
       
    24 #include <piprofiler/EngineUIDs.h>
       
    25 
       
    26 // constants
       
    27 //const TInt KDelayBeforeTimerStarts( 5000000 );
       
    28 
       
    29 // engine properties
       
    30 const TUid KEngineStatusPropertyCat={0x2001E5AD};
       
    31 enum TEnginePropertyKeys
       
    32     {
       
    33     EProfilerEngineStatus = 8,
       
    34     EProfilerErrorStatus
       
    35     };
       
    36 
       
    37 /*
       
    38  *
       
    39  *	Class CDiskWriterPlugin implementation
       
    40  *
       
    41  */
       
    42 
       
    43 //  Member Functions
       
    44 CDiskWriterPlugin* CDiskWriterPlugin::NewL(const TUid /*aImplementationUid*/, TAny* /*aInitParams*/)
       
    45     {
       
    46 	LOGTEXT(_L("CDiskWriterPlugin::NewL() - entry"));
       
    47 	CDiskWriterPlugin* self = new (ELeave) CDiskWriterPlugin(KDiskWriterPluginUid);
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop();
       
    51 	LOGTEXT(_L("CDiskWriterPlugin::NewL() - exit"));
       
    52     return self;
       
    53     }
       
    54 
       
    55 CDiskWriterPlugin::CDiskWriterPlugin(const TUid aImplementationUid) :
       
    56 	iWriterType(aImplementationUid.iUid)
       
    57     {
       
    58     LOGTEXT(_L("CDiskWriterPlugin::CDiskWriterPlugin - entry"));
       
    59     isEnabled = EFalse;
       
    60     iWriterId = Id().iUid;
       
    61     LOGTEXT(_L("CDiskWriterPlugin::CDiskWriterPlugin - exit"));
       
    62     }
       
    63 
       
    64 CDiskWriterPlugin::~CDiskWriterPlugin()
       
    65     {
       
    66     LOGTEXT(_L("CDiskWriterPlugin::~CDiskWriterPlugin - entry"));
       
    67 
       
    68     iErrorStatus.Close();
       
    69     
       
    70     if(iWriterHandler)
       
    71         {
       
    72         iWriterHandler->Cancel();
       
    73         delete iWriterHandler;
       
    74         }
       
    75     LOGTEXT(_L("CDiskWriterPlugin::~CDiskWriterPlugin - exit"));
       
    76     }
       
    77 
       
    78 void CDiskWriterPlugin::ConstructL()
       
    79 	{
       
    80 	// second phase constructor, anything that may leave must be constructed here
       
    81 	LOGTEXT(_L("CDiskWriterPlugin::ConstructL() - entry"));
       
    82 	iWriterHandler = CDiskWriterHandler::NewL(this);
       
    83 	User::LeaveIfError(iErrorStatus.Attach(KEngineStatusPropertyCat, EProfilerErrorStatus));
       
    84 	LOGTEXT(_L("CDiskWriterPlugin::ConstructL() - exit"));
       
    85 	}
       
    86 
       
    87 void CDiskWriterPlugin::GetCaption( TDes& aCaption ) const
       
    88 	{
       
    89     aCaption.Copy(KFileOutShortName);
       
    90     aCaption.TrimRight();
       
    91     LOGSTRING2("CDiskWriterPlugin::GetCaptionL() - Plugin name: %S", &aCaption);
       
    92 	}
       
    93 
       
    94 TUid CDiskWriterPlugin::Id() const 
       
    95 	{
       
    96     LOGSTRING2("CDiskWriterPlugin::Id():0x%X", KDiskWriterPluginUid.iUid );
       
    97     return KDiskWriterPluginUid;
       
    98 	}
       
    99 	 
       
   100 void CDiskWriterPlugin::GetWriterVersion(TDes* aDes)
       
   101 	{
       
   102 	_LIT(KDiskWriterVersion, "1.0.0");
       
   103 	aDes->Append(KDiskWriterVersion);
       
   104 	}
       
   105 
       
   106 TInt CDiskWriterPlugin::Start()
       
   107 	{
       
   108 //	if(isEnabled)
       
   109 //		{
       
   110 //		TRAPD(err, iWriterHandler->StartL());
       
   111 //		if( err != KErrNone)
       
   112 //		    {
       
   113 //		    LOGTEXT(_L("Could not start writer plugin"));
       
   114 //		    return err;
       
   115 //		    }
       
   116 //		}
       
   117 	return KErrNone;
       
   118 	}
       
   119 
       
   120 void CDiskWriterPlugin::Stop()
       
   121 	{
       
   122 	// stop writer handler normally
       
   123 	iWriterHandler->Stop();
       
   124 	}
       
   125 
       
   126 TBool CDiskWriterPlugin::GetEnabled()
       
   127 	{
       
   128 	return isEnabled;
       
   129 	}
       
   130 
       
   131 TUint32 CDiskWriterPlugin::GetWriterType()
       
   132 	{
       
   133 	return iWriterType;
       
   134 	}
       
   135 
       
   136 
       
   137 void CDiskWriterPlugin::SetValue( const TWriterPluginValueKeys aKey,
       
   138                                     TDes& aValue )
       
   139     {
       
   140     TRAP_IGNORE( SetValueL( aKey, aValue ) );
       
   141     }
       
   142 
       
   143 
       
   144 void CDiskWriterPlugin::GetValue( const TWriterPluginValueKeys aKey,
       
   145                                     TDes& aValue )
       
   146     {
       
   147     TRAP_IGNORE( GetValueL( aKey, aValue ) );
       
   148     }
       
   149 
       
   150 void CDiskWriterPlugin::SetValueL( const TWriterPluginValueKeys aKey, TDes& aValue )
       
   151     {
       
   152     TInt error(KErrNone);
       
   153     
       
   154     switch( aKey )
       
   155         {
       
   156         case EWriterPluginEnabled:
       
   157             isEnabled = ETrue;
       
   158         	LOGTEXT(_L("CDebOutWriterPlugin::SetValueL - plugin enabled"));
       
   159         	break;
       
   160         case EWriterPluginDisabled:
       
   161             isEnabled = EFalse;
       
   162         	LOGTEXT(_L("CDebOutWriterPlugin::SetValueL - plugin disabled"));	
       
   163             break;
       
   164         case EWriterPluginSettings:	// file name in case of disk writer plugin
       
   165         	iFileName.Zero();
       
   166         	iFileName.Append(aValue);
       
   167         	error = iWriterHandler->TestFile(iFileName);
       
   168         		User::LeaveIfError(error);
       
   169         	break;
       
   170         default:
       
   171         	break;
       
   172         }
       
   173     }
       
   174 
       
   175 void CDiskWriterPlugin::GetValueL( const TWriterPluginValueKeys aKey, TDes& aValue )
       
   176     {
       
   177     switch( aKey )
       
   178         {
       
   179         case EWriterPluginVersion:
       
   180         	GetWriterVersion(&aValue);
       
   181         	break;
       
   182         case EWriterPluginType:
       
   183         	break;
       
   184         case EWriterPluginSettings:	// file name in disk writer case
       
   185         	aValue.Copy(iFileName);
       
   186            default:
       
   187                 break;
       
   188         }
       
   189     }
       
   190 
       
   191 void CDiskWriterPlugin::DoCancel()
       
   192     {
       
   193 	LOGTEXT(_L("CDebOutWriterPlugin::DoCancel - entry"));
       
   194     }
       
   195 
       
   196 void CDiskWriterPlugin::WriteData()
       
   197     {
       
   198     // Activate handler to write data from buffer to output
       
   199     LOGTEXT(_L("CDiskWriterPlugin::WriteData() - entry"));
       
   200     iWriterHandler->StartL();
       
   201     LOGTEXT(_L("CDiskWriterPlugin::WriteData() - exit"));
       
   202     }
       
   203 
       
   204 void CDiskWriterPlugin::HandleError(TInt aError)
       
   205     {
       
   206     TInt err(KErrNone);
       
   207     RDebug::Print(_L("CDiskWriterPlugin::HandleError() - error received: %d"), aError);
       
   208     err = iErrorStatus.Set(aError);
       
   209     if(err != KErrNone)
       
   210         {
       
   211         RDebug::Print(_L("CDiskWriterPlugin::HandleError() - error in updating property: %d"), err);
       
   212         }
       
   213     }
       
   214 
       
   215 /*
       
   216  * 
       
   217  * Implementation of class CDiskWriterHandler
       
   218  * 
       
   219  */
       
   220 
       
   221 CDiskWriterHandler* CDiskWriterHandler::NewL(CDiskWriterPlugin* aWriter)
       
   222     {
       
   223 	LOGTEXT(_L("CDiskWriterHandler::NewL() - entry"));
       
   224 	CDiskWriterHandler* self = new (ELeave) CDiskWriterHandler(aWriter);
       
   225     CleanupStack::PushL( self );
       
   226     self->ConstructL();
       
   227     CleanupStack::Pop();
       
   228 	LOGTEXT(_L("CDiskWriterHandler::NewL() - exit"));
       
   229     return self;
       
   230     }
       
   231 
       
   232 CDiskWriterHandler::CDiskWriterHandler(CDiskWriterPlugin* aWriter)  :
       
   233     CActive(EPriorityStandard)
       
   234     {
       
   235     LOGTEXT(_L("CDiskWriterHandler::CDiskWriterHandler - entry"));
       
   236 
       
   237     iWriter = aWriter;
       
   238     
       
   239     // set initial mode to non-stopping
       
   240     iStopping = EFalse;
       
   241     
       
   242     // add the handler to the active scheduler
       
   243     CActiveScheduler::Add(this);
       
   244     
       
   245     LOGTEXT(_L("CDiskWriterHandler::CDiskWriterHandler - exit"));
       
   246     }
       
   247 
       
   248 
       
   249 CDiskWriterHandler::~CDiskWriterHandler()
       
   250     {
       
   251 	LOGTEXT(_L("CDiskWriterHandler::~CDiskWriterHandler - entry"));
       
   252 
       
   253 	LOGTEXT(_L("CDiskWriterHandler::~CDiskWriterHandler - exit"));
       
   254     }    
       
   255     
       
   256 void CDiskWriterHandler::ConstructL()
       
   257 	{
       
   258 	}
       
   259 
       
   260 TInt CDiskWriterHandler::TestFile(const TDesC& totalPrefix) 
       
   261     {
       
   262     TParse parse;
       
   263 
       
   264     TInt err(KErrNone);
       
   265     if((err = parse.Set(totalPrefix, NULL, NULL)) != KErrNone)
       
   266         return err;
       
   267 
       
   268     err = iFs.Connect();
       
   269     if(err != KErrNone)
       
   270         {
       
   271         LOGTEXT(_L("CDiskWriterHandler::TestFile() - Failed to open a session to file server"));
       
   272         return KErrNotFound;
       
   273         }
       
   274     
       
   275     iFs.MkDirAll(parse.FullName());
       
   276     
       
   277     err = iFile.Replace(iFs,parse.FullName(),EFileWrite);
       
   278     if(err != KErrNone)
       
   279         {
       
   280         iFs.Close();
       
   281         return KErrNotFound;
       
   282         }
       
   283     
       
   284     iFileName.Copy(parse.FullName());
       
   285 
       
   286     return KErrNone;
       
   287     }
       
   288 
       
   289 void CDiskWriterHandler::Reset()
       
   290     {
       
   291     // cancel active object
       
   292     Cancel();
       
   293     
       
   294     // start writing new buffer if there is one available
       
   295     TBapBuf* nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   296     
       
   297     // empty the rest of the buffers synchronously
       
   298     while(nextBuf != 0)
       
   299         {
       
   300         LOGTEXT(_L("CDiskWriterHandler::Reset - writing to file"));
       
   301         if(nextBuf->iDataSize != 0)
       
   302             {
       
   303             LOGTEXT(_L("CDiskWriterHandler::Reset - writing to file"));
       
   304             WriteMemBufferToFile(*(nextBuf->iBufDes),iStatus);
       
   305             }
       
   306         
       
   307         // empty buffers when profiling stopped
       
   308         iWriter->iStream->AddToFreeBuffers(nextBuf);
       
   309 
       
   310         LOGTEXT(_L("CDiskWriterHandler::Reset - get next full buffer"));
       
   311         // start writing new buffer if there is one available
       
   312         nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   313         LOGSTRING2(_L("CDiskWriterHandler::Reset - got next filled 0x%x"),nextBuf);
       
   314         }
       
   315     }
       
   316 
       
   317 void CDiskWriterHandler::HandleFullBuffers()
       
   318     {
       
   319     LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - entry"));
       
   320     // previous write operation has finished
       
   321     // release the previous buffer 
       
   322     iWriter->iStream->AddToFreeBuffers(iBufferBeingWritten);
       
   323 
       
   324     LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - get next full buffer"));
       
   325     // start writing new buffer if there is one available
       
   326     TBapBuf* nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   327 
       
   328     if(nextBuf != 0)
       
   329         {
       
   330         LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - writing to file"));
       
   331         if(nextBuf->iDataSize != 0)
       
   332             {
       
   333             WriteBufferToOutput(nextBuf);
       
   334             }
       
   335         } 
       
   336     LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - exit"));
       
   337     }
       
   338 
       
   339 void CDiskWriterHandler::RunL()
       
   340     {
       
   341     LOGTEXT(_L("CDiskWriterHandler::RunL - entry"));
       
   342     // call function to complete full buffer handling
       
   343     HandleFullBuffers();
       
   344     LOGTEXT(_L("CDiskWriterHandler::RunL - exit"));
       
   345     }
       
   346 
       
   347 void CDiskWriterHandler::DoCancel()
       
   348     {
       
   349     
       
   350     }
       
   351 
       
   352 //-----------------------------------------------------------------------------
       
   353 // CPIProfilerTraceCoreLauncher::RunError(TInt aError)
       
   354 // Handle leaves from RunL().
       
   355 //-----------------------------------------------------------------------------
       
   356 TInt CDiskWriterHandler::RunError(TInt aError)
       
   357     {
       
   358     // no reason to continue if disk full or removed
       
   359     iFile.Close();
       
   360     // close handle to file server too
       
   361     iFs.Close();
       
   362     iFileName.Zero();
       
   363     
       
   364     iWriter->HandleError(KErrDiskFull);
       
   365     return aError;
       
   366     }
       
   367 
       
   368 void CDiskWriterHandler::WriteMemBufferToFile(TDesC8& aDes, TRequestStatus& aStatus)
       
   369     {   
       
   370     LOGTEXT(_L("CDiskWriterPlugin::WriteMemBufferToFile - entry"));
       
   371 
       
   372     TUint sampleSize(aDes.Length());
       
   373     TInt err(KErrNone);
       
   374     TInt drive(0);
       
   375     TDriveInfo info;
       
   376     
       
   377     err = iFile.Drive(drive,info);
       
   378     // check first if still space on disk
       
   379     if( SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, sampleSize, drive) )
       
   380         {
       
   381         // set error to disk full
       
   382         err = KErrDiskFull;
       
   383         LOGTEXT(_L("CDiskWriterPlugin::WriteMemBufferToFile - disk full, cannot write"));
       
   384         }
       
   385     else
       
   386         {
       
   387         // check if profiling in stopping mode
       
   388         if(iStopping)
       
   389             {
       
   390             // RDebug::Print(_L("CDiskWriterPlugin::WriteMemBufferToFile - data written, length %d, stopping"), aDes.Length());
       
   391             // write to file without status
       
   392             err = iFile.Write(aDes);
       
   393             }
       
   394         else
       
   395             {
       
   396             // RDebug::Print(_L("CDiskWriterPlugin::WriteMemBufferToFile - data written, length %d"), aDes.Length());
       
   397             // write to file with status
       
   398             iFile.Write(aDes,aStatus);
       
   399             }
       
   400         }
       
   401     
       
   402     // check if error in write
       
   403     if(err != KErrNone)
       
   404         {
       
   405         // stop writer handler (and its timer) immediately, DO NOT try write data!
       
   406         Cancel();
       
   407         
       
   408         // no reason to continue if disk full or removed
       
   409         // end of stream detected, file can be closed
       
   410         iFile.Close();
       
   411         // close handle to file server too
       
   412         iFs.Close();
       
   413         iFileName.Zero();
       
   414         
       
   415         // set error status for engine to read
       
   416         iWriter->HandleError(err);
       
   417         }
       
   418     LOGTEXT(_L("CDiskWriterPlugin::WriteMemBufferToFile - exit"));
       
   419     }
       
   420 
       
   421 void CDiskWriterHandler::WriteBufferToOutput(TBapBuf* aBuf)
       
   422     {
       
   423     LOGTEXT(_L("CDiskWriterPlugin::WriteBufferToOutput - entry"));
       
   424     iBufferBeingWritten = aBuf;
       
   425 
       
   426     // set the data length just to be sure
       
   427     iBufferBeingWritten->iBufDes->SetLength(aBuf->iDataSize);
       
   428 
       
   429     LOGTEXT(_L("CDiskWriterPlugin::WriteBufferToOutput - writing to file"));
       
   430     WriteMemBufferToFile(*(iBufferBeingWritten->iBufDes), iStatus);
       
   431 
       
   432     // set AO back to active, until filled buffers are emptied 
       
   433     SetActive();
       
   434     
       
   435     LOGTEXT(_L("CDiskWriterPlugin::WriteBufferToOutput - exit"));
       
   436     }
       
   437 
       
   438 void CDiskWriterHandler::StartL()
       
   439 	{
       
   440 	LOGTEXT(_L("CDiskWriterHandler::StartL - entry"));
       
   441     if(!IsActive())
       
   442         {
       
   443         LOGTEXT(_L("CDiskWriterHandler::StartL - is not active"));
       
   444     
       
   445         TBapBuf* nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   446         LOGSTRING2(_L("CDiskWriterHandler::StartL - got next filled 0x%x"),nextBuf);
       
   447     
       
   448         if(nextBuf != 0)
       
   449             {
       
   450             LOGTEXT(_L("CDiskWriterHandler::StartL - writing to file"));
       
   451             WriteBufferToOutput(nextBuf);
       
   452             }
       
   453         }
       
   454     LOGTEXT(_L("CDiskWriterHandler::StartL - exit"));
       
   455 	}
       
   456 
       
   457 void CDiskWriterHandler::Stop()
       
   458 	{
       
   459 	LOGTEXT(_L("CDiskWriterHandler::Stop - entry"));
       
   460 	
       
   461 	// set to stopping mode, needed for emptying the remaining full buffers
       
   462 	iStopping = ETrue;
       
   463 	
       
   464 	// stop the timer
       
   465 	Reset();
       
   466 
       
   467     // end of stream detected, file can be closed
       
   468     iFile.Close();
       
   469     // close handle to file server too
       
   470     iFs.Close();
       
   471     iFileName.Zero();
       
   472     
       
   473     // set mode back to non-stopping
       
   474     iStopping = EFalse;
       
   475     
       
   476     LOGTEXT(_L("CDiskWriterHandler::Stop - exit"));
       
   477 	}
       
   478 
       
   479 // end of file