|
1 // Copyright (c) 1997-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 // DPACKETLOG.CPP |
|
15 // |
|
16 // |
|
17 |
|
18 #include "dpcktlog.h" |
|
19 |
|
20 #ifdef __LOGGER__ |
|
21 #ifdef __EXE__ |
|
22 GLDEF_D CDTSYLogger* CDTSYLogger; |
|
23 #endif |
|
24 |
|
25 _LIT(KLogFileName,"C:\\DTSY.LOG"); |
|
26 #define KGenericBufferSize (150) |
|
27 |
|
28 CDTSYLogger* CDTSYLogger::NewL() |
|
29 { |
|
30 CDTSYLogger* logger=new(ELeave) CDTSYLogger(); |
|
31 CleanupStack::PushL(logger); |
|
32 logger->ConstructL(); |
|
33 CleanupStack::Pop(); |
|
34 return logger; |
|
35 } |
|
36 |
|
37 CDTSYLogger::CDTSYLogger() : iValid(EFalse) |
|
38 {} |
|
39 |
|
40 void CDTSYLogger::ConstructL() |
|
41 /** |
|
42 * In debug mode the logfile will not be deleted at start of new session |
|
43 */ |
|
44 { |
|
45 iFs.Connect(); |
|
46 TInt ret=KErrNone; |
|
47 ret=iFile.Open(iFs,KLogFileName,EFileShareAny|EFileWrite); |
|
48 if(ret!=KErrNone) |
|
49 ret=iFile.Create(iFs,KLogFileName,EFileShareAny|EFileWrite); |
|
50 if(ret==KErrNone) |
|
51 { |
|
52 iValid=ETrue; |
|
53 TInt aPos=0; |
|
54 iFile.Seek(ESeekEnd,aPos); |
|
55 ret=iFile.Write(_L8("----------New Log----------\015\012")); |
|
56 } |
|
57 } |
|
58 |
|
59 void CDTSYLogger::Destruct() |
|
60 { |
|
61 #ifdef __EXE__ |
|
62 CDTSYLogger* context=aScriptLoggerContext; |
|
63 delete context; |
|
64 aScriptLoggerContext=NULL; |
|
65 #else |
|
66 CDTSYLogger* context=(CDTSYLogger*) Dll::Tls(); |
|
67 delete context; |
|
68 Dll::SetTls(NULL); |
|
69 #endif |
|
70 } |
|
71 |
|
72 CDTSYLogger::~CDTSYLogger() |
|
73 { |
|
74 if(iValid) |
|
75 iFile.Close(); |
|
76 iFs.Close(); |
|
77 } |
|
78 |
|
79 void CDTSYLogger::Write(const TDesC8& aText) |
|
80 { |
|
81 #ifdef __EXE__ |
|
82 CDTSYLogger* context=aScriptLoggerContext; |
|
83 #else |
|
84 CDTSYLogger* context=(CDTSYLogger*) Dll::Tls(); |
|
85 #endif |
|
86 if(context==NULL) |
|
87 { |
|
88 TRAPD(err,context=CDTSYLogger::NewL()); |
|
89 if (KErrNone != err) |
|
90 return; |
|
91 #ifdef __EXE__ |
|
92 aScriptLoggerContext=context; |
|
93 #else |
|
94 Dll::SetTls(context); |
|
95 #endif |
|
96 } |
|
97 if(context->iValid) |
|
98 context->WriteRecord(aText); |
|
99 } |
|
100 |
|
101 void CDTSYLogger::WriteFormat(TRefByValue<const TDesC8> aFmt,...) |
|
102 { |
|
103 TBuf8<KGenericBufferSize> buf; |
|
104 VA_LIST list; |
|
105 VA_START(list,aFmt); |
|
106 buf.FormatList(aFmt,list); |
|
107 TChar tmpchar; |
|
108 for(TInt i=0;i<buf.Length();i++) |
|
109 { |
|
110 tmpchar=buf[i]; |
|
111 if(!((tmpchar.IsPrint()) || (tmpchar=='\n') || (tmpchar=='\r') || (tmpchar=='\t'))) |
|
112 buf[i]='.'; |
|
113 } |
|
114 #ifdef __EXE__ |
|
115 CDTSYLogger* context=aScriptLoggerContext; |
|
116 #else |
|
117 CDTSYLogger* context=(CDTSYLogger*) Dll::Tls(); |
|
118 #endif |
|
119 if(context==NULL) |
|
120 { |
|
121 TRAPD(err,context=CDTSYLogger::NewL()); |
|
122 if (KErrNone != err) |
|
123 return; |
|
124 #ifdef __EXE__ |
|
125 aScriptLoggerContext=context; |
|
126 #else |
|
127 Dll::SetTls(context); |
|
128 #endif |
|
129 } |
|
130 if(context->iValid) |
|
131 context->WriteRecord(buf); |
|
132 } |
|
133 |
|
134 void CDTSYLogger::WriteRecord(const TDesC8& aText) |
|
135 { |
|
136 if(iValid) |
|
137 { |
|
138 TBuf8<KGenericBufferSize> buf; |
|
139 TTime now; |
|
140 now.UniversalTime(); |
|
141 TDateTime dateTime; |
|
142 dateTime = now.DateTime(); |
|
143 buf.Format(_L8 ("%04d/%02d/%02d %02d.%02d:%02d:%06d "),dateTime.Year(),dateTime.Month()+1, dateTime.Day()+1,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond()); |
|
144 buf.AppendFormat(_L8("%S\015\012"),&aText); |
|
145 iFile.Write(buf); |
|
146 iFile.Flush(); |
|
147 } |
|
148 } |
|
149 |
|
150 #endif |