uifw/AvKon/tsrc/bc/S60_SDK3.0/bctestcmdlg/inc/StreamLogger.h
changeset 22 75713bee6484
parent 21 558113899881
child 26 62ef28f7b435
child 28 d33307312dfe
equal deleted inserted replaced
21:558113899881 22:75713bee6484
     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 // just for simple use,
       
    19 // this file doesn't following any code convensions or symbian rules.
       
    20 //
       
    21 
       
    22 #ifndef __DLL_LOGGER_H__
       
    23 #define __DLL_LOGGER_H__
       
    24 
       
    25 #include <f32file.h>  // link against efsrv.lib
       
    26 #include <eikenv.h>
       
    27 
       
    28 #define CERR    StreamLogger::begin()
       
    29 
       
    30 /**
       
    31 * usage: StreamLogger::begin()<<"your string"<<yourNumber<<...<<end;
       
    32 */
       
    33 class StreamLogger{
       
    34 
       
    35     //
       
    36     // internel class
       
    37     // use RAII instead of symbian 2 phases contruction.
       
    38     //
       
    39     class LogFile{
       
    40     public:
       
    41         LogFile()
       
    42         {
       
    43             _LIT( KLogFile, "C:\\debug.log" );
       
    44             RFs& fs = CEikonEnv::Static()->FsSession();
       
    45             if ( file.Open( fs, KLogFile, EFileWrite | EFileShareAny ) != KErrNone )
       
    46                 file.Create( fs, KLogFile, EFileWrite | EFileShareAny );
       
    47 
       
    48             TInt pos=0;  // this must be 0
       
    49             file.Seek( ESeekEnd, pos );
       
    50         }
       
    51 
       
    52         ~LogFile(){
       
    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         LogFile file;
       
    73 
       
    74         HBufC8* text = HBufC8::NewL( aText.Length() );
       
    75         TPtr8 textPtr = text->Des();
       
    76         textPtr.Copy( aText );
       
    77         file().Write( *text );
       
    78         delete text;
       
    79 
       
    80         return *this;
       
    81     }
       
    82 
       
    83     StreamLogger& operator<<(TInt n){
       
    84         LogFile file;
       
    85 
       
    86         TInt i=1;
       
    87         for(TInt v=n; v!=0; ++i, v/=10){}
       
    88         HBufC* text = HBufC::NewL( i );
       
    89         TPtr textPtr = text->Des();
       
    90 
       
    91         _LIT(KFmt, "%d");
       
    92         textPtr.Format( KFmt, n );
       
    93         HBufC8*  text8 = HBufC8::NewL( textPtr.Length() );
       
    94         TPtr8 textPtr8 = text8->Des();
       
    95         textPtr8.Copy(*text);
       
    96 
       
    97         file().Write( *text8 );
       
    98         delete text;
       
    99         delete text8;
       
   100 
       
   101         return *this;
       
   102     }
       
   103 
       
   104     StreamLogger& cr(){
       
   105         LogFile file;
       
   106         TBuf8<2> enter;
       
   107         enter.Append( 13 );
       
   108         enter.Append( 10 );
       
   109         file().Write( enter );
       
   110         return *this;
       
   111     }
       
   112 
       
   113     typedef StreamLogger& (*_Manipulator)(StreamLogger&);
       
   114     StreamLogger& operator<<(_Manipulator op){ return op(*this); }
       
   115 
       
   116 private:
       
   117     StreamLogger(){}    //disable ctor
       
   118 };
       
   119 
       
   120 inline StreamLogger& end(StreamLogger& self){ self.suicide(); return self; }
       
   121 
       
   122 inline StreamLogger& endl(StreamLogger& self){ self.cr(); return self; }
       
   123 
       
   124 #endif //__FILE_LOGGER_H__