devicediagnosticsfw/diagframework/src/diaglogeventeraser.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2007 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:  This is the implementation of the Diagnostics Log Event
       
    15 *                Eraser, which is used to delete OS log entries.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // System Include Files
       
    21 #include <e32base.h>                        // CActiveSchedulerWait
       
    22 #include <logcli.h>                         // CLogClient
       
    23 #include <logview.h>                        // CLogViewEvent
       
    24 #include <DiagFrameworkDebug.h>             // Debugging macros
       
    25 
       
    26 // Local Include Files
       
    27 #include "diaglogeventeraser.h"             // CDiagLogEventEraser
       
    28 #include "diaglogeventeraserobserver.h"     // MDiagLogEventEraserObserver
       
    29 
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS =============================
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // Static two-phase constructor.
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 EXPORT_C CDiagLogEventEraser* CDiagLogEventEraser::NewL(
       
    38     MDiagLogEventEraserObserver& aObserver,
       
    39     CLogFilter* aFilter )
       
    40     {
       
    41     LOGSTRING( "CDiagLogEventEraser::NewL()" )
       
    42 
       
    43     CleanupStack::PushL( aFilter );
       
    44     CDiagLogEventEraser* self = new ( ELeave ) CDiagLogEventEraser(
       
    45         aObserver,
       
    46         aFilter );
       
    47     CleanupStack::Pop( aFilter );
       
    48 
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop( self );
       
    52 
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Destructor.
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 EXPORT_C CDiagLogEventEraser::~CDiagLogEventEraser()
       
    61     {
       
    62     LOGSTRING( "CDiagLogEventEraser::~CDiagLogEventEraser()" )
       
    63 
       
    64     Cancel();
       
    65 
       
    66     delete iLogFilter;
       
    67     delete iLogView;
       
    68     delete iLogClient;
       
    69     delete iWaitScheduler;
       
    70 
       
    71     iFsSession.Close();
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Gets the filter to use when erasing logs.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C CLogFilter& CDiagLogEventEraser::Filter()
       
    79     {
       
    80     LOGSTRING( "CDiagLogEventEraser::Filter()" )
       
    81     return *iLogFilter;
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Gets the log client used for the log erasure.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 EXPORT_C CLogClient& CDiagLogEventEraser::LogClient()
       
    89     {
       
    90     LOGSTRING( "CDiagLogEventEraser::LogClient()" )
       
    91     return *iLogClient;
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // Begins asynchronous erasure of logs.  The observer will be notified of
       
    96 // completion via callback.
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C void CDiagLogEventEraser::StartAsyncEraseLogsL()
       
   100     {
       
   101     LOGSTRING( "CDiagLogEventEraser::StartAsyncEraseLogsL()" )
       
   102 
       
   103     __ASSERT_DEBUG( iState == EStateInitial, User::Invariant() );
       
   104 
       
   105     // Apply the filter.
       
   106     if( iLogView->SetFilterL( *iLogFilter, iStatus ) )
       
   107         {
       
   108         iState = EStateApplyingFilter;
       
   109         iIsDeletingSynchronous = EFalse;
       
   110         SetActive();
       
   111         }
       
   112 
       
   113     // There are no logs, so set this object active so that the callback will
       
   114     // be asynchronous.
       
   115     else
       
   116         {
       
   117         iState = EStateComplete;
       
   118         TRequestStatus* status = &iStatus;
       
   119         User::RequestComplete( status, KErrNone );
       
   120         SetActive();
       
   121         }
       
   122     }
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // Begins synchronous erasure of logs.
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C void CDiagLogEventEraser::EraseLogsL()
       
   129     {
       
   130     LOGSTRING( "CDiagLogEventEraser::EraseLogsL()" )
       
   131 
       
   132     __ASSERT_DEBUG( iState == EStateInitial, User::Invariant() );
       
   133 
       
   134     iError = KErrNone;
       
   135 
       
   136     // Apply the filter.  iWaitScheduler->Start() will return after the wait
       
   137     // scheduler is stopped.
       
   138     if( iLogView->SetFilterL( *iLogFilter, iStatus ) )
       
   139         {
       
   140         iState = EStateApplyingFilter;
       
   141         iIsDeletingSynchronous = ETrue;
       
   142         SetActive();
       
   143         iWaitScheduler->Start();
       
   144         }
       
   145 
       
   146     // If there is an error, iError will be set when the active object is run.
       
   147     User::LeaveIfError( iError );
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // The default constructor.
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 CDiagLogEventEraser::CDiagLogEventEraser(
       
   155     MDiagLogEventEraserObserver& aObserver,
       
   156     CLogFilter* aFilter )
       
   157 :   CActive( EPriorityStandard ),
       
   158     iObserver( aObserver ),
       
   159     iLogFilter( aFilter )
       
   160     {
       
   161     LOGSTRING( "CDiagLogEventEraser::CDiagLogEventEraser()" )
       
   162     CActiveScheduler::Add( this );
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // The second phase constructor.
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CDiagLogEventEraser::ConstructL()
       
   170     {
       
   171     LOGSTRING( "CDiagLogEventEraser::ConstructL()" )
       
   172 
       
   173     User::LeaveIfError( iFsSession.Connect() );
       
   174     iWaitScheduler = new( ELeave ) CActiveSchedulerWait;
       
   175     iLogClient = CLogClient::NewL( iFsSession );
       
   176     iLogView = CLogViewEvent::NewL( *iLogClient );
       
   177     if ( !iLogFilter )
       
   178         {
       
   179         iLogFilter = CLogFilter::NewL();
       
   180         }
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // Handles the completion of log erasing and performs different operations
       
   185 // depending on whether the erasing was asynchronous or not.
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CDiagLogEventEraser::HandleCompletionL( TInt aError )
       
   189     {
       
   190     LOGSTRING2( "CDiagLogEventEraser::HandleCompletionL( %d )", aError )
       
   191 
       
   192     // Reset the state so that the object can be reused.
       
   193     iState = EStateInitial;
       
   194 
       
   195     // For synchronous erasure, set the error code and stop the wait
       
   196     // scheduler, so that it will return in EraseLogsL.
       
   197     if ( iIsDeletingSynchronous )
       
   198         {
       
   199         iError = aError;
       
   200         iWaitScheduler->AsyncStop();
       
   201         }
       
   202 
       
   203     // For asynchronous erasure, inform the observer that erasing has
       
   204     // completed.
       
   205     else
       
   206         {
       
   207         iObserver.CompleteEventEraseL( aError );
       
   208         }
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // Handles the completion of active requests.
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CDiagLogEventEraser::RunL()
       
   216     {
       
   217     LOGSTRING( "CDiagLogEventEraser::RunL()" )
       
   218 
       
   219     // Check that the request completed successfully.
       
   220     if( iStatus != KErrNone )
       
   221         {
       
   222         HandleCompletionL( iStatus.Int() );
       
   223         return;
       
   224         }
       
   225 
       
   226     LOGSTRING2( "CDiagLogEventEraser::RunL() - State %d", iState )
       
   227     switch( iState )
       
   228         {
       
   229         case EStateApplyingFilter:
       
   230             {
       
   231             // The log view filter has been successfully applied.  Issue
       
   232             // another request to start processing log events.  Events are
       
   233             // read from last to first.  The reason for this arrangement is
       
   234             // that we are deleting entries while reading them.
       
   235             if( iLogView->LastL( iStatus ) )
       
   236                 {
       
   237                 iState = EStateReadingEntries;
       
   238                 SetActive();
       
   239                 }
       
   240 
       
   241             // There are no events in the view.
       
   242             else
       
   243                 {
       
   244                 HandleCompletionL( KErrNone );
       
   245                 }
       
   246 
       
   247             break;
       
   248             }
       
   249 
       
   250         case EStateReadingEntries:
       
   251             {
       
   252             const CLogEvent& event = iLogView->Event();
       
   253 
       
   254             // The log event has been fetched successfully.  Ask the observer
       
   255             // if this event should be erased.  If so, issue another request
       
   256             // to begin erasing the event.
       
   257             if( iObserver.IsEventToBeDeleted( event ) )
       
   258                 {
       
   259                 iState = EStateDeletingEntry;
       
   260                 iLogClient->DeleteEvent( event.Id(), iStatus );
       
   261                 SetActive();
       
   262                 }
       
   263 
       
   264             // The log event should not be erased, so get the next event.
       
   265             else if( iLogView->PreviousL( iStatus ) )
       
   266                 {
       
   267                 SetActive();
       
   268                 }
       
   269 
       
   270             // There are no events in the view.
       
   271             else
       
   272                 {
       
   273                 HandleCompletionL( KErrNone );
       
   274                 }
       
   275 
       
   276             break;
       
   277             }
       
   278 
       
   279         case EStateDeletingEntry:
       
   280             {
       
   281             // The log event was successfully deleted.  Issue another request
       
   282             // to read the next entry.
       
   283             if( iLogView->PreviousL( iStatus ) )
       
   284                 {
       
   285                 iState = EStateReadingEntries;
       
   286                 SetActive();
       
   287                 }
       
   288 
       
   289             // There are no events in the view.
       
   290             else
       
   291                 {
       
   292                 HandleCompletionL( KErrNone );
       
   293                 }
       
   294 
       
   295             break;
       
   296             }
       
   297 
       
   298         case EStateComplete:
       
   299             {
       
   300             HandleCompletionL( KErrNone );
       
   301             break;
       
   302             }
       
   303 
       
   304         default:
       
   305             // This state should never be reached.
       
   306             LOGSTRING( "CDiagLogEventEraser::RunL() - Invalid State." )
       
   307             User::Invariant();
       
   308             break;
       
   309         }
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // Handles the cancellation of active requests.
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 void CDiagLogEventEraser::DoCancel()
       
   317     {
       
   318     LOGSTRING( "CDiagLogEventEraser::DoCancel()" )
       
   319 
       
   320     iLogView->Cancel();
       
   321     iLogClient->Cancel();
       
   322     iState = EStateInitial;
       
   323     }
       
   324 
       
   325 // End of file