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