|
1 // Copyright (c) 1999-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 #include "VWSDEBUG.H" |
|
17 |
|
18 #ifdef __DO_LOGGING__ |
|
19 |
|
20 |
|
21 //#define VIEWSRV_SHOW_TRACE |
|
22 #ifdef VIEWSRV_SHOW_TRACE |
|
23 void ShowTrace(TRefByValue<const TDesC> aFmt,...) |
|
24 { |
|
25 RDebug::Print(aFmt); |
|
26 } |
|
27 #else |
|
28 void ShowTrace(TRefByValue<const TDesC> /*aFmt*/,...) |
|
29 { |
|
30 } |
|
31 #endif |
|
32 |
|
33 // |
|
34 // Constants. |
|
35 // |
|
36 |
|
37 const TInt KMaxTraceLength=256; |
|
38 |
|
39 |
|
40 // |
|
41 // CVwsLog. |
|
42 // |
|
43 |
|
44 void CVwsLog::StartLogL() |
|
45 { |
|
46 CVwsLog* self=new(ELeave) CVwsLog; |
|
47 TRAPD(err,self->ConstructL()); |
|
48 if (!err) |
|
49 { |
|
50 Dll::SetTls(self); |
|
51 CVwsLog::Log(CVwsLog::ENormal,_L("View Server log created")); |
|
52 } |
|
53 } |
|
54 |
|
55 void CVwsLog::ShutdownLog() |
|
56 { |
|
57 CVwsLog::Log(CVwsLog::ENormal,_L("View Server log shutdown")); |
|
58 CVwsLog* self=STATIC_CAST(CVwsLog*,Dll::Tls()); |
|
59 ASSERT(self); |
|
60 delete self; |
|
61 Dll::SetTls(NULL); |
|
62 } |
|
63 |
|
64 CVwsLog::CVwsLog() : iType(EFile),iLevel(ELoud) |
|
65 { |
|
66 } |
|
67 |
|
68 CVwsLog::~CVwsLog() |
|
69 { |
|
70 iLogFile.Close(); |
|
71 iFs.Close(); |
|
72 } |
|
73 |
|
74 void CVwsLog::ConstructL() |
|
75 { |
|
76 TUint att; |
|
77 |
|
78 RBuf dataLogs; |
|
79 RBuf viewLog; |
|
80 RBuf viewLogOne; |
|
81 RBuf viewLogTwo; |
|
82 |
|
83 TChar sysDrive = RFs::GetSystemDriveChar(); |
|
84 _LIT(KDataLogs,":\\Data\\logs"); |
|
85 _LIT(KlogView,":\\Data\\logs\\viewsrv.txt"); |
|
86 _LIT(KLogViewOne,":\\Data\\logs\\viewsrv1.txt"); |
|
87 _LIT(KLogViewTwo,":\\Data\\logs\\viewsrv2.txt"); |
|
88 TInt maxSizeofFileName = KDataLogs().Length() + 1; |
|
89 dataLogs.CreateL(maxSizeofFileName); |
|
90 dataLogs.CleanupClosePushL(); |
|
91 dataLogs.Append(sysDrive); |
|
92 dataLogs.Append(KDataLogs()); |
|
93 |
|
94 maxSizeofFileName = KlogView().Length() + 1; |
|
95 viewLog.CreateL(maxSizeofFileName); |
|
96 viewLog.CleanupClosePushL(); |
|
97 viewLog.Append(sysDrive); |
|
98 viewLog.Append(KlogView()); |
|
99 |
|
100 maxSizeofFileName = KLogViewOne().Length() + 1; |
|
101 viewLogOne.CreateL(maxSizeofFileName); |
|
102 viewLogOne.CleanupClosePushL(); |
|
103 viewLogOne.Append(sysDrive); |
|
104 viewLogOne.Append(KLogViewOne()); |
|
105 |
|
106 maxSizeofFileName = KLogViewTwo().Length() + 1; |
|
107 viewLogTwo.CreateL(maxSizeofFileName); |
|
108 viewLogTwo.CleanupClosePushL(); |
|
109 viewLogTwo.Append(sysDrive); |
|
110 viewLogTwo.Append(KLogViewTwo()); |
|
111 |
|
112 // If the log file can be opened,logging is active |
|
113 if (iFs.Connect() == KErrNone && iFs.Att(dataLogs,att) == KErrNone) |
|
114 { |
|
115 // Make a copy of previous log file |
|
116 iFs.Delete(viewLogTwo); |
|
117 iFs.Rename(viewLogOne,viewLogTwo); |
|
118 iFs.Rename(viewLog,viewLogOne); |
|
119 |
|
120 if (iLogFile.Replace(iFs, viewLog, EFileShareAny | EFileWrite | EFileStreamText) == KErrNone) |
|
121 { |
|
122 iActive = ETrue; |
|
123 } |
|
124 |
|
125 TUint16 feffInt=0xFEFF; |
|
126 User::LeaveIfError(iLogFile.Write(TPtrC8((TUint8 *)&feffInt,2))); |
|
127 iEol16=TPtrC16((TUint16 *)_S("\r\n")); |
|
128 iEol.Set((TUint8 *)iEol16.Ptr(),iEol16.Size()); |
|
129 } |
|
130 |
|
131 CleanupStack::PopAndDestroy(&viewLogTwo); |
|
132 CleanupStack::PopAndDestroy(&viewLogOne); |
|
133 CleanupStack::PopAndDestroy(&viewLog); |
|
134 CleanupStack::PopAndDestroy(&dataLogs); |
|
135 } |
|
136 |
|
137 void CVwsLog::SetLevel(TLevel aLogLevel) |
|
138 { |
|
139 iLevel=aLogLevel; |
|
140 } |
|
141 |
|
142 void CVwsLog::SetType(TLogType aType) |
|
143 { |
|
144 iType=aType; |
|
145 } |
|
146 |
|
147 void CVwsLog::Log(TLevel aLogLevel,TRefByValue<const TDesC> aFmt,...) |
|
148 { |
|
149 CVwsLog* self=STATIC_CAST(CVwsLog*,Dll::Tls()); |
|
150 ASSERT(self); |
|
151 VA_LIST list; |
|
152 VA_START(list,aFmt); |
|
153 TBuf<KMaxTraceLength> buf; |
|
154 buf.FormatList(aFmt,list); |
|
155 self->DoLog(aLogLevel,buf); |
|
156 } |
|
157 |
|
158 void CVwsLog::DoLog(TLevel aLogLevel,const TDesC& aOutput) |
|
159 { |
|
160 if (aLogLevel>iLevel) |
|
161 { |
|
162 return; |
|
163 } |
|
164 |
|
165 switch (iType) |
|
166 { |
|
167 case EFile: |
|
168 LogToFile(aLogLevel,aOutput); |
|
169 break; |
|
170 case ERDebug: |
|
171 LogToRDebug(aOutput); |
|
172 break; |
|
173 case EBoth: |
|
174 LogToFile(aLogLevel,aOutput); |
|
175 LogToRDebug(aOutput); |
|
176 break; |
|
177 case ENeither: |
|
178 break; |
|
179 } |
|
180 } |
|
181 |
|
182 void CVwsLog::LogToFile(TLevel aLogLevel,const TDesC& aOutput) |
|
183 { |
|
184 if (iActive) |
|
185 { |
|
186 for (TInt ii=0;ii<aLogLevel;ii++) |
|
187 { |
|
188 TUint16 tab=0x0009; |
|
189 iLogFile.Write(TPtrC8((TUint8*)&tab,2)); |
|
190 } |
|
191 iLogFile.Write(TPtrC8((TUint8 *)aOutput.Ptr(),aOutput.Size())); |
|
192 iLogFile.Write(iEol); |
|
193 iLogFile.Flush(); |
|
194 } |
|
195 } |
|
196 |
|
197 void CVwsLog::LogToRDebug(const TDesC& aOutput) |
|
198 { |
|
199 TBuf<KMaxTraceLength> buf; |
|
200 buf.Insert(0,_L("VIEWSRV: ")); |
|
201 buf.Append(aOutput); |
|
202 ShowTrace(buf); |
|
203 } |
|
204 |
|
205 #endif |