uifw/AvKon/tsrc/bc/bctestlauncher/inc/StreamLogger.h
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Handles log writing.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef C_STREAMLOGGER_H
       
    20 #define C_STREAMLOGGER_H
       
    21 
       
    22 #include <f32file.h>  // link against efsrv.lib
       
    23 #include <eikenv.h>
       
    24 
       
    25 #define CERR    StreamLogger::begin()
       
    26 
       
    27 /**
       
    28  * usage: StreamLogger::begin()<<"your string"<<yourNumber<<...<<end;
       
    29  */
       
    30 class StreamLogger
       
    31     {
       
    32 
       
    33     //
       
    34     // internel class
       
    35     // use RAII instead of symbian 2 phases contruction.
       
    36     //
       
    37     class LogFile{
       
    38     public:
       
    39         LogFile()
       
    40             {
       
    41             _LIT( KLogFile, "c:\\debug.log" );
       
    42             RFs& fs = CEikonEnv::Static()->FsSession();
       
    43             if ( file.Open( fs, KLogFile, EFileWrite | EFileShareAny ) 
       
    44                 != KErrNone )
       
    45                 file.Create( fs, KLogFile, EFileWrite | EFileShareAny );
       
    46 
       
    47             TInt pos=0;  // this must be 0
       
    48             file.Seek( ESeekEnd, pos );
       
    49             }
       
    50 
       
    51         ~LogFile()
       
    52             {
       
    53             file.Close();
       
    54             }
       
    55 
       
    56         RFile& operator()(){ return file; }
       
    57     private:
       
    58         RFile file;
       
    59     };
       
    60 
       
    61 public:
       
    62     ~StreamLogger(){}
       
    63 
       
    64     static StreamLogger& begin(){
       
    65         StreamLogger* self = new (ELeave) StreamLogger;
       
    66         return *self;
       
    67     }
       
    68 
       
    69     void suicide(){ delete this; }
       
    70 
       
    71     StreamLogger& operator<<(const TDesC& aText)
       
    72         {
       
    73         LogFile file;
       
    74 
       
    75         HBufC8* text = HBufC8::NewL( aText.Length() );
       
    76         TPtr8 textPtr = text->Des();
       
    77         textPtr.Copy( aText );
       
    78         file().Write( *text );
       
    79         delete text;
       
    80 
       
    81         return *this;
       
    82         }
       
    83 
       
    84     StreamLogger& operator<<(TInt n)
       
    85         {
       
    86         LogFile file;
       
    87 
       
    88         TInt i=1;
       
    89         for(TInt v=n; v!=0; ++i, v/=10){}
       
    90         HBufC* text = HBufC::NewL( i );
       
    91         TPtr textPtr = text->Des();
       
    92 
       
    93         _LIT(KFmt, "%d");
       
    94         textPtr.Format( KFmt, n );
       
    95         HBufC8*  text8 = HBufC8::NewL( textPtr.Length() );
       
    96         TPtr8 textPtr8 = text8->Des();
       
    97         textPtr8.Copy(*text);
       
    98 
       
    99         file().Write( *text8 );
       
   100         delete text;
       
   101         delete text8;
       
   102 
       
   103         return *this;
       
   104         }
       
   105 
       
   106     StreamLogger& cr()
       
   107         {
       
   108         LogFile file;
       
   109         TBuf8<2> enter;
       
   110         enter.Append( 13 );
       
   111         enter.Append( 10 );
       
   112         file().Write( enter );
       
   113         return *this;
       
   114         }
       
   115 
       
   116     typedef StreamLogger& (*_Manipulator)(StreamLogger&);
       
   117     StreamLogger& operator<<(_Manipulator op){ return op(*this); }
       
   118 
       
   119 private:
       
   120     StreamLogger(){}    //disable ctor
       
   121 };
       
   122 
       
   123 inline StreamLogger& end(StreamLogger& self){ self.suicide(); return self; }
       
   124 
       
   125 inline StreamLogger& endl(StreamLogger& self){ self.cr(); return self; }
       
   126 
       
   127 #endif //C_STREAMLOGGER_H