1 //===-- MICmnLogMediumFile.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "MICmnLogMediumFile.h"
12 #include "MICmnResources.h"
14 //++ ------------------------------------------------------------------------------------
15 // Details: CMICmnLogMediumFile constructor.
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))
33 //++ ------------------------------------------------------------------------------------
34 // Details: CMICmnLogMediumFile destructor.
40 CMICmnLogMediumFile::~CMICmnLogMediumFile()
44 //++ ------------------------------------------------------------------------------------
45 // Details: Get the singleton instance of *this class.
48 // Return: CMICmnLogMediumFile - Reference to *this object.
52 CMICmnLogMediumFile::Instance()
54 static CMICmnLogMediumFile instance;
59 //++ ------------------------------------------------------------------------------------
60 // Details: Initialize setup *this medium ready for use.
63 // Return: MIstatus::success - Functional succeeded.
64 // MIstatus::failure - Functional failed.
68 CMICmnLogMediumFile::Initialize()
70 m_bInitialized = true;
71 return FileFormFileNamePath();
74 //++ ------------------------------------------------------------------------------------
75 // Details: Unbind detach or release resources used by *this medium.
82 CMICmnLogMediumFile::Shutdown()
86 m_bInitialized = false;
89 return MIstatus::success;
92 //++ ------------------------------------------------------------------------------------
93 // Details: Retrieve the name of *this medium.
96 // Return: CMIUtilString - Text data.
100 CMICmnLogMediumFile::GetName() const
102 return m_constThisMediumName;
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.
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.
119 CMICmnLogMediumFile::Write(const CMIUtilString &vData, const CMICmnLog::ELogVerbosity veType)
121 if (m_bInitialized && m_file.IsOk())
123 const bool bDoWrite = (m_eVerbosityType & veType);
126 bool bNewCreated = false;
127 bool bOk = m_file.CreateWrite(m_fileNamePath, bNewCreated);
131 bOk = FileWriteHeader();
132 bOk = bOk && FileWriteEnglish(MassagedData(vData, veType));
138 return MIstatus::failure;
141 //++ ------------------------------------------------------------------------------------
142 // Details: Retrieve *this medium's last error condition.
145 // Return: CString & - Text description.
148 const CMIUtilString &
149 CMICmnLogMediumFile::GetError() const
151 return m_strMILastErrorDescription;
154 //++ ------------------------------------------------------------------------------------
155 // Details: Set the verbosity mode for this medium.
157 // Args: veType - (R) Mask value.
158 // Return: MIstatus::success - Functional succeeded.
159 // MIstatus::failure - Functional failed.
163 CMICmnLogMediumFile::SetVerbosity(const MIuint veType)
165 m_eVerbosityType = veType;
166 return MIstatus::success;
169 //++ ------------------------------------------------------------------------------------
170 // Details: Get the verbosity mode for this medium.
172 // Args: veType - (R) Mask value.
173 // Return: CMICmnLog::ELogVerbosity - Mask value.
177 CMICmnLogMediumFile::GetVerbosity() const
179 return m_eVerbosityType;
182 //++ ------------------------------------------------------------------------------------
183 // Details: Write data to a file English font.
185 // Args: vData - (R) The data to write to the logger.
190 CMICmnLogMediumFile::FileWriteEnglish(const CMIUtilString &vData)
192 return m_file.Write(vData);
195 //++ ------------------------------------------------------------------------------------
196 // Details: Determine and form the medium file's directory path and name.
199 // Return: MIstatus::success - Functional succeeded.
200 // MIstatus::failure - Functional failed.
204 CMICmnLogMediumFile::FileFormFileNamePath()
206 ClrErrorDescription();
208 m_fileNamePath = MIRSRC(IDS_MEDIUMFILE_ERR_INVALID_PATH);
210 CMIUtilDateTimeStd date;
211 m_strMediumFileName = CMIUtilString::Format(m_constMediumFileNameFormat.c_str(), date.GetDateTimeLogFilename().c_str());
213 #if defined(_MSC_VER)
214 m_fileNamePath = CMIUtilString::Format("%s\\%s", m_strMediumFileDirectory.c_str(), m_strMediumFileName.c_str());
216 m_fileNamePath = CMIUtilString::Format("%s/%s", m_strMediumFileDirectory.c_str(), m_strMediumFileName.c_str());
217 #endif // defined ( _MSC_VER )
219 return MIstatus::success;
222 //++ ------------------------------------------------------------------------------------
223 // Details: Retrieve the medium file's directory path and name.
226 // Return: CMIUtilString & - File path.
229 const CMIUtilString &
230 CMICmnLogMediumFile::GetFileNamePath() const
232 return m_fileNamePath;
235 //++ ------------------------------------------------------------------------------------
236 // Details: Retrieve the medium file's name.
239 // Return: CMIUtilString & - File name.
242 const CMIUtilString &
243 CMICmnLogMediumFile::GetFileName() const
245 return m_strMediumFileName;
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.
253 // Args: vData - (R) Raw data.
254 // veType - (R) Message type.
255 // Return: CMIUtilString - Massaged data.
259 CMICmnLogMediumFile::MassagedData(const CMIUtilString &vData, const CMICmnLog::ELogVerbosity veType)
261 const CMIUtilString strCr("\n");
263 const char verbosityCode(ConvertLogVerbosityTypeToId(veType));
264 const CMIUtilString dt(CMIUtilString::Format("%s %s", m_strDate.c_str(), m_dateTime.GetTime().c_str()));
266 data = CMIUtilString::Format("%c,%s,%s", verbosityCode, dt.c_str(), vData.c_str());
267 data = ConvertCr(data);
270 const size_t pos = vData.rfind(strCr);
271 if (pos == vData.size())
274 // ... did not have an EOL so add one
275 data += GetLineReturn();
280 //++ ------------------------------------------------------------------------------------
281 // Details: Convert the Log's verbosity type number into a single char character.
283 // Args: veType - (R) Message type.
284 // Return: wchar_t - A letter.
288 CMICmnLogMediumFile::ConvertLogVerbosityTypeToId(const CMICmnLog::ELogVerbosity veType) const
294 MIuint number(veType);
297 number = number >> 1;
310 //++ ------------------------------------------------------------------------------------
311 // Details: Retrieve state of whether the file medium is ok.
314 // Return: True - file ok.
315 // False - file has a problem.
319 CMICmnLogMediumFile::IsOk() const
321 return m_file.IsOk();
324 //++ ------------------------------------------------------------------------------------
325 // Details: Status on the file log medium existing already.
328 // Return: True - Exists.
329 // False - Not found.
333 CMICmnLogMediumFile::IsFileExist() const
335 return m_file.IsFileExist(GetFileNamePath());
338 //++ ------------------------------------------------------------------------------------
339 // Details: Write the header text the logger file.
341 // Args: vText - (R) Text.
342 // Return: MIstatus::success - Functional succeeded.
343 // MIstatus::failure - Functional failed.
347 CMICmnLogMediumFile::FileWriteHeader()
349 return FileWriteEnglish(ConvertCr(m_fileHeaderTxt));
352 //++ ------------------------------------------------------------------------------------
353 // Details: Convert any carriage line returns to be compatible with the platform the
354 // Log fiel is being written to.
356 // Args: vData - (R) Text data.
357 // Return: CMIUtilString - Converted string data.
361 CMICmnLogMediumFile::ConvertCr(const CMIUtilString &vData) const
363 const CMIUtilString strCr("\n");
364 const CMIUtilString &rCrCmpat(GetLineReturn());
366 if (strCr == rCrCmpat)
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)
375 strConv.replace(pos, nSize, rCrCmpat);
376 pos = strConv.find(strCr, pos + nSizeCmpat);
382 //++ ------------------------------------------------------------------------------------
383 // Details: Set the header text that is written to the logger file at the beginning.
385 // Args: vText - (R) Text.
386 // Return: MIstatus::success - Functional succeeded.
387 // MIstatus::failure - Functional failed.
391 CMICmnLogMediumFile::SetHeaderTxt(const CMIUtilString &vText)
393 m_fileHeaderTxt = vText;
395 return MIstatus::success;
398 //++ ------------------------------------------------------------------------------------
399 // Details: Retrieve the file current carriage line return characters used.
402 // Return: CMIUtilString & - Text.
405 const CMIUtilString &
406 CMICmnLogMediumFile::GetLineReturn() const
408 return m_file.GetLineReturn();
411 //++ ------------------------------------------------------------------------------------
412 // Details: Set the directory to place the log file.
414 // Args: vPath - (R) Path to log.
415 // Return: MIstatus::success - Functional succeeded.
416 // MIstatus::failure - Functional failed.
420 CMICmnLogMediumFile::SetDirectory(const CMIUtilString &vPath)
422 m_strMediumFileDirectory = vPath;
424 return FileFormFileNamePath();