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.

328 lines
8.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: outfuncs.cxx
  7. //
  8. // Contents: functions for log/trace output
  9. //
  10. // Functions: AddOutputFunction
  11. // DelOutputFunction
  12. // CallOutputFunctions
  13. //
  14. // History: 09-Jan-96 murthys Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windows.h>
  21. #include <stdarg.h>
  22. #include <tchar.h>
  23. #if DBG==1
  24. #include "outfuncs.h"
  25. // *** Global Data ***
  26. static StringOutFunc debugscrfn = (StringOutFunc)OutputDebugStringA;
  27. StringOutFunc gpfunc[BUFFER_MAX_FUNCTIONS] = {
  28. (StringOutFunc)OutputDebugStringA,
  29. NULL
  30. };
  31. HANDLE ghLogFile = INVALID_HANDLE_VALUE;
  32. CRITICAL_SECTION g_LogFileCS;
  33. BOOL g_LogFileLockValid = FALSE;
  34. //+---------------------------------------------------------------------------
  35. //
  36. // Function: AddOutputFunction
  37. //
  38. // Synopsis:
  39. //
  40. // Arguments: [pfunc] --
  41. //
  42. // Returns:
  43. //
  44. // History: 09-Jan-96 murthys Created
  45. //
  46. // Notes:
  47. //
  48. //----------------------------------------------------------------------------
  49. void AddOutputFunction(StringOutFunc pfunc)
  50. {
  51. int i, at = -1;
  52. for (i = 0; i < BUFFER_MAX_FUNCTIONS; i++)
  53. {
  54. if ((at == -1) && (gpfunc[i] == NULL))
  55. {
  56. at = i; // Insert it here
  57. }
  58. else
  59. {
  60. if (gpfunc[i] == pfunc) // check for dups
  61. {
  62. return;
  63. }
  64. }
  65. }
  66. if (at != -1)
  67. {
  68. gpfunc[at] = pfunc;
  69. }
  70. }
  71. //+---------------------------------------------------------------------------
  72. //
  73. // Function: DelOutputFunction
  74. //
  75. // Synopsis:
  76. //
  77. // Arguments: [pfunc]
  78. //
  79. // Returns:
  80. //
  81. // History: 09-Jan-96 murthys Created
  82. //
  83. // Notes:
  84. //
  85. //----------------------------------------------------------------------------
  86. void DelOutputFunction(StringOutFunc pfunc)
  87. {
  88. int i;
  89. for (i = 0; i < BUFFER_MAX_FUNCTIONS; i++)
  90. {
  91. if (gpfunc[i] == pfunc)
  92. {
  93. gpfunc[i] = NULL;
  94. }
  95. }
  96. }
  97. //+---------------------------------------------------------------------------
  98. //
  99. // Function: CallOutputFunctions
  100. //
  101. // Synopsis:
  102. //
  103. // Arguments: (none)
  104. //
  105. // Returns:
  106. //
  107. // History: 09-Jan-96 murthys Created
  108. //
  109. // Notes:
  110. //
  111. //----------------------------------------------------------------------------
  112. void CallOutputFunctions(const char *buffer)
  113. {
  114. int i;
  115. for (i = 0; i < BUFFER_MAX_FUNCTIONS; i++)
  116. {
  117. if (gpfunc[i] != NULL)
  118. {
  119. gpfunc[i](buffer);
  120. }
  121. }
  122. }
  123. //+---------------------------------------------------------------------------
  124. //
  125. // Function: WriteToDebugScreen
  126. //
  127. // Synopsis:
  128. //
  129. // Arguments: [flag] - TRUE/FALSE to turn ON/OFF
  130. //
  131. // Returns:
  132. //
  133. // History: 09-Jan-96 murthys Created
  134. //
  135. // Notes:
  136. //
  137. //----------------------------------------------------------------------------
  138. void WriteToDebugScreen(BOOL flag)
  139. {
  140. if (flag)
  141. {
  142. AddOutputFunction(debugscrfn);
  143. }
  144. else
  145. {
  146. DelOutputFunction(debugscrfn);
  147. }
  148. }
  149. //+---------------------------------------------------------------------------
  150. //
  151. // Function: WriteToLogFile
  152. //
  153. // Synopsis:
  154. //
  155. // Arguments: [logfile] - path of file to write to
  156. //
  157. // Returns:
  158. //
  159. // History: 09-Jan-96 murthys Created
  160. //
  161. // Notes:
  162. //
  163. //----------------------------------------------------------------------------
  164. void WriteToLogFile(LPCTSTR lpfn)
  165. {
  166. if (!g_LogFileLockValid)
  167. return;
  168. EnterCriticalSection(&g_LogFileCS);
  169. if (ghLogFile != INVALID_HANDLE_VALUE)
  170. {
  171. CloseHandle(ghLogFile);
  172. DelOutputFunction(OutputLogFileA);
  173. ghLogFile = INVALID_HANDLE_VALUE;
  174. }
  175. if ((lpfn) && (lpfn[0] != _TEXT('\0')))
  176. {
  177. SECURITY_ATTRIBUTES sattr;
  178. sattr.nLength = sizeof(sattr);
  179. sattr.lpSecurityDescriptor = NULL;
  180. sattr.bInheritHandle = FALSE;
  181. ghLogFile = CreateFile(lpfn, GENERIC_READ|GENERIC_WRITE,
  182. FILE_SHARE_READ|FILE_SHARE_WRITE,
  183. &sattr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  184. if (ghLogFile == INVALID_HANDLE_VALUE)
  185. {
  186. OutputDebugStringA("OLE (WriteToLogFile):Unable to open log file!\n");
  187. }
  188. else
  189. {
  190. AddOutputFunction(OutputLogFileA);
  191. }
  192. }
  193. LeaveCriticalSection(&g_LogFileCS);
  194. }
  195. //+---------------------------------------------------------------------------
  196. //
  197. // Function: OutputLogFileA
  198. //
  199. // Synopsis:
  200. //
  201. // Arguments: [buf] - NULL terminated ANSI string to write
  202. //
  203. // Returns:
  204. //
  205. // History: 09-Jan-96 murthys Created
  206. //
  207. // Notes:
  208. //
  209. //----------------------------------------------------------------------------
  210. void OutputLogFileA(const char *buf)
  211. {
  212. DWORD dwtowrite = (DWORD) strlen(buf);
  213. DWORD dwwritten;
  214. LONG loffhigh = 0, lofflow;
  215. if (!g_LogFileLockValid)
  216. return;
  217. EnterCriticalSection(&g_LogFileCS);
  218. // Goto EOF, Lock, Write and Unlock
  219. lofflow = (LONG) SetFilePointer(ghLogFile, 0, &loffhigh, FILE_END);
  220. LockFile(ghLogFile, lofflow, loffhigh, dwtowrite, 0);
  221. WriteFile(ghLogFile, buf, dwtowrite, &dwwritten, NULL);
  222. UnlockFile(ghLogFile, lofflow, loffhigh, dwtowrite, 0);
  223. LeaveCriticalSection(&g_LogFileCS);
  224. }
  225. //+---------------------------------------------------------------------------
  226. //
  227. // Function: OpenDebugSinks()
  228. //
  229. // Synopsis:
  230. //
  231. // Arguments:
  232. //
  233. // Returns:
  234. //
  235. // History: 26-Jan-96 murthys Created
  236. //
  237. // Notes:
  238. //
  239. //----------------------------------------------------------------------------
  240. void OpenDebugSinks()
  241. {
  242. // Get LogFile name
  243. char tmpstr[MAX_PATH];
  244. DWORD cbtmpstr = sizeof(tmpstr);
  245. LPTSTR lptstr;
  246. NTSTATUS status = RtlInitializeCriticalSection(&g_LogFileCS);
  247. g_LogFileLockValid = NT_SUCCESS(status);
  248. if (!g_LogFileLockValid)
  249. return;
  250. GetProfileStringA("CairOLE InfoLevels", // section
  251. "LogFile", // key
  252. "", // default value
  253. tmpstr, // return buffer
  254. cbtmpstr);
  255. if (tmpstr[0] != '\0')
  256. {
  257. // convert ansi to unicode
  258. WCHAR wtmpstr[MAX_PATH];
  259. lptstr = wtmpstr;
  260. if (MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, tmpstr, -1, wtmpstr, MAX_PATH))
  261. {
  262. WriteToLogFile(lptstr);
  263. }
  264. else
  265. {
  266. OutputDebugStringA("OLE32: MultiByteToWideChar failed for logfile!\n");
  267. }
  268. }
  269. // See if Debug Screen should be turned off
  270. GetProfileStringA("CairOLE InfoLevels", // section
  271. "DebugScreen", // key
  272. "Yes", // default value
  273. tmpstr, // return buffer
  274. cbtmpstr);
  275. if ((tmpstr[0] == 'n') || (tmpstr[0] == 'N'))
  276. {
  277. WriteToDebugScreen(FALSE); // turn off output to debugger screen
  278. }
  279. }
  280. //+---------------------------------------------------------------------------
  281. //
  282. // Function: CloseDebugSinks()
  283. //
  284. // Synopsis:
  285. //
  286. // Arguments:
  287. //
  288. // Returns:
  289. //
  290. // History: 26-Jan-96 murthys Created
  291. //
  292. // Notes:
  293. //
  294. //----------------------------------------------------------------------------
  295. void CloseDebugSinks()
  296. {
  297. // close log file (if any)
  298. WriteToLogFile(NULL);
  299. if (g_LogFileLockValid)
  300. {
  301. DeleteCriticalSection(&g_LogFileCS);
  302. }
  303. }
  304. #endif // DBG == 1