/*
 * Grabarz.h
 *
 * Author: Pawe Gala
 */

#ifndef GRABARZ_H_
#define GRABARZ_H_

#include <e32base.h>    // CActive
#include <f32file.h>    // RFs, RFile


class CGrabarz : public CActive
{
    public:        // Konstruktor dwufazowy i destruktor
        static CGrabarz* NewL( const TDesC& aPlik );
        ~CGrabarz();
        
    public:
        void Obserwuj();
        
    private:    // Odziedziczone z CActive
        void DoCancel();
        void RunL();
        
    private:    // Konstruktory drugiej fazy
        CGrabarz();
        void ConstructL( const TDesC& aPlik );
        
    private:    // Pola
        RUndertaker    iGrabarz;
        TInt iNrUchwytuDoZakonczonegoWatku;
        
        RFs iSystemPlikow;
        RFile iPlik;
};

#endif /* GRABARZ_H_ */

/*
 * Grabarz.cpp
 *
 * Autor: Pawe Gala
 */

#include "Grabarz.h"


CGrabarz* CGrabarz::NewL( const TDesC& aPlik )
{
    CGrabarz* self = new( ELeave ) CGrabarz();
    CleanupStack::PushL( self );
    self->ConstructL( aPlik );
    CleanupStack::Pop( self );
    return self;
}

CGrabarz::CGrabarz() : CActive( EPriorityStandard )
{
    CActiveScheduler::Add( this );
}

CGrabarz::~CGrabarz()
{
    Cancel();
    iGrabarz.Close();
    iPlik.Close();
    iSystemPlikow.Close();
}

void CGrabarz::ConstructL( const TDesC& aPlik )
{
    User::LeaveIfError( iGrabarz.Create() );
    
    TInt blad = iSystemPlikow.Connect();
    if( blad == KErrNone )
    {
        blad = iPlik.Replace( iSystemPlikow, aPlik, EFileWrite );
    }
    
    User::LeaveIfError( blad );
}

void CGrabarz::Obserwuj()
{
    if( IsActive() )
    {
        return;
    }
    
    iGrabarz.Logon( iStatus, iNrUchwytuDoZakonczonegoWatku );
    SetActive();
}

void CGrabarz::DoCancel()
{
    iGrabarz.LogonCancel();
}

void CGrabarz::RunL()
{
    _LIT8( KNowaLinia, "\r\n" );    
    TBuf8< 128 > bufor8;

    TInt blad = iStatus.Int();
    
    if( blad == KErrDied )
    {
        RThread martwyWatek;
        martwyWatek.SetHandle( iNrUchwytuDoZakonczonegoWatku );
        
        _LIT8( KTabulator, "\t" );
        TBuf16< 64 > bufor16;
        
        // 1) Aktualny czas
        _LIT( KLancuchFormatujacyCzas, "%-B%:0%J%:1%T%:2%S%.%*C4%:3%" );
        
        TTime czas;
        czas.FormatL( bufor16 , KLancuchFormatujacyCzas );
        bufor8.Copy( bufor16 );
        iPlik.Write( bufor8 );
        iPlik.Write( KTabulator );
        
        // 2) Nazwa procesu, SID oraz nazwa wtku
        bufor8.Copy( martwyWatek.FullName() );
        iPlik.Write( bufor8 );
        iPlik.Write( KNowaLinia );
    }
    else if( blad != KErrNone )
    {
        _LIT8( KFormat, "%S%d%S" );
        _LIT8( KKomunikat, "CGrabarz::RunL() : Bad = " );
        
        bufor8.Format( KFormat, &KKomunikat, blad, &KNowaLinia );
        iPlik.Write( bufor8 );
    }
    
    Obserwuj();
}

// ----------------
// Uycie klasy CGrabarz
// ----------------

_LIT( KLog, "c:\\data\\log.txt" );
iGrabarz = CGrabarz::NewL( KLog );
iGrabarz->Obserwuj();

//...
// Przykadowy wpis uzyskany na emulatorze:
// "12:47:38.0896    HelloWorld1[e77b46fb]0001::DrugiWatekProcesu"
// Wpis wykonany zosta wedug nastpujacego wzoru:
// "Czas    NazwaProcesu[SID]NumerInstancji::NazwaWtku"
