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.

242 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. dbg.c
  5. Abstract:
  6. Contains common SNMP debugging routines.
  7. SnmpSvcSetLogLevel
  8. SnmpSvcSetLogType
  9. SnmpUtilDbgPrint
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // //
  16. // Include files //
  17. // //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <snmp.h>
  20. #include <snmputil.h>
  21. #include <stdio.h>
  22. #include <time.h>
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // //
  25. // Private Definitions //
  26. // //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. #define MAX_LOG_ENTRY_LEN 512
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // //
  31. // Global Variables //
  32. // //
  33. ///////////////////////////////////////////////////////////////////////////////
  34. INT g_nLogType = SNMP_OUTPUT_TO_DEBUGGER;
  35. INT g_nLogLevel = SNMP_LOG_SILENT;
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // //
  38. // Private Procedures //
  39. // //
  40. ///////////////////////////////////////////////////////////////////////////////
  41. VOID
  42. OutputLogEntry(
  43. LPSTR pLogEntry
  44. )
  45. /*++
  46. Routine Description:
  47. Writes log entry to log types specified.
  48. Arguments:
  49. pLogEntry - zero-terminated string containing log entry text.
  50. Return Values:
  51. None.
  52. --*/
  53. {
  54. // initialize descriptor
  55. static FILE * fd = NULL;
  56. // check if console output specified
  57. if (g_nLogType & SNMP_OUTPUT_TO_CONSOLE) {
  58. // output entry to stream
  59. fprintf(stdout, "%s", pLogEntry);
  60. // flush stream
  61. fflush(stdout);
  62. }
  63. // check if logfile output specified
  64. if (g_nLogType & SNMP_OUTPUT_TO_LOGFILE) {
  65. // validate
  66. if (fd == NULL) {
  67. // attempt to open log file
  68. fd = fopen("snmpdbg.log", "w");
  69. }
  70. // validate
  71. if (fd != NULL) {
  72. // output entry to stream
  73. fprintf(fd, "%s", pLogEntry);
  74. // flush stream
  75. fflush(fd);
  76. }
  77. }
  78. // check if debugger output specified
  79. if (g_nLogType & SNMP_OUTPUT_TO_DEBUGGER) {
  80. // output entry to debugger
  81. OutputDebugStringA(pLogEntry);
  82. }
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // //
  86. // Public Procedures //
  87. // //
  88. ///////////////////////////////////////////////////////////////////////////////
  89. VOID
  90. SNMP_FUNC_TYPE
  91. SnmpSvcSetLogLevel(
  92. INT nLogLevel
  93. )
  94. /*++
  95. Routine Description:
  96. Modifies the logging level of the SNMP process.
  97. Arguments:
  98. nLogLevel - new logging level.
  99. Return Values:
  100. None.
  101. --*/
  102. {
  103. // update log level
  104. g_nLogLevel = nLogLevel;
  105. }
  106. VOID
  107. SNMP_FUNC_TYPE
  108. SnmpSvcSetLogType(
  109. INT nLogType
  110. )
  111. /*++
  112. Routine Description:
  113. Modifies the type of log used by the SNMP process.
  114. Arguments:
  115. nLogType - type of log.
  116. Return Values:
  117. None.
  118. --*/
  119. {
  120. // update log type
  121. g_nLogType = nLogType;
  122. }
  123. VOID
  124. SNMP_FUNC_TYPE
  125. SnmpUtilDbgPrint(
  126. INT nLogLevel,
  127. LPSTR szFormat,
  128. ...
  129. )
  130. /*++
  131. Routine Description:
  132. Prints debug message to current log types.
  133. Arguments:
  134. nLogLevel - log level of message.
  135. szFormat - formatting string (see printf).
  136. Return Values:
  137. None.
  138. --*/
  139. {
  140. va_list arglist;
  141. // 640 octets should be enough to encode oid's of 128 sub-ids.
  142. // (one subid can be encoded on at most 5 octets; there can be at
  143. // 128 sub-ids per oid. MAX_LOG_ENTRY_LEN = 512
  144. char szLogEntry[4*MAX_LOG_ENTRY_LEN];
  145. // validate entry's level
  146. if (nLogLevel <= g_nLogLevel) {
  147. time_t now;
  148. // initialize variable args
  149. va_start(arglist, szFormat);
  150. time(&now);
  151. strftime(szLogEntry, MAX_LOG_ENTRY_LEN, "%H:%M:%S :", localtime(&now));
  152. // transfer variable args to buffer
  153. vsprintf(szLogEntry + strlen(szLogEntry), szFormat, arglist);
  154. // actually output entry
  155. OutputLogEntry(szLogEntry);
  156. }
  157. }