/** * Retrieve last 100 lines of log from /var/findik/filter.log * @return an array of VOLog */ public function getData($searchKey = null) { $lines = get_lines(); $ret = array(); foreach ($lines as $line) { $tmp = new VOLog(); $logInfo = explode(" ", $line); if ($logInfo[2] == "WARN") { $tmp->date = $logInfo[0]; $tmp->time = $logInfo[1]; list($x, $tmp->result) = explode(":", $logInfo[5]); list($x, $tmp->localAddr) = explode(":", $logInfo[6]); list($x, $tmp->domain) = explode(":", $logInfo[7]); list($x, $tmp->url) = explode(":", $logInfo[8], 2); list($x, $tmp->reqSize) = explode(":", $logInfo[9]); list($x, $tmp->respSize) = explode(":", $logInfo[10]); } else { $tmp->date = $logInfo[0]; $tmp->time = $logInfo[1]; list($x, $tmp->result) = explode(":", $logInfo[4]); list($x, $tmp->localAddr) = explode(":", $logInfo[5]); list($x, $tmp->domain) = explode(":", $logInfo[6]); list($x, $tmp->url) = explode(":", $logInfo[7], 2); list($x, $tmp->reqSize) = explode(":", $logInfo[8]); list($x, $tmp->respSize) = explode(":", $logInfo[9]); } $ret[] = $tmp; } return array_reverse($ret); }
/** * Imprime as informações extraídas * * @param array $addressList Lista de endereços * @param string $type Tipo do endereço * @return undefined */ function parse_lines_from_list($addressList, $type) { echo "\"data\" : {"; foreach ($addressList as $address) { $name = $address[0]; $code = $address[1]; // pega as informações do endereço $lines = get_lines($address[1]); // re-ordena as linhas sort($lines); // transforma de array PHP para JSON $lines = json_encode($lines); // monta as propriedades de cada linha echo "\n \"{$code}\" : {\n \"name\" : \"{$name}\",\n \"type\" : \"{$type}\",\n \"lines\" : {$lines}\n },"; } echo "\n}"; }
* Useful for large files, such as logs, when you want to process lines in PHP * or write lines to a database. * * @author Dan Ruscoe */ // The name of the file to read define('FILE_NAME', 'file.log'); // The number of lines to display define('LINES_TO_DISPLAY', 10); if (!is_file(FILE_NAME)) { die("Not a file - " . FILE_NAME . "\n"); } if (LINES_TO_DISPLAY < 1) { die("Number of lines to display must be greater than 0\n"); } if (!($lines = get_lines(FILE_NAME, LINES_TO_DISPLAY))) { die("Could not get lines from file - " . FILE_NAME . "\n"); } foreach ($lines as $line) { echo $line; } exit; /** * Returns an array containing X number of rows from the end of a file. * * @param string $filename * @param int $lines_to_display * @return array */ function get_lines($filename, $lines_to_display) {
//move the pointer at the first character; rewind($fp); //out of control break; } //substract one character from the pointer position $pointer--; //move the pointer at the end of the file with 'offset'(pointer) position fseek($fp, $pointer, SEEK_END); //get the current character from pointer $char = fgetc($fp); } //get a character $line = fgets($fp); //push into array lines array_push($lines, $line); //reset the character; $char = ''; } return array_reverse($lines); } $lines = get_lines(FILENAME, LINES_TO_DISPLAY); foreach ($lines as $line) { echo $line . "<br/>"; } if (isset($_GET['show'])) { highlight_file($_SERVER['SCRIPT_FILENAME']); } exit; ?>
function page_load() { global $CHAT_DATA_FILE_NAME; global $CHAT_KEY_LAST_INDEX; global $CHAT_CONNECTION_TIMEOUT; global $CHAT_KEY_FORMAT; global $CHAT_KEY_CALLBACK; //chat lines $lines = get_lines(); // How many lines are there in total. $lineCount = count($lines); // The last sequence number retrieved in that session. $lastIndex = get($CHAT_KEY_LAST_INDEX, 0); // Used for detecting connection timeout. $startTime = $currentTime = time(); // Used for detecting file changes. $startModified = $currentModified = filemtime($CHAT_DATA_FILE_NAME); // request format $format = get($CHAT_KEY_FORMAT, 'json'); // calback $callback = get($CHAT_KEY_CALLBACK, 'callback'); $isNewData = $lineCount - 1 > $lastIndex; // if there is unread data, flush immediately. if ($isNewData) { flush_buffer($lines, $lastIndex, $format, $callback); die; } //busy-wait while (true) { // Give some breathing time for the CPU. usleep(10000); // Reset file statistics. clearstatcache(); $currentTime = time(); $currentModified = filemtime($CHAT_DATA_FILE_NAME); $isFileModified = $currentModified - $startModified > 0; // Someone else has modified the file, get contents and flush. if ($isFileModified) { $lines = get_lines(); break; } $isResponseTimedOut = $currentTime - $startTime > $CHAT_CONNECTION_TIMEOUT; // Nothing special, break and give a default response. if ($isResponseTimedOut) { break; } } flush_buffer($lines, $lastIndex, $format, $callback); die; }