1 //===-- MICmnLogMediumFile.cpp ----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // In-house headers:
11 #include "MICmnLogMediumFile.h"
12 #include "MICmnResources.h"
13
14 //++
15 //------------------------------------------------------------------------------------
16 // Details: CMICmnLogMediumFile constructor.
17 // Type: Method.
18 // Args: None.
19 // Return: None.
20 // Throws: None.
21 //--
CMICmnLogMediumFile()22 CMICmnLogMediumFile::CMICmnLogMediumFile()
23 : m_constThisMediumName(MIRSRC(IDS_MEDIUMFILE_NAME)),
24 m_constMediumFileNameFormat("lldb-mi-%s.log"),
25 m_strMediumFileName(MIRSRC(IDS_MEDIUMFILE_ERR_INVALID_PATH)),
26 m_strMediumFileDirectory("."),
27 m_fileNamePath(MIRSRC(IDS_MEDIUMFILE_ERR_INVALID_PATH)),
28 m_eVerbosityType(CMICmnLog::eLogVerbosity_Log),
29 m_strDate(CMIUtilDateTimeStd().GetDate()),
30 m_fileHeaderTxt(MIRSRC(IDS_MEDIUMFILE_ERR_FILE_HEADER)) {}
31
32 //++
33 //------------------------------------------------------------------------------------
34 // Details: CMICmnLogMediumFile destructor.
35 // Type: Overridden.
36 // Args: None.
37 // Return: None.
38 // Throws: None.
39 //--
~CMICmnLogMediumFile()40 CMICmnLogMediumFile::~CMICmnLogMediumFile() {}
41
42 //++
43 //------------------------------------------------------------------------------------
44 // Details: Get the singleton instance of *this class.
45 // Type: Static.
46 // Args: None.
47 // Return: CMICmnLogMediumFile - Reference to *this object.
48 // Throws: None.
49 //--
Instance()50 CMICmnLogMediumFile &CMICmnLogMediumFile::Instance() {
51 static CMICmnLogMediumFile instance;
52
53 return instance;
54 }
55
56 //++
57 //------------------------------------------------------------------------------------
58 // Details: Initialize setup *this medium ready for use.
59 // Type: Overridden.
60 // Args: None.
61 // Return: MIstatus::success - Functional succeeded.
62 // MIstatus::failure - Functional failed.
63 // Throws: None.
64 //--
Initialize()65 bool CMICmnLogMediumFile::Initialize() {
66 m_bInitialized = true;
67 return FileFormFileNamePath();
68 }
69
70 //++
71 //------------------------------------------------------------------------------------
72 // Details: Unbind detach or release resources used by *this medium.
73 // Type: Method.
74 // Args: None.
75 // Return: None.
76 // Throws: None.
77 //--
Shutdown()78 bool CMICmnLogMediumFile::Shutdown() {
79 if (m_bInitialized) {
80 m_bInitialized = false;
81 m_file.Close();
82 }
83 return MIstatus::success;
84 }
85
86 //++
87 //------------------------------------------------------------------------------------
88 // Details: Retrieve the name of *this medium.
89 // Type: Overridden.
90 // Args: None.
91 // Return: CMIUtilString - Text data.
92 // Throws: None.
93 //--
GetName() const94 const CMIUtilString &CMICmnLogMediumFile::GetName() const {
95 return m_constThisMediumName;
96 }
97
98 //++
99 //------------------------------------------------------------------------------------
100 // Details: The callee client calls the write function on the Logger. The data
101 // to be
102 // written is given out to all the mediums registered. The verbosity
103 // type parameter
104 // indicates to the medium the type of data or message given to it. The
105 // medium has
106 // modes of verbosity and depending on the verbosity set determines
107 // which data is
108 // sent to the medium's output.
109 // Type: Method.
110 // Args: vData - (R) The data to write to the logger.
111 // veType - (R) Verbosity type.
112 // Return: MIstatus::success - Functional succeeded.
113 // MIstatus::failure - Functional failed.
114 // Throws: None.
115 //--
Write(const CMIUtilString & vData,const CMICmnLog::ELogVerbosity veType)116 bool CMICmnLogMediumFile::Write(const CMIUtilString &vData,
117 const CMICmnLog::ELogVerbosity veType) {
118 if (m_bInitialized && m_file.IsOk()) {
119 const bool bDoWrite = (m_eVerbosityType & veType);
120 if (bDoWrite) {
121 bool bNewCreated = false;
122 bool bOk = m_file.CreateWrite(m_fileNamePath, bNewCreated);
123 if (bOk) {
124 if (bNewCreated)
125 bOk = FileWriteHeader();
126 bOk = bOk && FileWriteEnglish(MassagedData(vData, veType));
127 }
128 return bOk;
129 }
130 }
131
132 return MIstatus::failure;
133 }
134
135 //++
136 //------------------------------------------------------------------------------------
137 // Details: Retrieve *this medium's last error condition.
138 // Type: Method.
139 // Args: None.
140 // Return: CString & - Text description.
141 // Throws: None.
142 //--
GetError() const143 const CMIUtilString &CMICmnLogMediumFile::GetError() const {
144 return m_strMILastErrorDescription;
145 }
146
147 //++
148 //------------------------------------------------------------------------------------
149 // Details: Set the verbosity mode for this medium.
150 // Type: Method.
151 // Args: veType - (R) Mask value.
152 // Return: MIstatus::success - Functional succeeded.
153 // MIstatus::failure - Functional failed.
154 // Throws: None.
155 //--
SetVerbosity(const MIuint veType)156 bool CMICmnLogMediumFile::SetVerbosity(const MIuint veType) {
157 m_eVerbosityType = veType;
158 return MIstatus::success;
159 }
160
161 //++
162 //------------------------------------------------------------------------------------
163 // Details: Get the verbosity mode for this medium.
164 // Type: Method.
165 // Args: veType - (R) Mask value.
166 // Return: CMICmnLog::ELogVerbosity - Mask value.
167 // Throws: None.
168 //--
GetVerbosity() const169 MIuint CMICmnLogMediumFile::GetVerbosity() const { return m_eVerbosityType; }
170
171 //++
172 //------------------------------------------------------------------------------------
173 // Details: Write data to a file English font.
174 // Type: Method.
175 // Args: vData - (R) The data to write to the logger.
176 // Return: None.
177 // Throws: None.
178 //--
FileWriteEnglish(const CMIUtilString & vData)179 bool CMICmnLogMediumFile::FileWriteEnglish(const CMIUtilString &vData) {
180 return m_file.Write(vData);
181 }
182
183 //++
184 //------------------------------------------------------------------------------------
185 // Details: Determine and form the medium file's directory path and name.
186 // Type: Method.
187 // Args: None.
188 // Return: MIstatus::success - Functional succeeded.
189 // MIstatus::failure - Functional failed.
190 // Throws: None.
191 //--
FileFormFileNamePath()192 bool CMICmnLogMediumFile::FileFormFileNamePath() {
193 ClrErrorDescription();
194
195 m_fileNamePath = MIRSRC(IDS_MEDIUMFILE_ERR_INVALID_PATH);
196
197 CMIUtilDateTimeStd date;
198 m_strMediumFileName =
199 CMIUtilString::Format(m_constMediumFileNameFormat.c_str(),
200 date.GetDateTimeLogFilename().c_str());
201
202 #if defined(_MSC_VER)
203 m_fileNamePath = CMIUtilString::Format(
204 "%s\\%s", m_strMediumFileDirectory.c_str(), m_strMediumFileName.c_str());
205 #else
206 m_fileNamePath = CMIUtilString::Format(
207 "%s/%s", m_strMediumFileDirectory.c_str(), m_strMediumFileName.c_str());
208 #endif // defined ( _MSC_VER )
209
210 return MIstatus::success;
211 }
212
213 //++
214 //------------------------------------------------------------------------------------
215 // Details: Retrieve the medium file's directory path and name.
216 // Type: Method.
217 // Args: None.
218 // Return: CMIUtilString & - File path.
219 // Throws: None.
220 //--
GetFileNamePath() const221 const CMIUtilString &CMICmnLogMediumFile::GetFileNamePath() const {
222 return m_fileNamePath;
223 }
224
225 //++
226 //------------------------------------------------------------------------------------
227 // Details: Retrieve the medium file's name.
228 // Type: Method.
229 // Args: None.
230 // Return: CMIUtilString & - File name.
231 // Throws: None.
232 //--
GetFileName() const233 const CMIUtilString &CMICmnLogMediumFile::GetFileName() const {
234 return m_strMediumFileName;
235 }
236
237 //++
238 //------------------------------------------------------------------------------------
239 // Details: Massage the data to behave correct when submitted to file. Insert
240 // extra log
241 // specific text. The veType is there to allow in the future to parse
242 // the log and
243 // filter in out specific types of message to make viewing the log more
244 // manageable.
245 // Type: Method.
246 // Args: vData - (R) Raw data.
247 // veType - (R) Message type.
248 // Return: CMIUtilString - Massaged data.
249 // Throws: None.
250 //--
251 CMIUtilString
MassagedData(const CMIUtilString & vData,const CMICmnLog::ELogVerbosity veType)252 CMICmnLogMediumFile::MassagedData(const CMIUtilString &vData,
253 const CMICmnLog::ELogVerbosity veType) {
254 const CMIUtilString strCr("\n");
255 CMIUtilString data;
256 const char verbosityCode(ConvertLogVerbosityTypeToId(veType));
257 const CMIUtilString dt(CMIUtilString::Format("%s %s", m_strDate.c_str(),
258 m_dateTime.GetTime().c_str()));
259
260 data = CMIUtilString::Format("%c,%s,%s", verbosityCode, dt.c_str(),
261 vData.c_str());
262 data = ConvertCr(data);
263
264 // Look for EOL...
265 const size_t pos = vData.rfind(strCr);
266 if (pos == vData.size())
267 return data;
268
269 // ... did not have an EOL so add one
270 data += GetLineReturn();
271
272 return data;
273 }
274
275 //++
276 //------------------------------------------------------------------------------------
277 // Details: Convert the Log's verbosity type number into a single char
278 // character.
279 // Type: Method.
280 // Args: veType - (R) Message type.
281 // Return: wchar_t - A letter.
282 // Throws: None.
283 //--
ConvertLogVerbosityTypeToId(const CMICmnLog::ELogVerbosity veType) const284 char CMICmnLogMediumFile::ConvertLogVerbosityTypeToId(
285 const CMICmnLog::ELogVerbosity veType) const {
286 char c = 0;
287 if (veType != 0) {
288 MIuint cnt = 0;
289 MIuint number(veType);
290 while (1 != number) {
291 number = number >> 1;
292 ++cnt;
293 }
294 c = 'A' + cnt;
295 } else {
296 c = '*';
297 }
298
299 return c;
300 }
301
302 //++
303 //------------------------------------------------------------------------------------
304 // Details: Retrieve state of whether the file medium is ok.
305 // Type: Method.
306 // Args: None.
307 // Return: True - file ok.
308 // False - file has a problem.
309 // Throws: None.
310 //--
IsOk() const311 bool CMICmnLogMediumFile::IsOk() const { return m_file.IsOk(); }
312
313 //++
314 //------------------------------------------------------------------------------------
315 // Details: Status on the file log medium existing already.
316 // Type: Method.
317 // Args: None.
318 // Return: True - Exists.
319 // False - Not found.
320 // Throws: None.
321 //--
IsFileExist() const322 bool CMICmnLogMediumFile::IsFileExist() const {
323 return m_file.IsFileExist(GetFileNamePath());
324 }
325
326 //++
327 //------------------------------------------------------------------------------------
328 // Details: Write the header text the logger file.
329 // Type: Method.
330 // Args: vText - (R) Text.
331 // Return: MIstatus::success - Functional succeeded.
332 // MIstatus::failure - Functional failed.
333 // Throws: None.
334 //--
FileWriteHeader()335 bool CMICmnLogMediumFile::FileWriteHeader() {
336 return FileWriteEnglish(ConvertCr(m_fileHeaderTxt));
337 }
338
339 //++
340 //------------------------------------------------------------------------------------
341 // Details: Convert any carriage line returns to be compatible with the platform
342 // the
343 // Log file is being written to.
344 // Type: Method.
345 // Args: vData - (R) Text data.
346 // Return: CMIUtilString - Converted string data.
347 // Throws: None.
348 //--
ConvertCr(const CMIUtilString & vData) const349 CMIUtilString CMICmnLogMediumFile::ConvertCr(const CMIUtilString &vData) const {
350 const CMIUtilString strCr("\n");
351 const CMIUtilString &rCrCmpat(GetLineReturn());
352
353 if (strCr == rCrCmpat)
354 return vData;
355
356 const size_t nSizeCmpat(rCrCmpat.size());
357 const size_t nSize(strCr.size());
358 CMIUtilString strConv(vData);
359 size_t pos = strConv.find(strCr);
360 while (pos != CMIUtilString::npos) {
361 strConv.replace(pos, nSize, rCrCmpat);
362 pos = strConv.find(strCr, pos + nSizeCmpat);
363 }
364
365 return strConv;
366 }
367
368 //++
369 //------------------------------------------------------------------------------------
370 // Details: Set the header text that is written to the logger file at the
371 // beginning.
372 // Type: Method.
373 // Args: vText - (R) Text.
374 // Return: MIstatus::success - Functional succeeded.
375 // MIstatus::failure - Functional failed.
376 // Throws: None.
377 //--
SetHeaderTxt(const CMIUtilString & vText)378 bool CMICmnLogMediumFile::SetHeaderTxt(const CMIUtilString &vText) {
379 m_fileHeaderTxt = vText;
380
381 return MIstatus::success;
382 }
383
384 //++
385 //------------------------------------------------------------------------------------
386 // Details: Retrieve the file current carriage line return characters used.
387 // Type: Method.
388 // Args: None.
389 // Return: CMIUtilString & - Text.
390 // Throws: None.
391 //--
GetLineReturn() const392 const CMIUtilString &CMICmnLogMediumFile::GetLineReturn() const {
393 return m_file.GetLineReturn();
394 }
395
396 //++
397 //------------------------------------------------------------------------------------
398 // Details: Set the directory to place the log file.
399 // Type: Method.
400 // Args: vPath - (R) Path to log.
401 // Return: MIstatus::success - Functional succeeded.
402 // MIstatus::failure - Functional failed.
403 // Throws: None.
404 //--
SetDirectory(const CMIUtilString & vPath)405 bool CMICmnLogMediumFile::SetDirectory(const CMIUtilString &vPath) {
406 m_strMediumFileDirectory = vPath;
407
408 return FileFormFileNamePath();
409 }
410