|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 // MessageFile.cpp: implementation of the CMessageFile class. |
|
18 // |
|
19 ////////////////////////////////////////////////////////////////////// |
|
20 |
|
21 #include "stdafx.h" |
|
22 #include "ServerClient.h" |
|
23 #include "MessageFile.h" |
|
24 #include "ServerManager.h" |
|
25 #include "TCErrorConstants.h" |
|
26 #include <stdio.h> |
|
27 #include <sys/stat.h> |
|
28 |
|
29 //#define USE_TEXT_FILE |
|
30 #ifdef USE_TEXT_FILE |
|
31 #define WRITE_MODE "w+t" |
|
32 #define APPEND_MODE "a+t" |
|
33 #else |
|
34 #define WRITE_MODE "w+b" |
|
35 #define APPEND_MODE "a+b" |
|
36 #endif |
|
37 |
|
38 #define FLUSH_FREQ 2000 // # messages to write between flushing (if buffered) |
|
39 #ifdef _DEBUG |
|
40 extern BOOL gDoLogging; |
|
41 #endif |
|
42 |
|
43 //#define LOG_MESSAGEFILE |
|
44 #if defined(LOG_MESSAGEFILE) && defined(_DEBUG) |
|
45 extern CServerManager* gManager; |
|
46 #define TCDEBUGOPEN() if (gDoLogging) { gManager->m_DebugLog->WaitForAccess(); } |
|
47 #define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(TCDebugMsg,"%s", s); gManager->m_DebugLog->log(TCDebugMsg); } |
|
48 #define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(TCDebugMsg, s, a1); gManager->m_DebugLog->log(TCDebugMsg); } |
|
49 #define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2); gManager->m_DebugLog->log(TCDebugMsg); } |
|
50 #define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2, a3); gManager->m_DebugLog->log(TCDebugMsg); } |
|
51 #define TCDEBUGCLOSE() if (gDoLogging) { gManager->m_DebugLog->ReleaseAccess(); } |
|
52 #else |
|
53 #define TCDEBUGOPEN() |
|
54 #define TCDEBUGLOGS(s) |
|
55 #define TCDEBUGLOGA1(s, a1) |
|
56 #define TCDEBUGLOGA2(s, a1, a2) |
|
57 #define TCDEBUGLOGA3(s, a1, a2, a3) |
|
58 #define TCDEBUGCLOSE() |
|
59 #endif |
|
60 |
|
61 ////////////////////////////////////////////////////////////////////// |
|
62 // Construction/Destruction |
|
63 ////////////////////////////////////////////////////////////////////// |
|
64 |
|
65 CMessageFile::CMessageFile(CHAR* pFilePath, long inClientID) |
|
66 { |
|
67 m_ClientID = inClientID; |
|
68 m_FileLocked = FALSE; |
|
69 strncpy(m_FilePath, pFilePath, MAX_FILEPATH); |
|
70 |
|
71 char toString[30]; |
|
72 sprintf(toString, "%s%04.4d", MESSAGEFILE_MUTEX_BASENAME, m_ClientID); |
|
73 |
|
74 m_Mutex.Open(toString, MESSAGEFILE_MUTEX_TIMEOUT); |
|
75 m_hFile = NULL; |
|
76 m_numWritten = 0; |
|
77 m_numWrittenSinceLastFlush = 0; |
|
78 m_Open = FALSE; |
|
79 |
|
80 #ifdef LOG_FILE_PERFORMANCE |
|
81 perfFileName="c:\\tcf\\serverfileperf.txt"; |
|
82 fpLog = NULL; |
|
83 numLogged=0; |
|
84 #endif |
|
85 OPENFILEPERF(); |
|
86 } |
|
87 |
|
88 CMessageFile::~CMessageFile() |
|
89 { |
|
90 CLOSEFILEPERF(); |
|
91 Close(); |
|
92 m_Mutex.Close(); |
|
93 |
|
94 } |
|
95 |
|
96 long CMessageFile::AddMessage(DWORD inLength, BYTE* inMessage) |
|
97 { |
|
98 long err = TCAPI_ERR_NONE; |
|
99 |
|
100 BOOL gotIt = WaitForAccess(); // will lock on first access only |
|
101 size_t lenWritten = 0; |
|
102 |
|
103 #ifdef USE_TEXT_FILE |
|
104 lenWritten = inLength; |
|
105 int textStart = 20; |
|
106 if (inMessage[9] == 0x5a) textStart = 64; |
|
107 for (int i = 0; i < textStart; i++) |
|
108 fprintf(m_hFile, "%02.2X ", inMessage[i]); |
|
109 |
|
110 for (i = textStart; i < (int)inLength; i++) |
|
111 { |
|
112 if (isprint(inMessage[i])) |
|
113 fprintf(m_hFile, "%c", inMessage[i]); |
|
114 else |
|
115 fprintf(m_hFile, "%02.2X ", inMessage[i]); |
|
116 } |
|
117 fprintf(m_hFile,"\n"); |
|
118 #else |
|
119 lenWritten = fwrite(inMessage, 1, inLength, m_hFile); |
|
120 #endif |
|
121 if (lenWritten < inLength) |
|
122 { |
|
123 err = TCAPI_ERR_WRITING_FILE; |
|
124 } |
|
125 else |
|
126 { |
|
127 m_numWrittenSinceLastFlush++; |
|
128 m_numWritten++; |
|
129 if ((m_numWrittenSinceLastFlush % FLUSH_FREQ) == 0) |
|
130 { |
|
131 FlushFile(TRUE); |
|
132 } |
|
133 } |
|
134 LOGFILEPERF("AddMessage\n"); |
|
135 |
|
136 // no ReleaseAccess this is done by Connection when all bytes processed in buffer and |
|
137 // calls all clients unlock method |
|
138 |
|
139 return err; |
|
140 } |
|
141 |
|
142 long CMessageFile::Open() |
|
143 { |
|
144 TCDEBUGOPEN(); |
|
145 long err = TCAPI_ERR_NONE; |
|
146 |
|
147 TCDEBUGLOGS("CMessageFile::Open\n"); |
|
148 WaitForAccess(); |
|
149 |
|
150 if (m_hFile) |
|
151 fclose(m_hFile); |
|
152 |
|
153 m_hFile = _fsopen(m_FilePath, APPEND_MODE, _SH_DENYNO); |
|
154 |
|
155 if (m_hFile == NULL) |
|
156 { |
|
157 err = TCAPI_ERR_FILE_DOES_NOT_EXIST; |
|
158 } |
|
159 else |
|
160 m_Open = TRUE; |
|
161 |
|
162 |
|
163 ReleaseAccess(); |
|
164 |
|
165 TCDEBUGCLOSE(); |
|
166 return err; |
|
167 } |
|
168 |
|
169 long CMessageFile::Close() |
|
170 { |
|
171 TCDEBUGOPEN(); |
|
172 long err = TCAPI_ERR_NONE; |
|
173 |
|
174 TCDEBUGLOGA1("CMessageFile::Close numWritten=%d\n", m_numWritten); |
|
175 WaitForAccess(); |
|
176 |
|
177 if (m_hFile) |
|
178 { |
|
179 fclose(m_hFile); |
|
180 m_hFile = NULL; |
|
181 } |
|
182 |
|
183 ReleaseAccess(); |
|
184 |
|
185 TCDEBUGCLOSE(); |
|
186 return err; |
|
187 } |
|
188 |
|
189 long CMessageFile::ClearFile() |
|
190 { |
|
191 TCDEBUGOPEN(); |
|
192 long err = TCAPI_ERR_NONE; |
|
193 |
|
194 TCDEBUGLOGA2("CMessageFile::ClearFile m_hFile=%x numWritten=%d\n", m_hFile, m_numWritten); |
|
195 WaitForAccess(); |
|
196 |
|
197 if (m_hFile) |
|
198 { |
|
199 fclose(m_hFile); |
|
200 m_hFile = NULL; |
|
201 m_Open = FALSE; |
|
202 } |
|
203 m_numWritten = 0; |
|
204 m_numWrittenSinceLastFlush = 0; |
|
205 |
|
206 m_hFile = _fsopen(m_FilePath, WRITE_MODE, _SH_DENYNO); |
|
207 |
|
208 if (m_hFile == NULL) |
|
209 { |
|
210 err = TCAPI_ERR_FILE_DOES_NOT_EXIST; |
|
211 } |
|
212 else |
|
213 m_Open = TRUE; |
|
214 |
|
215 LOGFILEPERF("ClearFile\n"); |
|
216 |
|
217 TCDEBUGLOGA1("CMessageFile::ClearFile m_hFile=%x\n", m_hFile); |
|
218 ReleaseAccess(); |
|
219 |
|
220 TCDEBUGCLOSE(); |
|
221 return err; |
|
222 } |
|
223 void CMessageFile::FlushFile(BOOL numberTimeOut) |
|
224 { |
|
225 if (m_hFile && m_numWrittenSinceLastFlush > 0) |
|
226 { |
|
227 fflush(m_hFile); |
|
228 m_numWrittenSinceLastFlush = 0; |
|
229 if (numberTimeOut) |
|
230 LOGFILEPERF("FlushFile <number>\n"); |
|
231 else |
|
232 LOGFILEPERF("FlushFile <time>\n"); |
|
233 } |
|
234 } |
|
235 #ifdef LOG_FILE_PERFORMANCE |
|
236 void CMessageFile::logPerf(char* msg) |
|
237 { |
|
238 if (fpLog) |
|
239 { |
|
240 SYSTEMTIME sTime; |
|
241 GetLocalTime(&sTime); |
|
242 fprintf(fpLog, |
|
243 "%02.2d%02.2d-%02.2d:%02.2d:%02.2d.%03.3d: %s", |
|
244 sTime.wDay, sTime.wMonth, sTime.wHour, sTime.wMinute, sTime.wSecond, sTime.wMilliseconds, |
|
245 msg); |
|
246 |
|
247 numLogged++; |
|
248 if ((numLogged % 1000) == 0) |
|
249 fflush(fpLog); |
|
250 } |
|
251 } |
|
252 void CMessageFile::openPerf() |
|
253 { |
|
254 struct _stat buf; |
|
255 char* dirname = "c:\\tcf"; |
|
256 int result = _stat(dirname, &buf); |
|
257 if (result == 0) // exists |
|
258 { |
|
259 if (fpLog == NULL) |
|
260 fpLog = _fsopen(perfFileName, "at", _SH_DENYNO); |
|
261 } |
|
262 else |
|
263 { |
|
264 fpLog = NULL; |
|
265 } |
|
266 } |
|
267 void CMessageFile::closePerf() |
|
268 { |
|
269 if (fpLog) |
|
270 { |
|
271 fflush(fpLog); |
|
272 fclose(fpLog); |
|
273 } |
|
274 fpLog = NULL; |
|
275 } |
|
276 #endif |