PHP Errors (AppleScript)

Tuesday, July 13, 2004


(*
script properties :

time_threshold: this is how many seconds back we should look in the error log
error_log_path: path to the apache error log
max_errors: the max number of lines of the error that we should read

*)

(*

This applescript copyright 2004, James A Spahr, james@spahr.org
Feel free to use and alter this code however you desire.
*)

property time_threshold : 10
property error_log_path : "/var/log/httpd/error_log"
property max_errors : 75



on run
   
   --
setup the array to hold our PHP errors
   
set myerrors to {}
   --
setup other variables that we will need
   
set cont to true
   
set startdate to 0
   
   --
read the apache error log
   
set errorstr to do shell script ("tail -n " & max_errors & " " & error_log_path as string)
   
set linect to the count (paragraphs of errorstr)
   
   
   --
loop backwards through the lines in the apache error log
   
repeat while cont
      
      --
get the current line of the error log
      
set errorline to paragraph linect of errorstr as string
      
      --
read the timestamp in the error log line
      
set o4 to offset of "]" in errorline
      
set d to (characters 2 thru (o4 - 1) of errorline as string)
      
set d to date d
      
      --
see if this is the first date we have read, if so set the startdate
      
if startdate is 0 then
         
set startdate to d
      
end if
      
      --
check to see if we are beyond the time threshold
      
if d + time_threshold < startdate then
         
set cont to false
      
end if
      
      
if cont then
         
         --
check to see if this line in the error log is PHP related
         
if errorline contains "[error] PHP" then
            
            --
compute offsets of known tokens in the error log
            
set o1 to offset of " in /" in errorline
            
set o2 to offset of " on line " in errorline
            
set o3 to offset of "[error] PHP" in errorline
            
            --
parse the error log line
            
set f to characters (o1 + 4) thru (o2 - 1) of errorline as string
            
set rl to characters (o2 + 9) thru (length of errorline) of errorline as string
            
set m to characters o3 thru o1 of errorline as string
            
            --
coerce the varaibles that need it
            
set rl to rl as number
            
set f to POSIX file f as alias
            
            --
read the referenced file
            (*
NOTE: we might be able to speed things up by caching this read
            , but I'm not having speed isues right now
*)
            
set w to read f
            
            --
find the line where the error happens and compute the character offsets
            
set theline to paragraph rl of w as string
            
set thetext to the text of w as string
            
            
set so to the offset of theline in thetext
            
set eo to so + the (length of theline)
            
            --
build the myerrors array in bbedit because we need it's AE constants
            
tell application "BBEdit"
               --
compute the type of error
               
set rk to note_kind
               
if m contains "Warning:" then
                  
set rk to warning_kind
               
else if m contains "error:" then
                  
set rk to error_kind
               
end if
               
               --
add error tp array
               
set myerrors to myerrors & {{result_kind:rk, result_file:f, message:m, result_line:rl, start_offset:so, end_offset:eo}}
            
end tell
            
         
end if
      
end if
      
      --
decrease the line count
      
set linect to linect - 1
      
      --
make sure we have not reached the end (or rather the beginning) of the file
      
if linect < 1 then
         
set cont to false
      
end if
      
   
end repeat
   
   --
tell bbedit to show the results browser with our errors
   
tell application "BBEdit"
      
make new results browser with data myerrors with properties {name:"PHP Errors @ " & startdate as string}
   
end tell
   
end run

Top | Made with Script Debugger 3.0