|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file testlog.h |
|
18 */ |
|
19 |
|
20 #include "testlog.h" |
|
21 |
|
22 _LIT(KLineNumber, " line %d "); |
|
23 |
|
24 #ifdef _WIN32 |
|
25 _LIT(KLineEnd, "\r\n"); |
|
26 #else |
|
27 _LIT(KLineEnd, "\n"); |
|
28 #endif |
|
29 |
|
30 CTestLog* CTestLog::NewLC(RFs& aFsSession, TPtrC aFilename, TInt aDebugOn, CConsoleBase* aConsole) |
|
31 { |
|
32 CTestLog* fd = new (ELeave) CTestLog; |
|
33 CleanupStack::PushL(fd); |
|
34 fd->ConstructL(aFsSession, aFilename, aDebugOn, aConsole); |
|
35 return fd; |
|
36 } |
|
37 |
|
38 CTestLog::~CTestLog() |
|
39 { |
|
40 iFile.Close(); |
|
41 iInitialised = EFalse; |
|
42 } |
|
43 |
|
44 |
|
45 void CTestLog::ConstructL(RFs &aFsSession, TPtrC aFilename, TInt aDebugOn, CConsoleBase* aConsole) |
|
46 /** |
|
47 Opens the output file |
|
48 |
|
49 @param aFsSession A session with the file server |
|
50 @param aFilename |
|
51 @param aDebugOn |
|
52 @param aConsole |
|
53 */ |
|
54 { |
|
55 if (iErr.Evaluate(iFile.Replace(aFsSession, aFilename, EFileShareAny|EFileStream|EFileWrite))) |
|
56 { |
|
57 iDebug = aDebugOn; |
|
58 iConsole = aConsole; |
|
59 iInitialised = ETrue; |
|
60 } |
|
61 } |
|
62 |
|
63 |
|
64 void CTestLog::WriteErrorMessage(const TPtrC &aMessage) |
|
65 /** |
|
66 Writes a message to the file |
|
67 |
|
68 @param aMessage The error message |
|
69 */ |
|
70 { |
|
71 _LIT(KWriteErrorStr,"ERROR - %S\r\n"); |
|
72 |
|
73 if (iInitialised && aMessage.Length() && aMessage.Length() < (MAX_BUF_LENGTH)) |
|
74 { |
|
75 iBuf.Format(KWriteErrorStr, |
|
76 &aMessage); |
|
77 WriteBufferToFile(iBuf, iFile); |
|
78 } |
|
79 } |
|
80 |
|
81 void CTestLog::Msg(TPtrC aText, ...) |
|
82 { |
|
83 if (iInitialised && aText.Length() && aText.Length() < MAX_BUF_LENGTH) |
|
84 { |
|
85 VA_LIST list; |
|
86 VA_START(list, aText); |
|
87 |
|
88 iBuf.FormatList(aText, list); |
|
89 iBuf.Append(KLineEnd); |
|
90 |
|
91 if (iConsole) |
|
92 iConsole->Printf(iBuf); |
|
93 |
|
94 WriteBufferToFile(iBuf, iFile); |
|
95 |
|
96 VA_END(list); |
|
97 } |
|
98 } |
|
99 |
|
100 void CTestLog::Dbg(TPtrC aText, ...) |
|
101 { |
|
102 if (iInitialised && iDebug && aText.Length() && aText.Length() < MAX_BUF_LENGTH) |
|
103 { |
|
104 VA_LIST list; |
|
105 VA_START(list, aText); |
|
106 |
|
107 iBuf.FormatList(aText, list); |
|
108 iBuf.Append(KLineEnd); |
|
109 |
|
110 if (iConsole) |
|
111 iConsole->Printf(iBuf); |
|
112 |
|
113 WriteBufferToFile(iBuf, iFile); |
|
114 |
|
115 VA_END(list); |
|
116 } |
|
117 } |
|
118 |
|
119 void CTestLog::DiagPrint(const TText8* aFile, TInt aLine, TPtrC aSeverity, TPtrC aText, ...) |
|
120 { |
|
121 if (iInitialised && iDebug && aText.Length() && aText.Length() < MAX_BUF_LENGTH) |
|
122 { |
|
123 VA_LIST list; |
|
124 VA_START(list, aText); |
|
125 |
|
126 // Clear the buffer |
|
127 iBuf.Zero(); |
|
128 |
|
129 // Add the file name |
|
130 TInt i = 0; |
|
131 while((aFile[i] != '\0') && (i < MAX_FILENAME_LENGTH)) |
|
132 { |
|
133 iBuf.Append(aFile[i++]); |
|
134 } |
|
135 |
|
136 // Add the line number and message |
|
137 iBuf.AppendFormat(KLineNumber, aLine); |
|
138 iBuf.AppendFormat(aSeverity); |
|
139 iBuf.AppendFormatList(aText, list); |
|
140 iBuf.Append(KLineEnd); |
|
141 |
|
142 if (iConsole) |
|
143 iConsole->Printf(iBuf); |
|
144 |
|
145 WriteBufferToFile(iBuf, iFile); |
|
146 |
|
147 VA_END(list); |
|
148 } |
|
149 } |
|
150 |
|
151 |
|
152 TBool CTestLog::WriteBufferToFile(TDesC16 &aBuffer, RFile &aHandle) |
|
153 { |
|
154 static TBuf8<1024> outputBuffer; |
|
155 static TPtrC16 ptr; |
|
156 static TInt consumed; |
|
157 static TInt remainder = 0; |
|
158 |
|
159 TBool valid = EFalse; |
|
160 |
|
161 if (aBuffer.Length()) |
|
162 { |
|
163 ptr.Set(aBuffer); |
|
164 do |
|
165 { |
|
166 // get something to write |
|
167 consumed = Min(outputBuffer.MaxLength(), ptr.Length()); |
|
168 |
|
169 // write it |
|
170 outputBuffer.Copy(ptr.Left(consumed)); |
|
171 if (aHandle.Write(outputBuffer) != KErrNone) |
|
172 return EFalse; |
|
173 |
|
174 // get the next chunk |
|
175 remainder = ptr.Length() - consumed; |
|
176 if (remainder > 0) |
|
177 ptr.Set(ptr.Right(remainder)); |
|
178 |
|
179 }while (remainder > 0); |
|
180 |
|
181 valid = ETrue; |
|
182 |
|
183 // Make sure the latest log messages are flushed to the file. |
|
184 aHandle.Flush(); |
|
185 } |
|
186 return valid; |
|
187 } |