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