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.

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