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 "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_iIsInitialized = 0;
  26. _tcscpy(m_szLogFile, UNINITIALIZED);
  27. }
  28. LogMsg::~LogMsg()
  29. {
  30. if (IsInitialized())
  31. LOGMESSAGE0(_T("********Terminating Log"));
  32. }
  33. /*--------------------------------------------------------------------------------------------------------
  34. * DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  35. * creates/opens the szLogFile for logging messages.
  36. * must be called befour using the Log Function.
  37. * -------------------------------------------------------------------------------------------------------*/
  38. DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  39. {
  40. USES_CONVERSION;
  41. ASSERT(szLogFile);
  42. ASSERT(szLogModule);
  43. ASSERT(!IsInitialized());
  44. // dont call this function twice.
  45. // maks_todo:why is the constructor not getting called?
  46. // maks_todo:enable this assert.
  47. ASSERT(_tcscmp(m_szLogFile, UNINITIALIZED) == 0);
  48. ASSERT(_tcslen(szLogFile) < MAX_PATH);
  49. ASSERT(_tcslen(szLogModule) < MAX_PATH);
  50. _tcscpy(m_szLogFile, szLogFile);
  51. _tcscpy(m_szLogModule, szLogModule);
  52. // open the log file
  53. HANDLE hfile = CreateFile(m_szLogFile,
  54. GENERIC_WRITE,
  55. 0,
  56. NULL,
  57. OPEN_EXISTING,
  58. 0,
  59. NULL);
  60. if (hfile == INVALID_HANDLE_VALUE)
  61. hfile = CreateFile(m_szLogFile,
  62. GENERIC_WRITE,
  63. 0,
  64. NULL,
  65. CREATE_ALWAYS,
  66. 0,
  67. NULL);
  68. if (hfile != INVALID_HANDLE_VALUE)
  69. {
  70. // lets prepare for writing to the file.
  71. SetFilePointer(hfile, 0, NULL, FILE_END);
  72. DWORD bytes;
  73. // get the current time/date stamp.
  74. TCHAR time[STAMP_SIZE];
  75. TCHAR date[STAMP_SIZE];
  76. TCHAR output_unicode[LOG_ENTRY_SIZE];
  77. _tstrdate(date);
  78. _tstrtime(time);
  79. _stprintf(output_unicode, _T("\r\n\r\n*******Initializing Message Log:%s %s %s\r\n"), m_szLogModule, date, time);
  80. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  81. // TCharStringToAnsiString(output_unicode, output);
  82. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  83. // now write some more info about the version etc.
  84. OSVERSIONINFO OsV;
  85. OsV.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  86. if (GetVersionEx(&OsV)== 0)
  87. {
  88. // get version failed.
  89. _stprintf(output_unicode, _T("GetVersionEx failed, ErrrorCode = %lu\r\n"), GetLastError());
  90. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  91. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  92. }
  93. else
  94. {
  95. //
  96. // ok we have the version info, write it out
  97. //
  98. _stprintf(output_unicode, _T("*******Version:Major=%lu, Minor=%lu, Build=%lu, PlatForm=%lu, CSDVer=%s, %s\r\n\r\n"),
  99. OsV.dwMajorVersion,
  100. OsV.dwMinorVersion,
  101. OsV.dwBuildNumber,
  102. OsV.dwPlatformId,
  103. OsV.szCSDVersion,
  104. #ifdef DBG
  105. _T("Checked")
  106. #else
  107. _T("Free")
  108. #endif
  109. );
  110. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  111. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  112. }
  113. CloseHandle(hfile);
  114. m_iIsInitialized = LOG_INITIALIZED;
  115. }
  116. return GetLastError();
  117. }
  118. BOOL LogMsg::IsInitialized ()
  119. {
  120. return m_iIsInitialized == LOG_INITIALIZED;
  121. }
  122. /*--------------------------------------------------------------------------------------------------------
  123. * void log(TCHAR *fmt, ...)
  124. * writes message to the log file. (LOGFILE)
  125. * -------------------------------------------------------------------------------------------------------*/
  126. DWORD LogMsg::Log(LPCTSTR file, int line, TCHAR *fmt, ...)
  127. {
  128. ASSERT(IsInitialized());
  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