home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 1997 / CT_SW_97.ISO / pc / software / entwickl / win95 / pw32i306.exe / lib / Win32 / eventlog.pm < prev    next >
Text File  |  1997-01-23  |  6KB  |  276 lines

  1. package Win32::EventLog;
  2.  
  3. #EventLog.pm
  4. #Creates an object oriented interface to the Windows NT Evenlog 
  5. #Written by Jesse Dougherty
  6. #
  7.  
  8. require Exporter;
  9. require DynaLoader;
  10. use Win32;
  11.  
  12. die "The Win32::Eventlog module works only on Windows NT" if(!Win32::IsWinNT() );
  13.  
  14. @ISA= qw( Exporter DynaLoader );
  15. # Items to export into callers namespace by default. Note: do not export
  16. # names by default without a very good reason. Use EXPORT_OK instead.
  17. # Do not simply export all your public functions/methods/constants.
  18. @EXPORT = qw(
  19.     EVENTLOG_AUDIT_FAILURE
  20.     EVENTLOG_AUDIT_SUCCESS
  21.     EVENTLOG_BACKWARDS_READ
  22.     EVENTLOG_END_ALL_PAIRED_EVENTS
  23.     EVENTLOG_END_PAIRED_EVENT
  24.     EVENTLOG_ERROR_TYPE
  25.     EVENTLOG_FORWARDS_READ
  26.     EVENTLOG_INFORMATION_TYPE
  27.     EVENTLOG_PAIRED_EVENT_ACTIVE
  28.     EVENTLOG_PAIRED_EVENT_INACTIVE
  29.     EVENTLOG_SEEK_READ
  30.     EVENTLOG_SEQUENTIAL_READ
  31.     EVENTLOG_START_PAIRED_EVENT
  32.     EVENTLOG_SUCCESS
  33.     EVENTLOG_WARNING_TYPE
  34. );
  35.  
  36. sub AUTOLOAD {
  37.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  38.     # XS function.  If a constant is not found then control is passed
  39.     # to the AUTOLOAD in AutoLoader.
  40.  
  41.     my($constname);
  42.     ($constname = $AUTOLOAD) =~ s/.*:://;
  43.     #reset $! to zero to reset any current errors.
  44.     $!=0;
  45.     my $val = constant($constname, @_ ? $_[0] : 0);
  46.     if ($! != 0) {
  47.     if ($! =~ /Invalid/) {
  48.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  49.         goto &AutoLoader::AUTOLOAD;
  50.     }
  51.     else {
  52.         ($pack,$file,$line) = caller;
  53.         die "Your vendor has not defined Win32::EventLog macro $constname, used at $file line $line.";
  54.     }
  55.     }
  56.     eval "sub $AUTOLOAD { $val }";
  57.     goto &$AUTOLOAD;
  58. }
  59.  
  60.  
  61. #Open
  62. #this method serves as a class constructor for the EventLog class.
  63. #Note:
  64. #
  65. #
  66. #               Win32::EventLog::Open($this, "source name", "ServerName");
  67. #
  68. sub Open
  69. {
  70.     if($#_ < 1){
  71.         die "usage: Open(\$this, \$sourcename[, \$servername])\n";
  72.     }
  73.  
  74.     my ($SourceName, $ServerName)=($_[1],$_[2]);
  75.     my ($handle);
  76.  
  77.     #Set the handle for this object.
  78.  
  79.  
  80.     OpenEventLog($handle, $ServerName, $SourceName);
  81.     $_[0] = {'handle' => $handle,
  82.         'Source' => $SourceName,
  83.         'Computer' => $ServerName };
  84.  
  85.     bless $_[0];
  86. }
  87.  
  88. sub Backup
  89. {
  90.     $self = shift;
  91.     if($#_ != 0){
  92.         die " usage: Backup(Filename)\n";
  93.     }
  94.     local( $FileName )= @_;
  95.     local ($Result);
  96.  
  97.     $Result = BackupEventLog($self->{'handle'},$FileName);
  98.     if(!$Result){
  99.         $!=Win32::GetLastError();
  100.     }
  101.  
  102.     return($Result);
  103.  
  104. }
  105.  
  106. #Read
  107. # Note: the EventInfo arguement requires a hash reference.
  108. sub Read
  109. {
  110.     $self = shift;
  111.     if($#_ != 2){
  112.         die "usage: Read(flags, RecordOffSet, hashref)\n";
  113.     }
  114.     local( $ReadFlags,$RecordOffSet)=@_;
  115.     local ($Result);
  116.     local ($datalength, $dataoffset, $sid);
  117.     local ($length, $reserved, $recordnumber, $timegenerated, $timewritten, $eventid);
  118.     local ($eventtype, $numstrings, $eventcategory, $reservedflags);
  119.     local ($closingrecordnumber, $stringoffset, $usersidlength, $usersidoffset);
  120.  
  121. #The following is stolen shamelessly from Wyt's tests for the registry. 
  122.  
  123.     $Result = ReadEventLog( $self->{'handle'}, $ReadFlags,
  124.     $RecordOffSet, $header, $source, $computer, $sid, $data, $strings );
  125.  
  126.     ( $length, $reserved, $recordnumber, $timegenerated, $timewritten, $eventid,
  127.           $eventtype, $numstrings, $eventcategory, $reservedflags,
  128.           $closingrecordnumber, $stringoffset, $usersidlength, $usersidoffset,
  129.           $datalength, $dataoffset ) = unpack( 'l6s4l6', $header );
  130.  
  131. #make a hash out of the values returned from ReadEventLog.
  132.  
  133.     $_[2]={         'Source'    =>          $source,
  134.                 'Computer'  =>          $computer,
  135.                 'Length'        =>              $datalength,
  136.                 'Category' =>   $eventcategory,
  137.                 'RecordNumber' =>       $recordnumber,
  138.                 'TimeGenerated' =>      $timegenerated,
  139.                 'Timewritten' =>        $timewritten,
  140.                 'EventID' =>            $eventid,
  141.                 'EventType' =>          $eventtype,
  142.                 'ClosingRecordNumber' => $closingrecordnumber,
  143.                 'Strings' =>            $strings,
  144.                 'Data'  =>                      $data,
  145.             };
  146.  
  147.     if(!$Result){
  148.         $!=Win32::GetLastError();
  149.     }
  150.  
  151.     return($Result);
  152.  
  153. }
  154.  
  155. sub Report
  156. {
  157.     my $self = shift;
  158.     
  159.     if($#_ != 0){
  160.         die "usage: Report( %Event )\n";
  161.     }
  162.  
  163.     local( $EventInfo )= @_;
  164.     local ($Result);
  165.  
  166.     if( ref( $EventInfo)  == HASH ){
  167.  
  168.         local ($length, $reserved, $recordnumber, $timegenerated, $timewritten, $eventid);
  169.         local ($eventtype, $numstrings, $eventcategory, $reservedflags);
  170.         local ($closingrecordnumber, $stringoffset, $usersidlength, $usersidoffset);
  171.         local ($source, $reserved, $data, $strings);
  172.     
  173.         $eventcategory = $EventInfo->{'Category'};
  174.         $source=$self->{'Source'};
  175.         $computer=$self->{'Computer'};
  176.         $length=$EventInfo->{'Length'};
  177.         $recordnumber=$EventInfo->{'RecordNumber'};
  178.         $timegenerated=$EventInfo->{'TimeGenerated'};
  179.         $timewritten=$EventInfo->{'Timewritten'};
  180.         $eventid =$EventInfo->{'EventID'};
  181.         $eventtype=$EventInfo->{'EventType'};
  182.         $closingrecordnumber=$EventInfo->{'ClosingRecordNumber'};
  183.         $strings=$EventInfo->{'Strings'};
  184.         $data=$EventInfo->{'Data'};
  185.  
  186.         $Result = WriteEventLog( $computer,$source, $eventtype, $eventcategory, $eventid, $reserved, $data, $strings);
  187.  
  188.         } 
  189.         else{
  190.             die "Win32::EventLog::Report requires a hash reference as arg 3\n";
  191.         }
  192.  
  193.     if(!$Result){
  194.         $!=Win32::GetLastError();
  195.     }
  196.  
  197.     return($Result);
  198.  
  199.  
  200. }
  201.  
  202. sub GetOldest
  203. {
  204.     my $self=shift;
  205.     local ($Result);
  206.         
  207.     if($#_ != 0){
  208.         die "usage: GetOldest( $scalaref )\n";
  209.     }
  210.  
  211.     $Result = GetOldestEventLogRecord( $self->{'handle'},$_[0]);
  212.     if(!$Result){
  213.         $!=Win32::GetLastError();
  214.     }
  215.  
  216.     return($Result);
  217.  
  218. }
  219.  
  220. sub GetNumber
  221. {
  222.     my $self=shift;
  223.     local ($Result);
  224.  
  225.     if($#_ != 0){
  226.         die "usage: GetNumber( $scalaref )\n";
  227.     }
  228.  
  229.     $Result = GetNumberOfEventLogRecords($self->{'handle'}, $_[0]);
  230.  
  231.     if(!$Result){
  232.         $!=Win32::GetLastError();
  233.     }
  234.  
  235.     return($Result);
  236.  
  237.     
  238. }
  239. sub Clear
  240. {
  241.     my $self=shift;
  242.     local ($Result);
  243.     if($#_ != 0){
  244.         die "usage: Clear( $FileName )\n";
  245.     }
  246.     local( $filename) = @_;
  247.  
  248.     $Result = ClearEventLog($self->{'handle'}, $filename);
  249.  
  250.     if(!$Result){
  251.         $!=Win32::GetLastError();
  252.     }
  253.  
  254.     return($Result);
  255.  
  256. }
  257.  
  258. bootstrap Win32::EventLog;
  259.  
  260. # Preloaded methods go here.
  261.  
  262. # Autoload methods go after __END__, and are processed by the autosplit program.
  263.  
  264. 1;
  265. __END__
  266.     
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.