home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / winnt / pop3 / debug.c next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  6.3 KB  |  285 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples.
  4. *       Copyright (C) 1992-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12.  
  13. //+---------------------------------------------------------------------------
  14. //
  15. //  File:       debug.c
  16. //
  17. //  Contents:   Debugging support functions
  18. //
  19. //  Classes:
  20. //
  21. //  Functions:
  22. //
  23. //  Note:       This file is not compiled for retail builds
  24. //
  25. //----------------------------------------------------------------------------
  26.  
  27.  
  28. #include "pop3srvp.h"
  29. #pragma hdrstop
  30.  
  31.  
  32. #if DBG         // NOTE:  This file not compiled for retail builds
  33.  
  34. FILE *  LogFile;
  35. DWORD   PopInfoLevel = 15;
  36.  
  37.  
  38.  
  39. // Debugging support functions.
  40.  
  41.  
  42. char * DebLevel[] = {   "Pop3Srv-Error",
  43.                         "Pop3Srv-Warn",
  44.                         "Pop3Srv-Trace",
  45.                         "Pop3Srv-Trace-Protocol"
  46.                     };
  47.  
  48. typedef struct _DebugKeys {
  49.     char *  Name;
  50.     DWORD   Value;
  51. } DebugKeys, *PDebugKeys;
  52.  
  53. DebugKeys   DebugKeyNames[] = {
  54.                 {"Error",       DEB_ERROR},
  55.                 {"Warning",     DEB_WARN},
  56.                 {"Trace",       DEB_TRACE},
  57.                 {"Pool",        DEB_TRACE_PROT},
  58.                 };
  59.  
  60. #define NUM_DEBUG_KEYS  sizeof(DebugKeyNames) / sizeof(DebugKeys)
  61. #define NUM_BREAK_KEYS  sizeof(BreakKeyNames) / sizeof(DebugKeys)
  62.  
  63. //+---------------------------------------------------------------------------
  64. //
  65. //  Function:   LogEvent
  66. //
  67. //  Synopsis:   Logs an event to the console and, optionally, a file.
  68. //
  69. //  Effects:
  70. //
  71. //  Arguments:  [Mask]   --
  72. //              [Format] --
  73. //              [Format] --
  74. //
  75. //  Requires:
  76. //
  77. //  Returns:
  78. //
  79. //  Signals:
  80. //
  81. //  Modifies:
  82. //
  83. //  Algorithm:
  84. //
  85. //  History:    4-29-93   RichardW   Created
  86. //
  87. //  Notes:
  88. //
  89. //----------------------------------------------------------------------------
  90.  
  91. void
  92. LogEvent(   long            Mask,
  93.             const char *    Format,
  94.             ...)
  95. {
  96.     va_list ArgList;
  97.     int     Level = 0;
  98.     int     PrefixSize = 0;
  99.     char    szOutString[256];
  100.     long    OriWinlogonlMask = Mask;
  101.  
  102.  
  103.     if (Mask & PopInfoLevel)
  104.     {
  105.         while (!(Mask & 1))
  106.         {
  107.             Level++;
  108.             Mask >>= 1;
  109.         }
  110.         if (Level >= (sizeof(DebLevel) / sizeof(char *)) )
  111.         {
  112.             Level = (sizeof(DebLevel) / sizeof(char *)) - 1;
  113.         }
  114.  
  115.  
  116.         //
  117.         // Make the prefix first:  "Process.Thread> Pop3Svr-XXX"
  118.         //
  119.  
  120.         PrefixSize = sprintf(szOutString, "%d.%d> %s: ",
  121.                 GetCurrentProcessId(), GetCurrentThreadId(), DebLevel[Level]);
  122.  
  123.  
  124.         va_start(ArgList, Format);
  125.  
  126.         if (_vsnprintf(&szOutString[PrefixSize], sizeof(szOutString) - PrefixSize,
  127.                             Format, ArgList) < 0)
  128.         {
  129.             //
  130.             // Less than zero indicates that the string could not be
  131.             // fitted into the buffer.  Output a special message indicating
  132.             // that:
  133.             //
  134.  
  135.             OutputDebugStringA("Pop3Svr!LogEvent:  Could not pack string into 256 bytes\n");
  136.  
  137.         }
  138.         else
  139.         {
  140.             OutputDebugStringA(szOutString);
  141.         }
  142.  
  143.  
  144.         if (LogFile)
  145.         {
  146.             SYSTEMTIME  stTime;
  147.  
  148.             GetLocalTime(&stTime);
  149.             fprintf(LogFile, "%02d:%02d:%02d.%03d: %s\n",
  150.                     stTime.wHour, stTime.wMinute, stTime.wSecond,
  151.                     stTime.wMilliseconds, szOutString);
  152.  
  153.             fflush(LogFile);
  154.         }
  155.  
  156.     }
  157.  
  158. }
  159.  
  160. void
  161. DebugOpenLogFile(LPSTR   pszLogFile)
  162. {
  163.     LogFile = fopen(pszLogFile, "a");
  164.     if (!LogFile)
  165.     {
  166.         OutputDebugStringA("Pop3Svr: Could not open logfile for append");
  167.         OutputDebugStringA(pszLogFile);
  168.     }
  169.     DebugLog((DEB_TRACE, "Log file '%s' begins\n", pszLogFile));
  170. }
  171.  
  172.  
  173. DWORD
  174. GetDebugKeyValue(
  175.     PDebugKeys      KeyTable,
  176.     int             cKeys,
  177.     LPSTR           pszKey)
  178. {
  179.     int     i;
  180.  
  181.     for (i = 0; i < cKeys ; i++ )
  182.     {
  183.         if (_stricmp(KeyTable[i].Name, pszKey) == 0)
  184.         {
  185.             return(KeyTable[i].Value);
  186.         }
  187.     }
  188.     return(0);
  189. }
  190.  
  191. //+---------------------------------------------------------------------------
  192. //
  193. //  Function:   LoadDebugParameters
  194. //
  195. //  Synopsis:   Loads debug parameters from win.ini
  196. //
  197. //  Effects:
  198. //
  199. //  Arguments:  (none)
  200. //
  201. //  Requires:
  202. //
  203. //  Returns:
  204. //
  205. //  Signals:
  206. //
  207. //  Modifies:
  208. //
  209. //  Algorithm:
  210. //
  211. //  History:    4-29-93   RichardW   Created
  212. //
  213. //  Notes:
  214. //
  215. //----------------------------------------------------------------------------
  216.  
  217.  
  218. void
  219. LoadDebugParameters(char * szSection)
  220. {
  221.     char    szVal[128];
  222.     char *  pszDebug;
  223.     int     cbVal;
  224.  
  225.     cbVal = GetProfileStringA(szSection, "DebugFlags", "Error,Warning", szVal, sizeof(szVal));
  226.  
  227.     pszDebug = strtok(szVal, ", \t");
  228.     while (pszDebug)
  229.     {
  230.         PopInfoLevel |= GetDebugKeyValue(DebugKeyNames, NUM_DEBUG_KEYS, pszDebug);
  231.         pszDebug = strtok(NULL, ", \t");
  232.     }
  233.  
  234.     cbVal = GetProfileStringA(szSection, "LogFile", "", szVal, sizeof(szVal));
  235.     if (cbVal)
  236.     {
  237.         DebugOpenLogFile(szVal);
  238.     }
  239.  
  240. }
  241.  
  242. //+---------------------------------------------------------------------------
  243. //
  244. //  Function:   InitDebugSupport
  245. //
  246. //  Synopsis:   Initializes debugging support for the Winlogon
  247. //
  248. //  Effects:
  249. //
  250. //  Arguments:  (none)
  251. //
  252. //  Requires:
  253. //
  254. //  Returns:
  255. //
  256. //  Signals:
  257. //
  258. //  Modifies:
  259. //
  260. //  Algorithm:
  261. //
  262. //  History:    4-29-93   RichardW   Created
  263. //
  264. //  Notes:
  265. //
  266. //----------------------------------------------------------------------------
  267.  
  268.  
  269. void
  270. InitDebugSupport(void)
  271. {
  272.     LoadDebugParameters("Pop");
  273.     LoadDebugParameters("Winlogon");
  274.  
  275. }
  276.  
  277.  
  278.  
  279. #else // DBG
  280.  
  281. #pragma warning(disable:4206)   // Disable the empty transation unit
  282.                                 // warning/error
  283.  
  284. #endif  // NOTE:  This file not compiled for retail builds
  285.