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.

228 lines
5.2 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. str.cpp
  5. Abstract:
  6. Author:
  7. Vlad Sadovsky (vlads) 26-Jan-1997
  8. Revision History:
  9. 26-Jan-1997 VladS created
  10. --*/
  11. #include "cplusinc.h"
  12. #include "sticomm.h"
  13. extern "C" {
  14. static CHAR szFmt0[] = "File %.40s, Line %u";
  15. static CHAR szFmt1[] = "%.60s: File %.40s, Line %u";
  16. static CHAR szMBCaption[] = "ASSERTION FAILED";
  17. static TCHAR szFAE[] = TEXT("ASSERTION FAILURE IN APP");
  18. BOOL fDoMessageBox = FALSE;
  19. VOID UIAssertHelper(
  20. const CHAR* pszFileName,
  21. UINT nLine )
  22. {
  23. CHAR szBuff[sizeof(szFmt0)+60+40] = {0};
  24. _snprintf(szBuff, sizeof(szBuff) / sizeof(szBuff[0]) - 1, szFmt0, pszFileName, nLine);
  25. MessageBoxA(NULL, szBuff, szMBCaption,
  26. (MB_TASKMODAL | MB_ICONSTOP | MB_OK) );
  27. FatalAppExit(0, szFAE);
  28. }
  29. VOID UIAssertSzHelper(
  30. const TCHAR* pszMessage,
  31. const CHAR* pszFileName,
  32. UINT nLine )
  33. {
  34. CHAR szBuff[sizeof(szFmt1)+60+40] = {0};
  35. _snprintf(szBuff, sizeof(szBuff) / sizeof(szBuff[0]) - 1, szFmt1, pszMessage, pszFileName, nLine);
  36. MessageBoxA(NULL, szBuff, szMBCaption,
  37. (MB_TASKMODAL | MB_ICONSTOP | MB_OK) );
  38. FatalAppExit(0, szFAE);
  39. }
  40. VOID AssertHelper(
  41. const CHAR* pszFileName,
  42. UINT nLine )
  43. {
  44. //DPRINTF(DM_ASSERT,szFmt0, pszFileName, nLine);
  45. Break();
  46. }
  47. VOID AssertSzHelper(
  48. const TCHAR* pszMessage,
  49. const CHAR* pszFileName,
  50. UINT nLine )
  51. {
  52. //DPRINTF(DM_ASSERT,szFmt1, pszMessage, pszFileName, nLine);
  53. Break();
  54. }
  55. //========== Debug output routines =========================================
  56. UINT uiStiDebugMask = 0xffff;
  57. UINT WINAPI StiSetDebugMask(UINT mask)
  58. {
  59. #ifdef DEBUG
  60. UINT uiOld = uiStiDebugMask;
  61. uiStiDebugMask = mask;
  62. return uiOld;
  63. #else
  64. return 0;
  65. #endif
  66. }
  67. UINT WINAPI StiGetDebugMask()
  68. {
  69. #ifdef DEBUG
  70. return uiStiDebugMask;
  71. #else
  72. return 0;
  73. #endif
  74. }
  75. #ifndef WINCAPI
  76. #define WINCAPI __cdecl
  77. #endif
  78. #ifdef DEBUG
  79. /* debug message output log file */
  80. UINT g_uSpewLine = 0;
  81. PCTSTR g_pcszSpewFile = NULL;
  82. TCHAR s_cszLogFile[MAX_PATH] = {'\0'};
  83. TCHAR s_cszDebugName[MAX_PATH] = {'\0'};
  84. UINT WINAPI StiSetDebugParameters(PTSTR pszName,PTSTR pszLogFile)
  85. {
  86. if (pszLogFile)
  87. {
  88. lstrcpyn(s_cszLogFile,pszLogFile, sizeof(s_cszLogFile) / sizeof(s_cszLogFile[0]) - 1);
  89. }
  90. if (pszName)
  91. {
  92. lstrcpyn(s_cszDebugName,pszName, sizeof(s_cszDebugName) / sizeof(s_cszDebugName[0]) - 1);
  93. }
  94. return 0;
  95. }
  96. BOOL LogOutputDebugString(PCTSTR pcsz)
  97. {
  98. BOOL bResult = FALSE;
  99. UINT ucb;
  100. TCHAR rgchLogFile[MAX_PATH + 1] = {0};
  101. //if (IS_EMPTY_STRING(s_cszLogFile) )
  102. // return FALSE;
  103. DWORD dwCharsAvailable = (sizeof(rgchLogFile) / sizeof(rgchLogFile[0])) - lstrlen(TEXT("\\")) - lstrlen(s_cszLogFile);
  104. ucb = ExpandEnvironmentStrings(TEXT("USERPROFILE"),
  105. rgchLogFile,
  106. dwCharsAvailable);
  107. rgchLogFile[sizeof(rgchLogFile) / sizeof(rgchLogFile[0]) - 1] = TEXT('\0');
  108. if (ucb > 0 && ucb < sizeof(rgchLogFile) && *s_cszLogFile) {
  109. HANDLE hfLog;
  110. lstrcat(rgchLogFile, TEXT("\\"));
  111. lstrcat(rgchLogFile, s_cszLogFile);
  112. hfLog = ::CreateFile(rgchLogFile,
  113. GENERIC_WRITE,
  114. 0,
  115. NULL,
  116. OPEN_ALWAYS,
  117. 0,
  118. NULL);
  119. if (hfLog != INVALID_HANDLE_VALUE) {
  120. if (SetFilePointer(hfLog, 0, NULL, FILE_END) != INVALID_FILE_SIZE) {
  121. DWORD dwcbWritten;
  122. bResult = WriteFile(hfLog, pcsz, lstrlen(pcsz), &dwcbWritten, NULL);
  123. if (! CloseHandle(hfLog) && bResult)
  124. bResult = FALSE;
  125. }
  126. }
  127. }
  128. return(bResult);
  129. }
  130. TCHAR *achDebugDisplayPrefix[] = {TEXT("t "),TEXT("w "),TEXT("e "),TEXT("a "),TEXT("t "),TEXT("t "),TEXT("t "),TEXT("t "),TEXT("t "),TEXT("t "),TEXT("t ")};
  131. void WINCAPI StiDebugMsg(UINT mask, LPCTSTR pszMsg, ...)
  132. {
  133. TCHAR ach[1024] = {0};
  134. UINT uiDisplayMask = mask & 0xff;
  135. CSimpleString csLogString;
  136. va_list list;
  137. va_start (list, pszMsg);
  138. // Determine prefix
  139. *ach = TEXT('\0');
  140. if (uiStiDebugMask & DM_PREFIX) {
  141. // Add trace type
  142. csLogString += achDebugDisplayPrefix[uiDisplayMask];
  143. // Add component name
  144. csLogString += s_cszDebugName;
  145. // Add thread ID
  146. TCHAR szThreadId[16];
  147. ::wsprintf(szThreadId,TEXT("[%#lx] "),::GetCurrentThreadId());
  148. csLogString += szThreadId;
  149. }
  150. ::_vsntprintf(ach, sizeof(ach)/sizeof(ach[0]), pszMsg, list);
  151. ach[sizeof(ach)/sizeof(ach[0]) - 1] = TEXT('\0');
  152. va_end(list);
  153. csLogString += ach;
  154. csLogString += TEXT("\r\n");
  155. if (uiStiDebugMask & DM_LOG_FILE) {
  156. LogOutputDebugString(csLogString.String());
  157. }
  158. // Check if we need to display this trace
  159. if (uiStiDebugMask & uiDisplayMask) {
  160. OutputDebugString(csLogString.String());
  161. }
  162. }
  163. #endif
  164. } /* extern "C" */