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.

210 lines
6.2 KiB

  1. // LogMsg.cpp: implementation of the LogMsg class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #define _LOGMESSAGE_CPP_
  5. #include "stdafx.h"
  6. #include <windows.h>
  7. #include <tchar.h>
  8. #include <time.h>
  9. #include <stdio.h>
  10. #include "LogMsg.h"
  11. DWORD TCharStringToAnsiString(const TCHAR *tsz ,char *asz);
  12. // maks_todo: is there any standard file to be used for logs.
  13. //////////////////////////////////////////////////////////////////////
  14. // constants
  15. //////////////////////////////////////////////////////////////////////
  16. const UINT LOG_ENTRY_SIZE = 1024;
  17. const UINT S_SIZE = 1024;
  18. LPCTSTR UNINITIALIZED = _T("uninitialized");
  19. //////////////////////////////////////////////////////////////////////
  20. // globals.
  21. ////////////////////////////////////////////////////////////////////////
  22. LogMsg thelog(26); // used by LOGMESSAGE macros.
  23. //////////////////////////////////////////////////////////////////////
  24. // Construction / destruction
  25. ////////////////////////////////////////////////////////////////////////
  26. LogMsg::LogMsg(int value)
  27. {
  28. m_temp = value;
  29. _tcscpy(m_szLogFile, UNINITIALIZED);
  30. }
  31. LogMsg::~LogMsg()
  32. {
  33. LOGMESSAGE0(_T("********Terminating Log"));
  34. }
  35. /*--------------------------------------------------------------------------------------------------------
  36. * DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  37. * creates/opens the szLogFile for logging messages.
  38. * must be called befour using the Log Function.
  39. * -------------------------------------------------------------------------------------------------------*/
  40. DWORD LogMsg::Init(LPCTSTR szLogFile, LPCTSTR szLogModule)
  41. {
  42. ASSERT(szLogFile);
  43. ASSERT(szLogModule);
  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. TCHAR time[S_SIZE];
  53. TCHAR date[S_SIZE];
  54. _tstrdate(date);
  55. _tstrtime(time);
  56. TCHAR output_unicode[LOG_ENTRY_SIZE];
  57. _stprintf(output_unicode, _T("\r\n\r\n*******Initializing Message Log:%s %s %s\r\n\r\n"), m_szLogModule, date, time);
  58. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  59. char output[LOG_ENTRY_SIZE];
  60. TCharStringToAnsiString(output_unicode, output);
  61. // open the log file
  62. HANDLE hfile = CreateFile(m_szLogFile,
  63. GENERIC_WRITE,
  64. 0,
  65. NULL,
  66. OPEN_EXISTING,
  67. 0,
  68. NULL);
  69. if (hfile == INVALID_HANDLE_VALUE)
  70. hfile = CreateFile(m_szLogFile,
  71. GENERIC_WRITE,
  72. 0,
  73. NULL,
  74. CREATE_ALWAYS,
  75. 0,
  76. NULL);
  77. if (hfile != INVALID_HANDLE_VALUE)
  78. {
  79. SetFilePointer(hfile, 0, NULL, FILE_END);
  80. DWORD bytes;
  81. WriteFile(hfile, output, strlen(output) * sizeof(char), &bytes, NULL);
  82. CloseHandle(hfile);
  83. }
  84. return GetLastError();
  85. }
  86. /*--------------------------------------------------------------------------------------------------------
  87. * void log(TCHAR *fmt, ...)
  88. * writes message to the log file. (LOGFILE)
  89. * -------------------------------------------------------------------------------------------------------*/
  90. DWORD LogMsg::Log(LPCTSTR file, int line, TCHAR *fmt, ...)
  91. {
  92. ASSERT(file);
  93. ASSERT(fmt);
  94. ASSERT(_tcscmp(m_szLogFile, UNINITIALIZED) != 0);
  95. // write down file and line info into the buffer..
  96. TCHAR fileline_unicode[LOG_ENTRY_SIZE];
  97. _stprintf(fileline_unicode, _T("%s:File:%s Line:%d:"), m_szLogModule, file, line);
  98. char fileline[LOG_ENTRY_SIZE];
  99. TCharStringToAnsiString(fileline_unicode, fileline);
  100. // create the output string
  101. TCHAR output_unicode[LOG_ENTRY_SIZE];
  102. va_list vaList;
  103. va_start(vaList, fmt);
  104. _vstprintf(output_unicode, fmt, vaList);
  105. va_end(vaList);
  106. ASSERT(_tcslen(output_unicode) < LOG_ENTRY_SIZE);
  107. char output[LOG_ENTRY_SIZE];
  108. TCharStringToAnsiString(output_unicode, output);
  109. ASSERT(strlen(output) < LOG_ENTRY_SIZE);
  110. // open the log file
  111. HANDLE hfile = CreateFile(m_szLogFile,
  112. GENERIC_WRITE,
  113. 0,
  114. NULL,
  115. OPEN_EXISTING,
  116. 0,
  117. NULL);
  118. if (hfile != INVALID_HANDLE_VALUE)
  119. {
  120. SetFilePointer(hfile, 0, NULL, FILE_END);
  121. DWORD bytes;
  122. const LPCSTR CRLF = "\r\n";
  123. WriteFile(hfile, fileline, strlen(fileline) * sizeof(char), &bytes, NULL);
  124. WriteFile(hfile, output, strlen(output) * sizeof(char), &bytes, NULL);
  125. WriteFile(hfile, CRLF, strlen(CRLF) * sizeof(char), &bytes, NULL);
  126. CloseHandle(hfile);
  127. }
  128. return GetLastError();
  129. }
  130. /*--------------------------------------------------------------------------------------------------------
  131. * TCharStringToAnsiString(const TCHAR *tsz ,char *asz)
  132. * converts the given TCHAR * to char *
  133. * -------------------------------------------------------------------------------------------------------*/
  134. DWORD TCharStringToAnsiString(const TCHAR *tsz ,char *asz)
  135. {
  136. DWORD count;
  137. ASSERT(tsz && asz);
  138. #ifdef UNICODE
  139. count = WideCharToMultiByte(CP_ACP,
  140. 0,
  141. tsz,
  142. -1,
  143. NULL,
  144. 0,
  145. NULL,
  146. NULL);
  147. if (!count || count > S_SIZE)
  148. return count;
  149. return WideCharToMultiByte(CP_ACP,
  150. 0,
  151. tsz,
  152. -1,
  153. asz,
  154. count,
  155. NULL,
  156. NULL);
  157. #else
  158. _tcscpy(asz, tsz);
  159. return _tcslen(asz);
  160. #endif
  161. }
  162. // EOF