self::MAX_BYTES ) { @rename( $file, $dir . '/spinix.old.log' ); // phpcs:ignore } $line = '[' . gmdate( 'Y-m-d H:i:s' ) . ' UTC] ' . strtoupper( $level ) . ( $job ? ' [' . $job . ']' : '' ) . ' ' . str_replace( array( "\r", "\n" ), ' ', $message ) . "\n"; @file_put_contents( $file, $line, FILE_APPEND | LOCK_EX ); // phpcs:ignore } /** * Last N lines of the log. * * @param int $lines Lines. * @return string */ public static function tail( $lines = 200 ) { $file = Spinix_Storage::logs_dir() . '/spinix.log'; if ( ! is_file( $file ) ) { return ''; } $size = filesize( $file ); $h = fopen( $file, 'rb' ); // phpcs:ignore $read = min( $size, 262144 ); fseek( $h, -$read, SEEK_END ); $data = fread( $h, $read ); // phpcs:ignore Spinix_Env::close( $h ); $arr = explode( "\n", trim( (string) $data ) ); return implode( "\n", array_slice( $arr, -$lines ) ); } /** * Record a finished operation in the history list. * * @param array $entry Entry. */ public static function history_add( $entry ) { $file = Spinix_Storage::logs_dir() . '/history.json'; $list = self::history(); array_unshift( $list, $entry + array( 'time' => time() ) ); $list = array_slice( $list, 0, 50 ); @file_put_contents( $file, wp_json_encode( $list ), LOCK_EX ); // phpcs:ignore } /** * History list. * * @return array */ public static function history() { $file = Spinix_Storage::logs_dir() . '/history.json'; if ( ! is_file( $file ) ) { return array(); } $list = Spinix_Env::read_json( $file ); return is_array( $list ) ? $list : array(); } }