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.

272 lines
5.0 KiB

  1. //-----------------------------------------------------------------------------
  2. // File: cfguitrace.cpp
  3. //
  4. // Desc: Contains all trace functionalities used by the UI.
  5. // Define __CFGUI_TRACE__TO_FILE to have output written to a file.
  6. // Define __CFGUI_TRACE__TO_DEBUG_OUT to direct output to a debugger.
  7. // These two symbols can coexist, and are defined in defines.h.
  8. //
  9. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  10. //-----------------------------------------------------------------------------
  11. #include "common.hpp"
  12. #ifndef NTRACE
  13. const int mindepth = 0;
  14. static int depth = mindepth;
  15. static int filedepth = 0;
  16. static FILE *file = NULL;
  17. __cfgui_out_filescope::__cfgui_out_filescope(bool bInternal)
  18. : m_bInternal(bInternal)
  19. {
  20. #ifdef __CFGUI_TRACE__TO_FILE
  21. static bool bFirst = true;
  22. filedepth++;
  23. if (filedepth == 1)
  24. {
  25. assert(file == NULL);
  26. if (file == NULL)
  27. file = fopen("c:\\cfguilog.txt", bFirst ? "w+t" : "a+t");
  28. assert(file != NULL);
  29. if (file != NULL)
  30. {
  31. if (bFirst)
  32. {
  33. time_t curtime;
  34. time(&curtime);
  35. LPSTR str = _strdup(ctime(&curtime));
  36. if (str != NULL)
  37. {
  38. LPSTR last = str + strlen(str) - 1;
  39. if (last >= str && *last == '\n')
  40. *last = 0;
  41. }
  42. fprintf(file,
  43. "\n"
  44. "\n"
  45. "\n"
  46. "--------------------------------------------------------------------------------\n"
  47. "DInput Mapper Device Configuration UI\n"
  48. "New logfile session started at %s.\n"
  49. "--------------------------------------------------------------------------------\n"
  50. "\n"
  51. , str);
  52. free(str);
  53. }
  54. bFirst = false;
  55. }
  56. }
  57. #endif
  58. }
  59. __cfgui_out_filescope::~__cfgui_out_filescope()
  60. {
  61. #ifdef __CFGUI_TRACE__TO_FILE
  62. assert(filedepth > 0);
  63. if (filedepth < 0)
  64. filedepth = 0;
  65. if (filedepth > 0)
  66. {
  67. filedepth--;
  68. assert(file != NULL);
  69. if (file != NULL)
  70. {
  71. if (filedepth == 0)
  72. {
  73. fclose(file);
  74. file = NULL;
  75. }
  76. else if (!m_bInternal)
  77. fflush(file);
  78. }
  79. }
  80. #endif
  81. }
  82. static void __cfgui_out(LPCTSTR str)
  83. {
  84. #ifdef __CFGUI_TRACE__TO_FILE
  85. assert(file != NULL);
  86. if (file != NULL)
  87. _ftprintf(file, str);
  88. #endif
  89. #ifdef __CFGUI_TRACE__TO_DEBUG_OUT
  90. OutputDebugString(str);
  91. #endif
  92. }
  93. __cfgui_tracescope::__cfgui_tracescope(LPCTSTR str)
  94. {
  95. if (str != NULL)
  96. trace(str);
  97. depth++;
  98. }
  99. __cfgui_tracescope::~__cfgui_tracescope()
  100. {
  101. depth--;
  102. }
  103. LPTSTR splitlines(LPTSTR);
  104. /*void test(LPTSTR str)
  105. {
  106. LPTSTR orig = _tcsdup(str), str2 = _tcsdup(str);
  107. static TCHAR buf[1024];
  108. int i = 1;
  109. for (LPTSTR token = splitlines(str2);
  110. token != NULL;
  111. token = splitlines(NULL), i++)
  112. {
  113. LPTSTR t = _tcsdup(token);
  114. int len = _tcslen(t);
  115. BOOL b = t[len - 1] == _T("\n")[0];
  116. if (b)
  117. t[len - 1] = _T("\0")[0];
  118. _stprintf(buf, _T("%02d: \"%s\" (%s)\n"), i, t, BOOLSTR(b));
  119. __cfgui_out(buf);
  120. free(t);
  121. }
  122. free(str2);
  123. free(orig);
  124. }
  125. */
  126. void __cfgui_trace(__cfgui_tracetype t, LPCTSTR format, ...)
  127. {
  128. __cfgui_out_filescope fs(true);
  129. int i;
  130. bool bError = t == __cfgui_tracetype_ERROR;
  131. LPCTSTR errorprefix = _T("ERROR! ");
  132. const int prefixlen = 8, depthbuflen = 1024, buflen = 4096;
  133. static TCHAR prefixbuf[prefixlen + depthbuflen + 1] = _T("cfgUI: "), buf[buflen];
  134. static LPTSTR depthbuf = prefixbuf + prefixlen;
  135. static TCHAR space = _T(" ")[0];
  136. static TCHAR zero = _T("\0")[0];
  137. static TCHAR endl = _T("\n")[0];
  138. static int last = -2;
  139. static bool bendl = true;
  140. if (last == -2)
  141. {
  142. for (i = 0; i < depthbuflen; i++)
  143. depthbuf[i] = space;
  144. depthbuf[i] = zero;
  145. last = -1;
  146. /*
  147. test(_T("aopiwfoiefef\n\nwpoeifef\naefoie\n\n\nwpoeifwef asefeiof"));
  148. test(_T("\npw\noiefpow ij e f owpiejf\n\n"));
  149. test(_T("\n\npw\noiefpo wije\n\n \n\n\nfowpie jf \n"));
  150. */ }
  151. if (last != -1)
  152. {
  153. depthbuf[last] = space;
  154. }
  155. int d = depth;
  156. if (d < mindepth)
  157. d = mindepth;
  158. last = d * 4;
  159. if (last >= depthbuflen)
  160. last = depthbuflen - 1;
  161. depthbuf[last] = zero;
  162. va_list args;
  163. va_start(args, format);
  164. #ifdef WIN95
  165. {
  166. char *psz = NULL;
  167. char szDfs[1024]={0};
  168. strcpy(szDfs,format); // make a local copy of format string
  169. while (psz = strstr(szDfs,"%p")) // find each %p
  170. *(psz+1) = 'x'; // replace each %p with %x
  171. _vstprintf(buf, szDfs, args); // use the local format string
  172. }
  173. #else
  174. {
  175. _vstprintf(buf, format, args);
  176. }
  177. #endif
  178. va_end(args);
  179. LPTSTR tempbuf = _tcsdup(buf);
  180. bool doprefix = bendl;
  181. for (LPTSTR token = splitlines(tempbuf);
  182. token != NULL;
  183. token = splitlines(NULL))
  184. {
  185. if (doprefix)
  186. __cfgui_out(depthbuf/*prefixbuf*/);
  187. if (bError && doprefix)
  188. __cfgui_out(errorprefix);
  189. __cfgui_out(token);
  190. bendl = token[_tcslen(token) - 1] == endl;
  191. doprefix = bendl;
  192. }
  193. free(tempbuf);
  194. }
  195. LPTSTR splitlines(LPTSTR split)
  196. {
  197. static LPTSTR str = NULL;
  198. static int last = 0;
  199. static TCHAR save = _T("!")[0];
  200. static TCHAR newline = _T("\n")[0], zero = _T("\0")[0];
  201. if (split != NULL)
  202. str = split;
  203. else
  204. {
  205. if (str == NULL)
  206. return NULL;
  207. str[last] = save;
  208. str += last;
  209. }
  210. if (str[0] == zero)
  211. {
  212. str = NULL;
  213. return NULL;
  214. }
  215. LPCTSTR at = str, f = _tcschr(at, newline);
  216. last = f ? f - at + 1: _tcslen(at);
  217. save = str[last];
  218. str[last] = zero;
  219. return str;
  220. }
  221. #endif