2011-11-15 13:08:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Allows log files to be written to for debugging purposes.
|
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Logger
|
2012-08-14 19:42:38 +00:00
|
|
|
* @version 1.6.4
|
2012-08-14 22:43:48 +00:00
|
|
|
* @package WooCommerce/Classes
|
2012-08-14 19:42:38 +00:00
|
|
|
* @author WooThemes
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Logger {
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/**
|
|
|
|
* @var array Stores open file handles.
|
|
|
|
* @access private
|
|
|
|
*/
|
2011-11-15 13:08:56 +00:00
|
|
|
private $handles;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-15 17:08:42 +00:00
|
|
|
* Constructor for the logger.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-11-15 13:08:56 +00:00
|
|
|
function __construct() {
|
|
|
|
$this->handles = array();
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-11-15 13:08:56 +00:00
|
|
|
function __destruct() {
|
2012-10-17 12:28:41 +00:00
|
|
|
foreach ( $this->handles as $handle )
|
2012-10-24 10:28:05 +00:00
|
|
|
@fclose( escapeshellarg( $handle ) );
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Open log file for writing.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param mixed $handle
|
|
|
|
* @return bool success
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
|
|
|
private function open( $handle ) {
|
|
|
|
global $woocommerce;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-10-17 12:28:41 +00:00
|
|
|
if ( isset( $this->handles[ $handle ] ) )
|
2012-08-14 19:42:38 +00:00
|
|
|
return true;
|
|
|
|
|
2012-10-17 12:28:41 +00:00
|
|
|
if ( $this->handles[ $handle ] = @fopen( $woocommerce->plugin_path() . '/logs/' . $this->file_name( $handle ) . '.txt', 'a' ) )
|
2012-08-14 19:42:38 +00:00
|
|
|
return true;
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Add a log entry to chosen file.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $handle
|
|
|
|
* @param mixed $message
|
|
|
|
* @return void
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
|
|
|
public function add( $handle, $message ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
if ( $this->open( $handle ) ) {
|
2012-08-25 09:20:56 +00:00
|
|
|
$time = date_i18n( 'm-d-Y @ H:i:s -' ); //Grab Time
|
2012-10-17 12:28:41 +00:00
|
|
|
fwrite( $this->handles[ $handle ], $time . " " . $message . "\n" );
|
2012-08-14 19:42:38 +00:00
|
|
|
}
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Clear entrys from chosen file.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $handle
|
|
|
|
* @return void
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
|
|
|
public function clear( $handle ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
if ( $this->open( $handle ) )
|
2012-10-17 12:28:41 +00:00
|
|
|
ftruncate( $this->handles[ $handle ], 0 );
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
|
|
|
|
2012-10-17 12:28:41 +00:00
|
|
|
/**
|
|
|
|
* file_name function.
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-10-17 12:28:41 +00:00
|
|
|
* @access private
|
|
|
|
* @param mixed $handle
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function file_name( $handle ) {
|
|
|
|
return $handle . '-' . sanitize_file_name( wp_hash( $handle ) );
|
2012-08-14 19:42:38 +00:00
|
|
|
}
|
2012-10-17 12:28:41 +00:00
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|