|
1 /* |
|
2 * Copyright (c) 2006-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: Handles log writing. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <f32file.h> |
|
20 #include <eikenv.h> |
|
21 |
|
22 #include "bcteststrmlogger.h" |
|
23 |
|
24 // |
|
25 // helper class |
|
26 // |
|
27 class CLogFile: public CBase |
|
28 { |
|
29 public: |
|
30 static CLogFile& CreateLC( const TDesC& aFileName ); |
|
31 virtual ~CLogFile(); |
|
32 RFile& operator()(); |
|
33 |
|
34 private: |
|
35 RFile iFile; |
|
36 }; |
|
37 |
|
38 //=====================Helper class Member functions========================== |
|
39 |
|
40 // --------------------------------------------------------------------------- |
|
41 // CLogFile::CreateLC() |
|
42 // --------------------------------------------------------------------------- |
|
43 // |
|
44 CLogFile& CLogFile::CreateLC( const TDesC& aFileName ) |
|
45 { |
|
46 CLogFile* self = new( ELeave ) CLogFile; |
|
47 CleanupStack::PushL( self ); |
|
48 |
|
49 RFs& fs = CEikonEnv::Static()->FsSession(); |
|
50 if ( self->iFile.Open( fs, aFileName, EFileWrite | EFileShareAny ) |
|
51 != KErrNone ) |
|
52 { |
|
53 self->iFile.Create( fs, aFileName, EFileWrite | EFileShareAny ); |
|
54 } |
|
55 |
|
56 TInt pos=0; // this must be 0 |
|
57 self->iFile.Seek( ESeekEnd, pos ); |
|
58 return *self; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------------- |
|
62 // CLogFile::~CLogFile() |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 CLogFile::~CLogFile() |
|
66 { |
|
67 iFile.Close(); |
|
68 } |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // CLogFile::operator() |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 RFile& CLogFile::operator()() |
|
75 { |
|
76 return iFile; |
|
77 } |
|
78 |
|
79 //=====================Member functions======================================= |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // CBCTestStreamLogger::BeginLC() |
|
83 // --------------------------------------------------------------------------- |
|
84 // |
|
85 CBCTestStreamLogger& CBCTestStreamLogger::BeginLC( const TDesC& aFileName ) |
|
86 { |
|
87 CBCTestStreamLogger* self = new( ELeave ) CBCTestStreamLogger(); |
|
88 CleanupStack::PushL( self ); |
|
89 self->ConstructL( aFileName ); |
|
90 return *self; |
|
91 } |
|
92 |
|
93 // --------------------------------------------------------------------------- |
|
94 // CBCTestStreamLogger::CBCTestStreamLogger() |
|
95 // --------------------------------------------------------------------------- |
|
96 // |
|
97 CBCTestStreamLogger::CBCTestStreamLogger() |
|
98 { |
|
99 } |
|
100 |
|
101 // --------------------------------------------------------------------------- |
|
102 // CBCTestStreamLogger::ConstructL() |
|
103 // --------------------------------------------------------------------------- |
|
104 // |
|
105 void CBCTestStreamLogger::ConstructL( const TDesC& aFileName ) |
|
106 { |
|
107 iFileName = aFileName; |
|
108 } |
|
109 |
|
110 // --------------------------------------------------------------------------- |
|
111 // CBCTestStreamLogger::~CBCTestStreamLogger() |
|
112 // --------------------------------------------------------------------------- |
|
113 // |
|
114 CBCTestStreamLogger::~CBCTestStreamLogger() |
|
115 { |
|
116 } |
|
117 |
|
118 // --------------------------------------------------------------------------- |
|
119 // CBCTestStreamLogger::Suicide() |
|
120 // --------------------------------------------------------------------------- |
|
121 // |
|
122 void CBCTestStreamLogger::Suicide() |
|
123 { |
|
124 CleanupStack::PopAndDestroy(); //self |
|
125 } |
|
126 |
|
127 // --------------------------------------------------------------------------- |
|
128 // CBCTestStreamLogger::operator<<(const TDesC& aText) |
|
129 // --------------------------------------------------------------------------- |
|
130 // |
|
131 CBCTestStreamLogger& CBCTestStreamLogger::operator<<( const TDesC& aText ) |
|
132 { |
|
133 CLogFile& file = CLogFile::CreateLC( iFileName ); |
|
134 HBufC8* text = NULL; |
|
135 TRAPD( result, text = HBufC8::NewL( aText.Length() ) ); |
|
136 if ( result != KErrNone ) |
|
137 { |
|
138 User::Leave( result ); |
|
139 } |
|
140 TPtr8 textPtr = text->Des(); |
|
141 textPtr.Copy( aText ); |
|
142 file().Write( *text ); |
|
143 delete text; |
|
144 CleanupStack::PopAndDestroy(); //file |
|
145 |
|
146 return *this; |
|
147 } |
|
148 |
|
149 // --------------------------------------------------------------------------- |
|
150 // CBCTestStreamLogger::operator<<(TInt aNum) |
|
151 // --------------------------------------------------------------------------- |
|
152 // |
|
153 CBCTestStreamLogger& CBCTestStreamLogger::operator<<( TInt aNum ) |
|
154 { |
|
155 CLogFile& file = CLogFile::CreateLC( iFileName ); |
|
156 |
|
157 TInt i = 1; |
|
158 for( TInt v = aNum; v != 0; ++i, v /= 10 ) |
|
159 { |
|
160 } |
|
161 HBufC* text = NULL; |
|
162 TRAPD( result, text = HBufC::NewL( i ) ); |
|
163 TPtr textPtr = text->Des(); |
|
164 |
|
165 _LIT(KFmt, "%d"); |
|
166 textPtr.Format( KFmt, aNum ); |
|
167 HBufC8* text8 = NULL; |
|
168 TRAP( result, text8 = HBufC8::NewL( textPtr.Length() ) ); |
|
169 if ( result != KErrNone ) |
|
170 { |
|
171 User::Leave( result ); |
|
172 } |
|
173 TPtr8 textPtr8 = text8->Des(); |
|
174 textPtr8.Copy(*text); |
|
175 |
|
176 file().Write( *text8 ); |
|
177 delete text; |
|
178 delete text8; |
|
179 CleanupStack::PopAndDestroy(); //file |
|
180 |
|
181 return *this; |
|
182 } |
|
183 |
|
184 // --------------------------------------------------------------------------- |
|
185 // CBCTestStreamLogger::CRL() |
|
186 // --------------------------------------------------------------------------- |
|
187 // |
|
188 CBCTestStreamLogger& CBCTestStreamLogger::CRL() |
|
189 { |
|
190 CLogFile& file = CLogFile::CreateLC( iFileName ); |
|
191 TBuf8<2> enter; |
|
192 enter.Append( 13 ); |
|
193 enter.Append( 10 ); |
|
194 file().Write( enter ); |
|
195 CleanupStack::PopAndDestroy(); //file |
|
196 return *this; |
|
197 } |
|
198 |
|
199 // --------------------------------------------------------------------------- |
|
200 // CBCTestStreamLogger::operator<<( CBCTestStreamLogger::Manipulator aOp ) |
|
201 // --------------------------------------------------------------------------- |
|
202 // |
|
203 CBCTestStreamLogger& CBCTestStreamLogger::operator<<( |
|
204 CBCTestStreamLogger::Manipulator aOp ) |
|
205 { |
|
206 return aOp( *this ); |
|
207 } |
|
208 |
|
209 namespace BCTest |
|
210 { |
|
211 CBCTestStreamLogger& End( CBCTestStreamLogger& self ) |
|
212 { |
|
213 self.Suicide(); |
|
214 return self; |
|
215 } |
|
216 |
|
217 CBCTestStreamLogger& EndLine( CBCTestStreamLogger& self ) |
|
218 { |
|
219 self.CRL(); |
|
220 return self; |
|
221 } |
|
222 } |