home *** CD-ROM | disk | FTP | other *** search
/ Freelog 15 / FREELOG 15.ISO / WebMaster / Perl / PERL5106.ZIP / perl5 / Lib / Win32 / EVENTLOG.PM < prev    next >
Encoding:
Perl POD Document  |  1996-01-17  |  6.3 KB  |  280 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 for hip communications.
  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.     local($constname);
  42.     ($constname = $AUTOLOAD) =~ s/.*:://;
  43.     #reset $! to zero to reset any current errors.
  44.     $!=0;
  45.     $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 EventLog macro $constname, used at $file line $line.
  54. ";
  55.     }
  56.     }
  57.     eval "sub $AUTOLOAD { $val }";
  58.     goto &$AUTOLOAD;
  59. }
  60.  
  61.  
  62. #Open
  63. #this method serves as a class constructor for the EventLog class.
  64. #Note:
  65. #
  66. #
  67. #               Open Win32::EventLog( $this, "source name","ServerName");
  68. #
  69. sub Open
  70. {
  71.     shift;
  72.     
  73.     if ($#_ != 2){
  74.         die "usage: Open(\$ObjRef,\$servername,\$sourcename)\n";
  75.     }
  76.  
  77.     local($ServerName,$SourceName)=($_[1],$_[2]);
  78.     local $handle;
  79.  
  80. #Set the handle for this object.
  81.  
  82.     OpenEventLog($handle, $ServerName,$SourceName);
  83.     $_[0]={'handle' => $handle };
  84.  
  85. #Return the object.
  86.  
  87.     bless $_[0];
  88.     
  89. }
  90.  
  91. sub Backup
  92. {
  93.     $self = shift;
  94.     if( $#_ != 0 ){
  95.         die " usage: Backup( $Filename )\n";
  96.     }
  97.     local( $FileName )= @_;
  98.     local $Result;
  99.  
  100.     $Result=BackupEventLog($self->{'handle'},$FileName);
  101.     if(!$Result){
  102.         $!=Win32::GetLastError();
  103.     }
  104.  
  105.     return($Result);
  106.  
  107. }
  108.  
  109. #Read
  110. # Note: the EventInfo arguement requires a hash reference.
  111. sub Read
  112. {
  113.     $self = shift;
  114.     if( $#_ != 2 ){
  115.         die "usage: Read( $flags,$RecordOffSet,$hashref)\n";
  116.     }
  117.     local( $ReadFlags,$RecordOffSet)=@_;
  118.     local $Result;
  119.     local $buffer,$dwRead,$Next,$datalength, $dataoffset;
  120.     local $length, $reserved, $recordnumber, $timegenerated, $timewritten, $eventid;
  121.     local $eventtype, $numstrings, $eventcategory, $reservedflags;
  122.     local $closingrecordnumber, $stringoffset, $usersidlength, $usersidoffset;
  123.  
  124. #The following is stolen shamelessly from Wyt's tests for the registry. 
  125.  
  126.     $Result = NTReadEventLog( $self->{'handle'}, $ReadFlags,
  127.     $RecordOffSet, $header, $source, $computer, $sid, $data, $strings );
  128.  
  129.     ( $length, $reserved, $recordnumber, $timegenerated, $timewritten, $eventid,
  130.           $eventtype, $numstrings, $eventcategory, $reservedflags,
  131.           $closingrecordnumber, $stringoffset, $usersidlength, $usersidoffset,
  132.           $datalength, $dataoffset ) = unpack( 'l6s4l6', $header );
  133.  
  134. #make a hash out of the values returned from NTReadEventLog.
  135.  
  136.     $_[2]={         'Source'    =>          $source,
  137.                 'Computer'  =>          $computer,
  138.                 'Length'        =>              $datalength,
  139.                 'Category' =>   $eventcategory,
  140.                 'RecordNumber' =>       $recordnumber,
  141.                 'TimeGenerated' =>      $timegenerated,
  142.                 'Timewritten' =>        $timewritten,
  143.                 'EventID' =>            $eventid,
  144.                 'EventType' =>          $eventtype,
  145.                 'ClosingRecordNumber' => $closingrecordnumber,
  146.                 'Strings' =>            $strings,
  147.                 'Data'  =>                      $data,
  148.             };
  149.  
  150.     if(!$Result){
  151.         $!=Win32::GetLastError();
  152.     }
  153.  
  154.     return($Result);
  155.  
  156. }
  157.  
  158. sub Report
  159. {
  160.     my $self = shift;
  161.     
  162.     if ($#_ != 0 ){
  163.         die "usage: Report( %Event )\n";
  164.     }
  165.  
  166.     local( $EventInfo )= @_;
  167.     local $Result;
  168.  
  169.     if( ref( $EventInfo)  == HASH ){
  170.  
  171.         local $length, $reserved, $recordnumber, $timegenerated, $timewritten, $eventid;
  172.         local $eventtype, $numstrings, $eventcategory, $reservedflags;
  173.         local $closingrecordnumber, $stringoffset, $usersidlength, $usersidoffset;
  174.  
  175.         local $server, $source, $eventType, $category, $eventID, $reserved, $data, $strings;
  176.     
  177.         $eventcategory = $EventInfo->{'Category'};
  178.         $source=$EventInfo->{   'Source'  };    
  179.         $computer=$EventInfo->{ 'Computer' };           
  180.         $length=$EventInfo->{'Length'};                 
  181.         $recordnumber=$EventInfo->{'RecordNumber'};
  182.         $timegenerated=$EventInfo->{'TimeGenerated'};
  183.         $timewritten=$EventInfo->{'Timewritten'};
  184.         $eventid =$EventInfo->{'EventID'};
  185.         $eventtype=$EventInfo->{'EventType'};
  186.         $closingrecordnumber=$EventInfo->{'ClosingRecordNumber'};
  187.         $strings=$EventInfo->{'Strings'};
  188.         $data=$EventInfo->{'Data'};
  189.  
  190.         $Result = NTWriteEventLog( $computer,$source, $eventtype, $eventcategory, $eventid, $reserved, $data, $strings);
  191.  
  192.         } 
  193.         else{
  194.             die "Win32::EventLog::Report requires a hash reference as arg 3\n";
  195.         }
  196.  
  197.     if(!$Result){
  198.         $!=Win32::GetLastError();
  199.     }
  200.  
  201.     return($Result);
  202.  
  203.  
  204. }
  205.  
  206. sub GetOldest
  207. {
  208.     my $self=shift;
  209.     local $Result;
  210.         
  211.     if($#_ != 0 ){
  212.         die "usage: GetOldest( $scalaref )\n";
  213.     }
  214.  
  215.     $Result= GetOldestEventLogRecord( $self->{'handle'},$_[0]);
  216.     if(!$Result){
  217.         $!=Win32::GetLastError();
  218.     }
  219.  
  220.     return($Result);
  221.  
  222. }
  223.  
  224. sub GetNumber
  225. {
  226.     my $self=shift;
  227.     local $Result;
  228.  
  229.     if($#_ != 0 ){
  230.         die "usage: GetNumber( $scalaref )\n";
  231.     }
  232.  
  233.     $Result = GetNumberOfEventLogRecords( $self->{'handle'},$_[0] );
  234.  
  235.     if(!$Result){
  236.         $!=Win32::GetLastError();
  237.     }
  238.  
  239.     return($Result);
  240.  
  241.     
  242. }
  243. sub Clear
  244. {
  245.     my $self=shift;
  246.     local $Result;
  247.     if($#_ != 0 ){
  248.         die "usage: Clear( $FileName )\n";
  249.     }
  250.     local( $filename) = @_;
  251.  
  252.     $Result =ClearEventLog( $self->{'handle'},$filename);
  253.  
  254.     if(!$Result){
  255.         $!=Win32::GetLastError();
  256.     }
  257.  
  258.     return($Result);
  259.  
  260. }
  261.  
  262. bootstrap Win32::EventLog;
  263.  
  264. # Preloaded methods go here.
  265.  
  266. # Autoload methods go after __END__, and are processed by the autosplit program.
  267.  
  268. 1;
  269. __END__
  270.     
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.