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.

263 lines
7.6 KiB

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