Subversion Repositories simple_log_event

Rev

Rev 3 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 6
Line 10... Line 10...
10
type
10
type
11
  TViaThinkSoftSimpleEventLog = class(TAutoObject, IViaThinkSoftSimpleEventLog)
11
  TViaThinkSoftSimpleEventLog = class(TAutoObject, IViaThinkSoftSimpleEventLog)
12
  protected
12
  protected
13
    procedure LogEvent(const SourceName: WideString; EventType: LogEventType; const LogMsg: WideString);
13
    procedure LogEvent(const SourceName: WideString; EventType: LogEventType; const LogMsg: WideString);
14
          safecall;
14
          safecall;
-
 
15
    procedure LogSimulate(const SourceName: WideString; EventType: LogEventType; const LogMsg: WideString;
-
 
16
          out Reason: OleVariant); safecall;
15
  end;
17
  end;
16
 
18
 
17
implementation
19
implementation
18
 
20
 
19
uses ComServ, Windows;
21
uses ComServ, Windows, SysUtils;
20
 
22
 
21
const
23
const
22
  MSG_SUCCESS        = $20000000;
24
  MSG_SUCCESS        = $20000000;
23
  MSG_INFORMATIONAL  = $60000001;
25
  MSG_INFORMATIONAL  = $60000001;
24
  MSG_WARNING        = $A0000002;
26
  MSG_WARNING        = $A0000002;
25
  MSG_ERROR          = $E0000003;
27
  MSG_ERROR          = $E0000003;
26
 
28
 
27
function WriteEventLog(AProvider: string; AEventType: word; AEventId: Cardinal; AEntry: string): boolean;
29
procedure WriteEventLog(AProvider: string; AEventType: word; AEventId: Cardinal; AEntry: string);
28
var
30
var
29
  EventLog: integer;
31
  EventLog: THandle;
30
  P: Pointer;
32
  P: Pointer;
31
begin
33
begin
32
  Result := False;
-
 
33
  P := PChar(AEntry);
34
  P := PChar(AEntry);
34
  EventLog := RegisterEventSource(nil, PChar(AProvider));
35
  EventLog := RegisterEventSource(nil, PChar(AProvider));
-
 
36
  if EventLog = 0 then
-
 
37
  begin
-
 
38
    raise Exception.CreateFmt('RegisterEventSource failed with error code %d', [GetLastError]);
-
 
39
  end;
35
  if EventLog <> 0 then
40
  if EventLog <> 0 then
36
  try
41
  try
37
    ReportEvent(EventLog, // event log handle
42
    if not ReportEvent(EventLog, // event log handle
38
          AEventType,     // event type
43
          AEventType,     // event type
39
          0,              // category zero
44
          0,              // category zero
40
          AEventId,       // event identifier
45
          AEventId,       // event identifier
41
          nil,            // no user security identifier
46
          nil,            // no user security identifier
42
          1,              // one substitution string
47
          1,              // one substitution string
43
          0,              // no data
48
          0,              // no data
44
          @P,             // pointer to string array
49
          @P,             // pointer to string array
45
          nil);           // pointer to data
50
          nil) then       // pointer to data
-
 
51
    begin
-
 
52
      raise Exception.CreateFmt('ReportEvent failed with error code %d', [GetLastError]);
46
    Result := True;
53
    end;
47
  finally
54
  finally
48
    DeregisterEventSource(EventLog);
55
    DeregisterEventSource(EventLog);
49
  end;
56
  end;
50
end;
57
end;
51
 
58
 
Line 59... Line 66...
59
      WriteEventLog(SourceName, EVENTLOG_INFORMATION_TYPE, MSG_INFORMATIONAL, LogMsg);
66
      WriteEventLog(SourceName, EVENTLOG_INFORMATION_TYPE, MSG_INFORMATIONAL, LogMsg);
60
    ViaThinkSoftSimpleLogEvent_TLB.Warning:
67
    ViaThinkSoftSimpleLogEvent_TLB.Warning:
61
      WriteEventLog(SourceName, EVENTLOG_WARNING_TYPE,     MSG_WARNING,       LogMsg);
68
      WriteEventLog(SourceName, EVENTLOG_WARNING_TYPE,     MSG_WARNING,       LogMsg);
62
    ViaThinkSoftSimpleLogEvent_TLB.Error:
69
    ViaThinkSoftSimpleLogEvent_TLB.Error:
63
      WriteEventLog(SourceName, EVENTLOG_ERROR_TYPE,       MSG_ERROR,         LogMsg);
70
      WriteEventLog(SourceName, EVENTLOG_ERROR_TYPE,       MSG_ERROR,         LogMsg);
-
 
71
    else
-
 
72
    begin
-
 
73
      raise Exception.CreateFmt('ViaThinkSoftSimpleEventLog.LogEvent: Unexpected event type %d', [Ord(EventType)]);
-
 
74
    end;
-
 
75
  end;
-
 
76
end;
-
 
77
 
-
 
78
procedure TViaThinkSoftSimpleEventLog.LogSimulate(const SourceName: WideString; EventType: LogEventType;
-
 
79
          const LogMsg: WideString; out Reason: OleVariant);
-
 
80
var
-
 
81
  EventLog: THandle;
-
 
82
begin
-
 
83
  try
-
 
84
    Reason := '';
-
 
85
    if (EventType < 0) or (EventType > ViaThinkSoftSimpleLogEvent_TLB.Error) then
-
 
86
    begin
-
 
87
      Reason := Format('Unexpected event type %d', [Ord(EventType)]);
-
 
88
      Exit;
-
 
89
    end;
-
 
90
 
-
 
91
    EventLog := RegisterEventSource(nil, PChar(SourceName));
-
 
92
    if EventLog = 0 then
-
 
93
    begin
-
 
94
      Reason := Format('RegisterEventSource failed with error code %d', [GetLastError]);
-
 
95
      Exit;
-
 
96
    end
-
 
97
    else
-
 
98
    begin
-
 
99
      DeregisterEventSource(EventLog);
-
 
100
    end;
-
 
101
  except
-
 
102
    on E: Exception do
-
 
103
    begin
-
 
104
      Reason := Format('Unexpected error: %s', [e.Message]);
-
 
105
    end;
64
  end;
106
  end;
65
end;
107
end;
66
 
108
 
67
initialization
109
initialization
68
  TAutoObjectFactory.Create(ComServer, TViaThinkSoftSimpleEventLog, Class_ViaThinkSoftSimpleEventLog,
110
  TAutoObjectFactory.Create(ComServer, TViaThinkSoftSimpleEventLog, Class_ViaThinkSoftSimpleEventLog,