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.

695 lines
17 KiB

  1. <% '==================================================
  2. ' Module: inc_debug.asp
  3. '
  4. ' Synopsis: Server Appliance Web Framework Error Handling
  5. '
  6. ' Copyright (c) Microsoft Corporation. All rights reserved.
  7. '==================================================
  8. %>
  9. <%
  10. 'Server.ScriptTimeout = 300
  11. '
  12. ' Global variables for Error handling support
  13. '
  14. Const MAX_TRACEFILE_SIZE = 500000
  15. Const MINIMUM_SCRIPT_TIMEOUT = 300
  16. '
  17. ' ReEntrancy check variable
  18. DIM SA_INSIDE_DEBUG
  19. SA_INSIDE_DEBUG = 0
  20. ' Last error code.
  21. ' DO NOT access this variable directly, use SA_GetLastError
  22. DIM SA_LASTERROR
  23. ' Function executing during last error
  24. ' DO NOT access this variable, it's for internal use only
  25. DIM SA_LASTERROR_FUNCTION
  26. ' Tracing options
  27. Const SA_TRACE_OUTPUT_NONE = 0
  28. Const SA_TRACE_OUTPUT_HTML = 1
  29. Const SA_TRACE_OUTPUT_FILE = 2
  30. '
  31. ' Debugging enabled, default is enabled.
  32. ' DO NOT access this variable, it's for internal use only.
  33. '
  34. ' This variable is set in LoadRuntimeOptions() which
  35. ' is called below. We default to enabled just in case
  36. ' LoadRuntimeOptions failes to initialize.
  37. DIM SA_DEBUG_ENABLED
  38. SA_DEBUG_ENABLED = 0
  39. ' Tracing option, default is HTML
  40. ' DO NOT access this variable, it's for internal use only.
  41. '
  42. ' This variable is set in LoadRuntimeOptions() which
  43. ' is called below. We default to enabled just in case
  44. ' LoadRuntimeOptions failes to initialize.
  45. DIM SA_TRACE_OPTION
  46. SA_TRACE_OPTION = SA_TRACE_OUTPUT_NONE
  47. ' Current Tracing output file
  48. ' DO NOT access this variable, it's for internal use only
  49. DIM SA_TRACE_FILE
  50. '
  51. ' Global constant error codes
  52. Const gc_ERR_SUCCESS = 0
  53. SA_LASTERROR = gc_ERR_SUCCESS
  54. '
  55. ' Set the Runtime options
  56. '
  57. LoadRuntimeOptions()
  58. '
  59. ' Set page level error handling
  60. '
  61. If (SA_DEBUG_ENABLED <> 0) Then
  62. '
  63. ' Debugging mode
  64. '
  65. On Error goto 0
  66. Else
  67. '
  68. ' Release mode
  69. '
  70. On Error Resume Next
  71. End If
  72. ' --------------------------------------------------------------
  73. '
  74. ' Function:
  75. '
  76. ' Synopsis:
  77. '
  78. ' Arguments:
  79. '
  80. ' --------------------------------------------------------------
  81. Private Function LoadRuntimeOptions()
  82. ON ERROR RESUME NEXT
  83. Dim objRegistry
  84. Dim dwDebugOption
  85. '
  86. ' Prevent recursion into this module. Specifically, SA_TraceOut
  87. SA_EnterDebugModule()
  88. '
  89. ' Disable for now
  90. '
  91. Set objRegistry = RegConnection()
  92. If (NOT IsObject(objRegistry)) Then
  93. SA_TraceOut "LoadRuntimeOptions", "RegConnection() failed " + "(" + Hex(Err.Number) + ")"
  94. Exit Function
  95. End If
  96. '
  97. ' Fetch debugging flag
  98. '
  99. SA_DEBUG_ENABLED= GetRegkeyValue( objRegistry, _
  100. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  101. "Debug", CONST_DWORD)
  102. If (SA_DEBUG_ENABLED <> 0) Then
  103. '
  104. ' Debugging mode
  105. '
  106. On Error goto 0
  107. Else
  108. '
  109. ' Release mode
  110. '
  111. On Error Resume Next
  112. End If
  113. SA_TRACE_OPTION = GetRegkeyValue( objRegistry, _
  114. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  115. "TraceOption", CONST_DWORD)
  116. SA_TRACE_FILE = GetRegkeyValue( objRegistry, _
  117. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  118. "TraceFile", CONST_STRING)
  119. Dim iScriptTimeOut
  120. iScriptTimeOut = GetRegkeyValue( objRegistry, _
  121. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  122. "ScriptTimeOut", CONST_DWORD)
  123. If ( NOT IsNumeric(iScriptTimeOut) ) Then
  124. iScriptTimeOut = MINIMUM_SCRIPT_TIMEOUT
  125. ElseIf ( iScriptTimeOut < MINIMUM_SCRIPT_TIMEOUT ) Then
  126. iScriptTimeOut = MINIMUM_SCRIPT_TIMEOUT
  127. End If
  128. Server.ScriptTimeout = CInt(iScriptTimeOut)
  129. SA_ExitDebugModule()
  130. Set objRegistry = nothing
  131. End Function
  132. ' --------------------------------------------------------------
  133. '
  134. ' Function: SA_EnterDebugModule
  135. '
  136. ' Synopsis: Mark us as inside the debug module. This is used for
  137. ' reentrancy checks. We don't want to get caught in
  138. ' a recussion loop if we have an error inside this module.
  139. '
  140. ' Arguments:
  141. '
  142. ' --------------------------------------------------------------
  143. Private Function SA_EnterDebugModule()
  144. SA_INSIDE_DEBUG = 1
  145. End Function
  146. ' --------------------------------------------------------------
  147. '
  148. ' Function: SA_ExitDebugModule
  149. '
  150. ' Synopsis: Mark us as exiting the debug module. This is used for
  151. ' reentrancy checks. We don't want to get caught in
  152. ' a recussion loop if we have an error inside this module.
  153. '
  154. ' Arguments:
  155. '
  156. ' --------------------------------------------------------------
  157. Private Function SA_ExitDebugModule()
  158. SA_INSIDE_DEBUG = 0
  159. End Function
  160. ' --------------------------------------------------------------
  161. '
  162. ' Function: SA_IsExecutingDebugModule
  163. '
  164. ' Synopsis: Check to see if we are reentering this module
  165. '
  166. ' --------------------------------------------------------------
  167. Private Function SA_IsExecutingDebugModule()
  168. SA_IsExecutingDebugModule = SA_INSIDE_DEBUG
  169. End Function
  170. ' --------------------------------------------------------------
  171. '
  172. ' Function:
  173. '
  174. ' Synopsis:
  175. '
  176. ' Arguments:
  177. '
  178. ' --------------------------------------------------------------
  179. Public Function SA_IsDebugEnabled()
  180. SA_IsDebugEnabled = SA_DEBUG_ENABLED
  181. End Function
  182. ' --------------------------------------------------------------
  183. '
  184. ' Function:
  185. '
  186. ' Synopsis:
  187. '
  188. ' Arguments:
  189. '
  190. ' --------------------------------------------------------------
  191. Public Function SA_EnableDebug(ByVal DebugEnabled)
  192. SA_DEBUG_ENABLED = DebugEnabled
  193. Dim objRegistry
  194. Dim rc
  195. Set objRegistry = RegConnection()
  196. If (NOT IsObject(objRegistry)) Then
  197. SA_TraceOut "SA_EnableDebug", "RegConnection() failed " + "(" + Hex(Err.Number) + ")"
  198. Exit Function
  199. End If
  200. rc = UpdateRegkeyValue( objRegistry, _
  201. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  202. "Debug", _
  203. DebugEnabled, _
  204. CONST_DWORD)
  205. Set objRegistry = nothing
  206. SA_EnableDebug = gc_ERR_SUCCESS
  207. End Function
  208. ' --------------------------------------------------------------
  209. '
  210. ' Function:
  211. '
  212. ' Synopsis:
  213. '
  214. ' Arguments:
  215. '
  216. ' --------------------------------------------------------------
  217. Public Function SA_GetTraceOption()
  218. SA_GetTraceOption = SA_TRACE_OPTION
  219. End Function
  220. ' --------------------------------------------------------------
  221. '
  222. ' Function: SA_SetDebugOption
  223. '
  224. ' Synopsis: Set the debugging option.
  225. '
  226. ' Arguments: [in] Option - Debugging option to use which can be a combination of
  227. ' SA_TRACE_OUTPUT_HTML Debugging errors emitted with HTML response
  228. ' SA_TRACE_OUTPUT_FILE Debugging errors emitted to output file
  229. '
  230. ' --------------------------------------------------------------
  231. Public Function SA_SetDebugOption(ByVal DebugOption)
  232. SA_TRACE_OPTION = DebugOption
  233. Dim objRegistry
  234. Dim rc
  235. Set objRegistry = RegConnection()
  236. If (NOT IsObject(objRegistry)) Then
  237. SA_TraceOut "SA_SetDebugOption", "RegConnection() failed " + "(" + Hex(Err.Number) + ")"
  238. Exit Function
  239. End If
  240. rc = UpdateRegkeyValue( objRegistry, _
  241. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  242. "TraceOption", _
  243. DebugOption, _
  244. CONST_DWORD)
  245. Set objRegistry = nothing
  246. SA_SetDebugOption = gc_ERR_SUCCESS
  247. End Function
  248. ' --------------------------------------------------------------
  249. '
  250. ' Function:
  251. '
  252. ' Synopsis:
  253. '
  254. ' Arguments:
  255. '
  256. ' --------------------------------------------------------------
  257. Public Function SA_GetTraceOutputFile()
  258. SA_GetTraceOutputFile = SA_TRACE_FILE
  259. End Function
  260. ' --------------------------------------------------------------
  261. '
  262. ' Function: SA_SetDebugOutputFile
  263. '
  264. ' Synopsis: Set the debugging output file
  265. '
  266. ' Arguments: [in] File - Filename to receive debugging output
  267. '
  268. ' --------------------------------------------------------------
  269. Public Function SA_SetDebugOutputFile(ByVal File)
  270. SA_TRACE_FILE = File
  271. Dim objRegistry
  272. Dim rc
  273. Set objRegistry = RegConnection()
  274. If (NOT IsObject(objRegistry)) Then
  275. SA_TraceOut "SA_SetDebugOutputFile", "RegConnection() failed " + "(" + Hex(Err.Number) + ")"
  276. Exit Function
  277. End If
  278. rc = UpdateRegkeyValue( objRegistry, _
  279. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  280. "TraceFile", _
  281. File, _
  282. CONST_STRING)
  283. Set objRegistry = nothing
  284. SA_SetDebugOutputFile = gc_ERR_SUCCESS
  285. End Function
  286. ' --------------------------------------------------------------
  287. '
  288. ' Function:
  289. '
  290. ' Synopsis:
  291. '
  292. ' Arguments:
  293. '
  294. ' --------------------------------------------------------------
  295. Function SA_ClearTraceLog()
  296. #ifdef DBG
  297. Dim fso
  298. Dim traceFile
  299. Const ForReading = 1, ForWriting = 2, ForAppending = 8
  300. Set fso = CreateObject("Scripting.FileSystemObject")
  301. If ( NOT IsObject(fso)) Then
  302. SA_TraceOut "SA_ClearTraceLog", "CreateObject(Scripting.FileSystemObject) failed " + "(" + Hex(Err.Number) + ")"
  303. Exit Function
  304. End If
  305. on error resume next
  306. Set traceFile = fso.OpenTextFile(SA_TRACE_FILE, ForWriting, True)
  307. If (NOT IsObject(traceFile)) Then
  308. SA_TraceOut "LoadRuntimeOptions", _
  309. "fso.OpenTextFile(SA_TRACE_FILE, ForWriting, True) failed " + "(" + Hex(Err.Number) + ")"
  310. If SA_IsDebugEnabled() Then
  311. on error goto 0
  312. End If
  313. Exit Function
  314. End If
  315. traceFile.Close
  316. If SA_IsDebugEnabled() Then
  317. on error goto 0
  318. End If
  319. Set traceFile = nothing
  320. Set fso = nothing
  321. #endif
  322. End Function
  323. ' --------------------------------------------------------------
  324. '
  325. ' Function:
  326. '
  327. ' Synopsis:
  328. '
  329. ' Arguments:
  330. '
  331. ' --------------------------------------------------------------
  332. Function SA_ShowTraceLog()
  333. #ifdef DBG
  334. Const ForReading = 1, ForWriting = 2, ForAppending = 8
  335. Dim fso
  336. Dim traceFile
  337. Dim str
  338. on error resume next
  339. Set fso = CreateObject("Scripting.FileSystemObject")
  340. If ( NOT IsObject(fso)) Then
  341. SA_TraceOut "SA_ShowTraceLog", "CreateObject(Scripting.FileSystemObject) failed " + "(" + Hex(Err.Number) + ")"
  342. If SA_IsDebugEnabled() Then
  343. on error goto 0
  344. End If
  345. Exit Function
  346. End If
  347. Set traceFile = fso.OpenTextFile(SA_TRACE_FILE, ForReading, True)
  348. If (NOT IsObject(traceFile)) Then
  349. SA_TraceOut "SA_ShowTraceLog", _
  350. "fso.OpenTextFile(SA_TRACE_FILE, ForWriting, True) failed " + "(" + Hex(Err.Number) + ")"
  351. If SA_IsDebugEnabled() Then
  352. on error goto 0
  353. End If
  354. fso = Nothing
  355. Exit Function
  356. End If
  357. Response.Write("<H2>Trace Log</H2>")
  358. Response.Write("<table>")
  359. While NOT traceFile.AtEndOfStream
  360. str = traceFile.ReadLine()
  361. Response.Write("<tr><td>"+str+"</td></tr>")
  362. Response.Flush
  363. WEnd
  364. Response.Write("</table>")
  365. traceFile.Close
  366. If SA_IsDebugEnabled() Then
  367. on error goto 0
  368. End If
  369. Set traceFile = nothing
  370. Set fso = nothing
  371. #endif
  372. End Function
  373. ' --------------------------------------------------------------
  374. '
  375. ' Function:
  376. '
  377. ' Synopsis:
  378. '
  379. ' Arguments:
  380. '
  381. ' --------------------------------------------------------------
  382. Function SA_ShowRuntimeOptions()
  383. #ifdef DBG
  384. SA_TraceOut "CheckRuntimeOptions", "Begin"
  385. Response.Write("<H2>Runtime Options</H2>")
  386. Response.Write("<table title='SA Web Framework Options' cols=3 border=2 cellspace=2>"+vbCrLf)
  387. Response.Write("<tr>")
  388. Response.Write("<th align='left'>Setting</th>")
  389. Response.Write("<th align='center'>Value</th>")
  390. Response.Write("</tr>"+vbCrLf)
  391. Response.Write("<tr>")
  392. Response.Write("<td>Debug enabled</td>")
  393. Response.Write("<td>" + CStr(SA_IsDebugEnabled()) + "</td>")
  394. Response.Write("</tr>"+vbCrLf)
  395. Response.Write("<tr>")
  396. Response.Write("<td>Trace option</td>")
  397. Response.Write("<td>" + CStr(SA_GetTraceOption()) + "</td>")
  398. Response.Write("</tr>"+vbCrLf)
  399. Response.Write("<tr>")
  400. Response.Write("<td>Trace output</td>")
  401. Response.Write("<td>" + Trim(SA_GetTraceOutputFile()) + "</td>")
  402. Response.Write("</tr>"+vbCrLf)
  403. Response.Write("</table>"+vbCrLf)
  404. SA_TraceOut "CheckRuntimeOptions", "End"
  405. #endif
  406. End Function
  407. ' --------------------------------------------------------------
  408. '
  409. ' Function: SA_TraceOut
  410. '
  411. ' Synopsis: Output tracing information
  412. '
  413. ' Arguments: [in] Module - Module/Function issuing the tracing message
  414. ' [in] Message - Message to be output
  415. '
  416. ' --------------------------------------------------------------
  417. Public Function SA_TraceOut(ByVal Module, ByVal Message)
  418. #ifdef DBG
  419. If (NOT SA_IsExecutingDebugModule() ) Then
  420. SA_EnterDebugModule()
  421. SA_InternalTraceOut Module, Message
  422. SA_ExitDebugModule()
  423. End If
  424. #endif
  425. SA_TraceOut = gc_ERR_SUCCESS
  426. End Function
  427. ' --------------------------------------------------------------
  428. '
  429. ' Function: SA_TraceErrorOut
  430. '
  431. ' Synopsis: Output tracing information for error conditions. Errors are
  432. ' flaged with the keyword ISSUE.
  433. '
  434. ' Arguments: [in] Module - Module/Function issuing the tracing message
  435. ' [in] Message - Message to be output
  436. '
  437. ' --------------------------------------------------------------
  438. Public Function SA_TraceErrorOut(ByVal Module, ByVal Message)
  439. Dim rc
  440. rc = SA_TraceOut("ISSUE: " + Module, Message)
  441. SA_TraceErrorOut = rc
  442. End Function
  443. ' --------------------------------------------------------------
  444. '
  445. ' Function: SA_SetLastError
  446. '
  447. ' Synopsis: Set the last error code.
  448. '
  449. ' Arguments: [in] ErrorCode - Error code
  450. ' [in] FunctionName - Name of function where error occured
  451. '
  452. ' --------------------------------------------------------------
  453. Public Function SA_GetLastError()
  454. SA_GetLastError = SA_LASTERROR
  455. End Function
  456. ' --------------------------------------------------------------
  457. '
  458. ' Function: SA_SetLastError
  459. '
  460. ' Synopsis: Set the last error code.
  461. '
  462. ' Arguments: [in] ErrorCode - Error code
  463. ' [in] FunctionName - Name of function where error occured
  464. '
  465. ' Returns: The error code specified in ErrorCode parameter
  466. '
  467. ' --------------------------------------------------------------
  468. Public Function SA_SetLastError(ByVal ErrorCode, ByVal FunctionName )
  469. SA_LASTERROR = ErrorCode
  470. SA_LASTERROR_FUNCTION = FunctionName
  471. Err.Number = ErrorCode
  472. SA_SetLastError = ErrorCode
  473. '
  474. ' If we had an error then emit trace output. An error is
  475. ' any error code other than gc_ERR_SUCCESS.
  476. '
  477. If ( ErrorCode <> gc_ERR_SUCCESS ) Then
  478. SA_InternalTraceOut "ISSUE: "+FunctionName, CStr(ErrorCode)
  479. End If
  480. End Function
  481. ' --------------------------------------------------------------
  482. '
  483. ' Function: SA_SetLastError
  484. '
  485. ' Synopsis: Set the last error code.
  486. '
  487. ' Arguments: [in] ErrorCode - Error code
  488. ' [in] FunctionName - Name of function where error occured
  489. '
  490. ' --------------------------------------------------------------
  491. Public Function SA_ClearError()
  492. SA_LASTERROR = gc_ERR_SUCCESS
  493. Err.Number = 0
  494. SA_ClearError = gc_ERR_SUCCESS
  495. End Function
  496. ' --------------------------------------------------------------
  497. '
  498. ' Function: _SA_InternalTraceOut
  499. '
  500. ' Synopsis: Internal function to handle output tracing.
  501. '
  502. ' Arguments: [in] Module - Module/Function issuing the tracing message
  503. ' [in] Message - Message to be output
  504. '
  505. ' --------------------------------------------------------------
  506. Private Function SA_InternalTraceOut(ByVal Module, ByVal Message)
  507. #ifdef DBG
  508. on error resume next
  509. '
  510. ' Trace errors to HTML response buffer
  511. '
  512. If (SA_TRACE_OPTION AND SA_TRACE_OUTPUT_HTML) Then
  513. Response.Write("<SPAN style='color:white; background:red;'>")
  514. Response.Write("<BR>")
  515. Response.Write("<p>" + Module + " : " + Message)
  516. Response.Write("<BR>")
  517. Response.Write("</SPAN>")
  518. End If
  519. '
  520. ' Trace to file
  521. '
  522. If (SA_TRACE_OPTION AND SA_TRACE_OUTPUT_FILE) Then
  523. Const ForReading = 1, ForWriting = 2, ForAppending = 8
  524. Dim fso
  525. Dim traceFile
  526. Set fso = CreateObject("Scripting.FileSystemObject")
  527. If ( NOT IsObject(fso)) Then
  528. '
  529. ' Can not call SA_TraceOut here since we are in it.
  530. '
  531. If SA_IsDebugEnabled() Then
  532. on error goto 0
  533. Else
  534. on error resume next
  535. End If
  536. Exit Function
  537. End If
  538. Dim enOpenOption
  539. enOpenOption = ForAppending
  540. Err.Clear
  541. Set traceFile = fso.GetFile(SA_TRACE_FILE)
  542. If ( Err.Number = 0 ) Then
  543. If ( traceFile.size > MAX_TRACEFILE_SIZE ) Then
  544. enOpenOption = ForWriting
  545. End If
  546. End If
  547. Set traceFile = nothing
  548. Set traceFile = fso.OpenTextFile(SA_TRACE_FILE, enOpenOption, True)
  549. If (NOT IsObject(traceFile)) Then
  550. '
  551. ' Can not call SA_TraceOut here since we are in it.
  552. '
  553. Set fso = nothing
  554. If SA_IsDebugEnabled() Then
  555. on error goto 0
  556. Else
  557. on error resume next
  558. End If
  559. Exit Function
  560. End If
  561. traceFile.WriteLine CStr(Now())+": " + Module + " : " + Message
  562. traceFile.Close
  563. Set traceFile = Nothing
  564. Set fso = Nothing
  565. End If
  566. If SA_IsDebugEnabled() Then
  567. on error goto 0
  568. Else
  569. on error resume next
  570. End If
  571. #endif
  572. SA_InternalTraceOut = gc_ERR_SUCCESS
  573. End Function
  574. %>