|
1 // Copyright (c) 2001-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 // Class to handle output from the test harness. All output goes to the log file, |
|
15 // but screen output can be more controlled. |
|
16 // |
|
17 // |
|
18 |
|
19 #include "logger.h" |
|
20 |
|
21 #include <e32cons.h> |
|
22 |
|
23 |
|
24 CTOCSPLogger* CTOCSPLogger::NewLC(const TDesC& aLogFile) |
|
25 { |
|
26 CTOCSPLogger* self = new (ELeave) CTOCSPLogger; |
|
27 CleanupStack::PushL(self); |
|
28 self->ConstructL(aLogFile); |
|
29 return self; |
|
30 } |
|
31 |
|
32 |
|
33 CTOCSPLogger* CTOCSPLogger::NewL(const TDesC& aLogFile) |
|
34 { |
|
35 CTOCSPLogger* self = CTOCSPLogger::NewLC(aLogFile); |
|
36 CleanupStack::Pop(self); |
|
37 return self; |
|
38 } |
|
39 |
|
40 |
|
41 CTOCSPLogger::~CTOCSPLogger() |
|
42 { |
|
43 iFile.Close(); |
|
44 iSession.Close(); |
|
45 |
|
46 delete iConsole; |
|
47 } |
|
48 |
|
49 |
|
50 void CTOCSPLogger::ConstructL(const TDesC& aLogFile) |
|
51 { |
|
52 iConsole = Console::NewL(_L("OCSP Test"), TSize(KDefaultConsWidth,KDefaultConsHeight)); |
|
53 |
|
54 User::LeaveIfError(iSession.Connect()); |
|
55 |
|
56 TInt err = iSession.MkDirAll(aLogFile); |
|
57 if (err != KErrAlreadyExists) |
|
58 { |
|
59 User::LeaveIfError(err); |
|
60 } |
|
61 User::LeaveIfError(iFile.Replace(iSession, aLogFile, EFileShareExclusive)); |
|
62 } |
|
63 |
|
64 |
|
65 void CTOCSPLogger::LogL(const TDesC& aText, const TBool aEcho) |
|
66 { |
|
67 iEcho = aEcho; |
|
68 LogL(aText); |
|
69 } |
|
70 |
|
71 |
|
72 void CTOCSPLogger::LogL(const TDesC8& aText, const TBool aEcho) |
|
73 { |
|
74 iEcho = aEcho; |
|
75 LogL(aText); |
|
76 } |
|
77 |
|
78 |
|
79 void CTOCSPLogger::LogL(const TInt aInt, const TBool aEcho) |
|
80 { |
|
81 iEcho = aEcho; |
|
82 LogL(aInt); |
|
83 } |
|
84 |
|
85 |
|
86 void CTOCSPLogger::LogL(const TDesC& aText) |
|
87 { |
|
88 if (iEcho) |
|
89 { |
|
90 iConsole->Write(aText); |
|
91 } |
|
92 |
|
93 // Have to convert to 8-bit for file |
|
94 |
|
95 TInt textLen = aText.Length(); |
|
96 |
|
97 HBufC8* buf8 = NULL; |
|
98 TRAPD(err, buf8=HBufC8::NewL(textLen)); |
|
99 if( KErrNoMemory == err) |
|
100 { |
|
101 User::CompressAllHeaps(); |
|
102 |
|
103 TRAPD(err2, buf8=HBufC8::NewL(textLen)); |
|
104 if( KErrNone != err2) |
|
105 { |
|
106 User::Leave(err2); |
|
107 } |
|
108 |
|
109 } |
|
110 CleanupStack::PushL(buf8); |
|
111 |
|
112 TPtr8 pBuf8(buf8->Des()); |
|
113 pBuf8.Copy(aText); |
|
114 |
|
115 User::LeaveIfError(iFile.Write(*buf8)); |
|
116 CleanupStack::PopAndDestroy(buf8); |
|
117 } |
|
118 |
|
119 void CTOCSPLogger::LogL(const TDesC8& aText) |
|
120 { |
|
121 if (iEcho) |
|
122 { |
|
123 // Convert to 16 bit descriptor to write to console |
|
124 RBuf buf16; |
|
125 buf16.CreateL(aText.Length()); |
|
126 CleanupClosePushL(buf16); |
|
127 buf16.Copy(aText); |
|
128 iConsole->Write(buf16); |
|
129 CleanupStack::PopAndDestroy(&buf16); |
|
130 } |
|
131 |
|
132 User::LeaveIfError(iFile.Write(aText)); |
|
133 } |
|
134 |
|
135 |
|
136 void CTOCSPLogger::LogL(const TInt aInt) |
|
137 { |
|
138 TBuf<12> buf; |
|
139 buf.Num(aInt); |
|
140 LogL(buf); |
|
141 } |
|
142 |
|
143 |
|
144 void CTOCSPLogger::NewLineL() |
|
145 { |
|
146 LogL(_L("\n")); |
|
147 } |
|
148 |
|
149 |
|
150 void CTOCSPLogger::PressAnyKeyL() |
|
151 { |
|
152 LogL(_L("[Press any key]\n")); |
|
153 |
|
154 if (iEcho) |
|
155 { |
|
156 iConsole->Getch(); |
|
157 } |
|
158 } |