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