Leaked source code of windows server 2003
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
8.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_bInitialized = false;
  26. }
  27. LogMsg::~LogMsg()
  28. {
  29. LOGMESSAGE0(_T("********Terminating Log."));
  30. }
  31. /*--------------------------------------------------------------------------------------------------------
  32. * DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  33. * creates/opens the szLogFile for logging messages.
  34. * must be called befour using the Log Function.
  35. * -------------------------------------------------------------------------------------------------------*/
  36. DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  37. {
  38. USES_CONVERSION;
  39. ASSERT(szLogFile);
  40. ASSERT(szLogModule);
  41. // dont call this function twice.
  42. // maks_todo:why is the constructor not getting called?
  43. // maks_todo:enable this assert.
  44. //ASSERT(_tcscmp(m_szLogFile, UNINITIALIZED) == 0);
  45. ASSERT(_tcslen(szLogFile) < MAX_PATH);
  46. ASSERT(_tcslen(szLogModule) < MAX_PATH);
  47. _tcsncpy(m_szLogFile, szLogFile, sizeof(m_szLogFile)/sizeof(m_szLogFile[0]) -1);
  48. _tcsncpy(m_szLogModule, szLogModule, sizeof(m_szLogModule)/sizeof(m_szLogModule[0]) -1);
  49. m_szLogFile[sizeof(m_szLogFile)/sizeof(m_szLogFile[0]) -1] = NULL;
  50. m_szLogModule[sizeof(m_szLogModule)/sizeof(m_szLogModule[0]) -1] = NULL;
  51. // open the log file
  52. HANDLE hfile = CreateFile(m_szLogFile,
  53. GENERIC_WRITE,
  54. 0,
  55. NULL,
  56. OPEN_EXISTING,
  57. 0,
  58. NULL);
  59. if (hfile == INVALID_HANDLE_VALUE)
  60. hfile = CreateFile(m_szLogFile,
  61. GENERIC_WRITE,
  62. 0,
  63. NULL,
  64. CREATE_ALWAYS,
  65. 0,
  66. NULL);
  67. if (hfile != INVALID_HANDLE_VALUE)
  68. {
  69. // lets prepare for writing to the file.
  70. SetFilePointer(hfile, 0, NULL, FILE_END);
  71. DWORD bytes;
  72. // get the current time/date stamp.
  73. TCHAR time[STAMP_SIZE];
  74. TCHAR date[STAMP_SIZE];
  75. TCHAR output_unicode[LOG_ENTRY_SIZE];
  76. _tstrdate(date);
  77. _tstrtime(time);
  78. _sntprintf(output_unicode, sizeof(output_unicode)/sizeof(output_unicode[0]) -1, _T("\r\n\r\n*******Initializing Message Log:%s %s %s\r\n"), m_szLogModule, date, time);
  79. output_unicode[sizeof(output_unicode)/sizeof(output_unicode[0]) -1] = NULL;
  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. _sntprintf(output_unicode, sizeof(output_unicode)/sizeof(output_unicode[0]) -1, _T("GetVersionEx failed, ErrrorCode = %lu\r\n"), GetLastError());
  90. output_unicode[sizeof(output_unicode)/sizeof(output_unicode[0]) -1] = NULL;
  91. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  92. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  93. }
  94. else
  95. {
  96. //
  97. // ok we have the version info, write it out
  98. //
  99. _sntprintf(output_unicode, sizeof(output_unicode)/sizeof(output_unicode[0]) -1, _T("*******Version:Major=%lu, Minor=%lu, Build=%lu, PlatForm=%lu, CSDVer=%s, %s\r\n\r\n"),
  100. OsV.dwMajorVersion,
  101. OsV.dwMinorVersion,
  102. OsV.dwBuildNumber,
  103. OsV.dwPlatformId,
  104. OsV.szCSDVersion,
  105. #ifdef DBG
  106. _T("Checked")
  107. #else
  108. _T("Free")
  109. #endif
  110. );
  111. output_unicode[sizeof(output_unicode)/sizeof(output_unicode[0]) -1] = NULL;
  112. WriteFile(hfile, T2A(output_unicode), _tcslen(output_unicode), &bytes, NULL);
  113. }
  114. m_bInitialized = true;
  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. if (!m_bInitialized)
  126. return 0;
  127. USES_CONVERSION;
  128. ASSERT(file);
  129. ASSERT(fmt);
  130. ASSERT(_tcscmp(m_szLogFile, UNINITIALIZED) != 0);
  131. // write down file and line info into the buffer..
  132. TCHAR fileline_unicode[LOG_ENTRY_SIZE];
  133. // file is actually full path to the file.
  134. ASSERT(_tcschr(file, '\\'));
  135. // we want to print only file name not full path
  136. UINT uiFileLen = _tcslen(file);
  137. while (uiFileLen && *(file + uiFileLen - 1) != '\\')
  138. {
  139. uiFileLen--;
  140. }
  141. ASSERT(uiFileLen);
  142. _sntprintf(fileline_unicode, sizeof(fileline_unicode)/sizeof(fileline_unicode[0]) -1, _T("%s(%d)"), (file+uiFileLen), line);
  143. fileline_unicode[sizeof(fileline_unicode)/sizeof(fileline_unicode[0]) -1] = NULL;
  144. // create the output string
  145. TCHAR output_unicode[LOG_ENTRY_SIZE];
  146. va_list vaList;
  147. va_start(vaList, fmt);
  148. _vsntprintf(output_unicode, sizeof(output_unicode)/sizeof(output_unicode[0]) -1, fmt, vaList);
  149. va_end(vaList);
  150. output_unicode[sizeof(output_unicode)/sizeof(output_unicode[0]) -1] = NULL;
  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