Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
7.5 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. // LogMsg.cpp: implementation of the LogMsg class.
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5. #define _LOGMESSAGE_CPP_
  6. #include "stdafx.h"
  7. #include "LogMsg.h"
  8. DWORD TCharStringToAnsiString(const TCHAR *tsz ,char *asz);
  9. // maks_todo: is there any standard file to be used for logs.
  10. //////////////////////////////////////////////////////////////////////
  11. // constants
  12. //////////////////////////////////////////////////////////////////////
  13. const UINT LOG_ENTRY_SIZE = 1024;
  14. const UINT STAMP_SIZE = 1024;
  15. LPCTSTR UNINITIALIZED = _T("uninitialized");
  16. //////////////////////////////////////////////////////////////////////
  17. // globals.
  18. ////////////////////////////////////////////////////////////////////////
  19. LogMsg thelog(26); // used by LOGMESSAGE macros.
  20. //////////////////////////////////////////////////////////////////////
  21. // Construction / destruction
  22. ////////////////////////////////////////////////////////////////////////
  23. LogMsg::LogMsg(int value)
  24. {
  25. m_temp = value;
  26. _tcscpy(m_szLogFile, UNINITIALIZED);
  27. }
  28. LogMsg::~LogMsg()
  29. {
  30. LOGMESSAGE0(_T("********Terminating Log"));
  31. }
  32. /*--------------------------------------------------------------------------------------------------------
  33. * DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  34. * creates/opens the szLogFile for logging messages.
  35. * must be called befour using the Log Function.
  36. * -------------------------------------------------------------------------------------------------------*/
  37. DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  38. {
  39. USES_CONVERSION;
  40. ASSERT(szLogFile);
  41. ASSERT(szLogModule);
  42. // dont call this function twice.
  43. // maks_todo:why is the constructor not getting called?
  44. // maks_todo:enable this assert.
  45. //ASSERT(_tcscmp(m_szLogFile, UNINITIALIZED) == 0);
  46. ASSERT(_tcslen(szLogFile) < MAX_PATH);
  47. ASSERT(_tcslen(szLogModule) < MAX_PATH);
  48. _tcscpy(m_szLogFile, szLogFile);
  49. _tcscpy(m_szLogModule, szLogModule);
  50. // open the log file
  51. HANDLE hfile = CreateFile(m_szLogFile,
  52. GENERIC_WRITE,
  53. 0,
  54. NULL,
  55. OPEN_EXISTING,
  56. 0,
  57. NULL);
  58. if (hfile == INVALID_HANDLE_VALUE)
  59. hfile = CreateFile(m_szLogFile,
  60. GENERIC_WRITE,
  61. 0,
  62. NULL,
  63. CREATE_ALWAYS,
  64. 0,
  65. NULL);
  66. if (hfile != INVALID_HANDLE_VALUE)
  67. {
  68. // lets prepare for writing to the file.
  69. SetFilePointer(hfile, 0, NULL, FILE_END);
  70. DWORD bytes;
  71. // get the current time/date stamp.
  72. TCHAR time[STAMP_SIZE];
  73. TCHAR date[STAMP_SIZE];
  74. TCHAR output_unicode[LOG_ENTRY_SIZE];
  75. _tstrdate(date);
  76. _tstrtime(time);
  77. _stprintf(output_unicode, _T("\r\n\r\n*******Initializing Message Log:%s %s %s\r\n"), m_szLogModule, date, time);
  78. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  79. // TCharStringToAnsiString(output_unicode, output);
  80. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  81. // now write some more info about the version etc.
  82. OSVERSIONINFO OsV;
  83. OsV.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  84. if (GetVersionEx(&OsV)== 0)
  85. {
  86. // get version failed.
  87. _stprintf(output_unicode, _T("GetVersionEx failed, ErrrorCode = %lu\r\n"), GetLastError());
  88. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  89. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  90. }
  91. else
  92. {
  93. //
  94. // ok we have the version info, write it out
  95. //
  96. _stprintf(output_unicode, _T("*******Version:Major=%lu, Minor=%lu, Build=%lu, PlatForm=%lu, CSDVer=%s, %s\r\n\r\n"),
  97. OsV.dwMajorVersion,
  98. OsV.dwMinorVersion,
  99. OsV.dwBuildNumber,
  100. OsV.dwPlatformId,
  101. OsV.szCSDVersion,
  102. #ifdef DBG
  103. _T("Checked")
  104. #else
  105. _T("Free")
  106. #endif
  107. );
  108. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  109. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  110. }
  111. CloseHandle(hfile);
  112. }
  113. return GetLastError();
  114. }
  115. /*--------------------------------------------------------------------------------------------------------
  116. * void log(TCHAR *fmt, ...)
  117. * writes message to the log file. (LOGFILE)
  118. * -------------------------------------------------------------------------------------------------------*/
  119. DWORD LogMsg::Log(LPCTSTR file, int line, TCHAR *fmt, ...)
  120. {
  121. USES_CONVERSION;
  122. ASSERT(file);
  123. ASSERT(fmt);
  124. ASSERT(_tcscmp(m_szLogFile, UNINITIALIZED) != 0);
  125. // write down file and line info into the buffer..
  126. TCHAR fileline_unicode[LOG_ENTRY_SIZE];
  127. // file is actually full path to the file.
  128. ASSERT(_tcschr(file, '\\'));
  129. // we want to print only file name not full path
  130. UINT uiFileLen = _tcslen(file);
  131. while (uiFileLen && *(file + uiFileLen - 1) != '\\')
  132. {
  133. uiFileLen--;
  134. }
  135. ASSERT(uiFileLen);
  136. _stprintf(fileline_unicode, _T("%s(%d)"), (file+uiFileLen), line);
  137. // create the output string
  138. TCHAR output_unicode[LOG_ENTRY_SIZE];
  139. va_list vaList;
  140. va_start(vaList, fmt);
  141. _vstprintf(output_unicode, fmt, vaList);
  142. va_end(vaList);
  143. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  144. // open the log file
  145. HANDLE hfile = CreateFile(m_szLogFile,
  146. GENERIC_WRITE,
  147. 0,
  148. NULL,
  149. OPEN_EXISTING,
  150. 0,
  151. NULL);
  152. if (hfile != INVALID_HANDLE_VALUE)
  153. {
  154. SetFilePointer(hfile, 0, NULL, FILE_END);
  155. DWORD bytes;
  156. const LPCSTR CRLF = "\r\n";
  157. WriteFile(hfile, T2A(fileline_unicode), _tcslen(fileline_unicode), &bytes, NULL);
  158. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  159. WriteFile(hfile, CRLF, strlen(CRLF) * sizeof(char), &bytes, NULL);
  160. CloseHandle(hfile);
  161. }
  162. return GetLastError();
  163. }
  164. /*--------------------------------------------------------------------------------------------------------
  165. * TCharStringToAnsiString(const TCHAR *tsz ,char *asz)
  166. * converts the given TCHAR * to char *
  167. * -------------------------------------------------------------------------------------------------------*/
  168. DWORD TCharStringToAnsiString(const TCHAR *tsz ,char *asz)
  169. {
  170. ASSERT(tsz && asz);
  171. #ifdef UNICODE
  172. DWORD count;
  173. count = WideCharToMultiByte(CP_ACP,
  174. 0,
  175. tsz,
  176. -1,
  177. NULL,
  178. 0,
  179. NULL,
  180. NULL);
  181. if (!count || count > STAMP_SIZE)
  182. return count;
  183. return WideCharToMultiByte(CP_ACP,
  184. 0,
  185. tsz,
  186. -1,
  187. asz,
  188. count,
  189. NULL,
  190. NULL);
  191. #else
  192. _tcscpy(asz, tsz);
  193. return _tcslen(asz);
  194. #endif
  195. }
  196. // EOF