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 / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  3.8 KB  |  151 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. //  File:       events.c
  15. //
  16. //  Contents:
  17. //
  18. //  Classes:
  19. //
  20. //  Functions:
  21. //
  22. //----------------------------------------------------------------------------
  23.  
  24. #include "pop3srvp.h"
  25. #pragma hdrstop
  26.  
  27. HANDLE  hEventLog = INVALID_HANDLE_VALUE;
  28. DWORD   LoggingLevel = 1;
  29. WCHAR   EventSourceName[] = TEXT("Pop3 Server");
  30.  
  31. #define MAX_EVENT_STRINGS 8
  32.  
  33. //+---------------------------------------------------------------------------
  34. //
  35. //  Function:   InitializeEvents
  36. //
  37. //  Synopsis:   Connects to event log service
  38. //
  39. //  Arguments:  (none)
  40. //
  41. //  History:    1-03-95   RichardW   Created
  42. //
  43. //  Notes:
  44. //
  45. //----------------------------------------------------------------------------
  46. BOOL
  47. InitializeEvents(void)
  48. {
  49.     hEventLog = RegisterEventSource(NULL, EventSourceName);
  50.     if (hEventLog)
  51.     {
  52.         return(TRUE);
  53.     }
  54.     DebugLog((DEB_ERROR, "Could not open event log, error %d\n", GetLastError()));
  55.     return(FALSE);
  56. }
  57.  
  58.  
  59. //+---------------------------------------------------------------------------
  60. //
  61. //  Function:   ReportServiceEvent
  62. //
  63. //  Synopsis:   Reports an event to the event log
  64. //
  65. //  Arguments:  [EventType]       -- EventType (ERROR, WARNING, etc.)
  66. //              [EventId]         -- Event ID
  67. //              [SizeOfRawData]   -- Size of raw data
  68. //              [RawData]         -- Raw data
  69. //              [NumberOfStrings] -- number of strings
  70. //              ...               -- PWSTRs to string data
  71. //
  72. //  History:    1-03-95   RichardW   Created
  73. //
  74. //  Notes:
  75. //
  76. //----------------------------------------------------------------------------
  77. DWORD
  78. ReportServiceEvent(
  79.     IN WORD EventType,
  80.     IN DWORD EventId,
  81.     IN DWORD SizeOfRawData,
  82.     IN PVOID RawData,
  83.     IN DWORD NumberOfStrings,
  84.     ...
  85.     )
  86. {
  87.     va_list arglist;
  88.     ULONG i;
  89.     PWSTR Strings[ MAX_EVENT_STRINGS ];
  90.     DWORD rv;
  91.  
  92.     if (!hEventLog)
  93.     {
  94.         DebugLog((DEB_ERROR, "Cannot log event, no handle!\n"));
  95.         return((DWORD)-1);
  96.     }
  97.  
  98.     //
  99.     // We're not supposed to be logging this, so nuke it
  100.     //
  101.     if ((LoggingLevel & (1 << EventType)) == 0)
  102.     {
  103.         return(0);
  104.     }
  105.  
  106.     //
  107.     // Look at the strings, if they were provided
  108.     //
  109.     va_start( arglist, NumberOfStrings );
  110.  
  111.     if (NumberOfStrings > MAX_EVENT_STRINGS) {
  112.         NumberOfStrings = MAX_EVENT_STRINGS;
  113.     }
  114.  
  115.     for (i=0; i<NumberOfStrings; i++) {
  116.         Strings[ i ] = va_arg( arglist, PWSTR );
  117.     }
  118.  
  119.  
  120.     //
  121.     // Report the event to the eventlog service
  122.     //
  123.  
  124.     if (!ReportEvent(   hEventLog,
  125.                         EventType,
  126.                         0,            // event category
  127.                         EventId,
  128.                         NULL,
  129.                         (WORD)NumberOfStrings,
  130.                         SizeOfRawData,
  131.                         Strings,
  132.                         RawData) )
  133.     {
  134.         rv = GetLastError();
  135.         DebugLog((DEB_ERROR,  "ReportEvent( %u ) failed - %u\n", EventId, GetLastError() ));
  136.  
  137.     }
  138.     else
  139.     {
  140.         rv = ERROR_SUCCESS;
  141.     }
  142.  
  143.     return rv;
  144. }
  145.  
  146. BOOL
  147. ShutdownEvents(void)
  148. {
  149.     return(DeregisterEventSource(hEventLog));
  150. }
  151.