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