woocommerce/includes/class-wc-logger.php

128 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
2015-11-03 13:53:50 +00:00
* Allows log files to be written to for debugging purposes
*
* @class WC_Logger
* @version 1.6.4
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
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
/**
* Stores open file _handles.
*
* @var array
2012-08-15 17:08:42 +00:00
* @access private
*/
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
*/
2012-12-14 21:44:43 +00:00
public function __construct() {
2012-12-14 22:00:04 +00:00
$this->_handles = array();
}
2012-08-14 19:42:38 +00:00
/**
* Destructor.
*/
2012-12-14 21:44:43 +00:00
public function __destruct() {
foreach ( $this->_handles as $handle ) {
2016-04-19 15:56:41 +00:00
if ( is_resource( $handle ) ) {
fclose( $handle );
}
}
}
2012-08-14 19:42:38 +00:00
/**
2012-08-14 19:42:38 +00:00
* Open log file for writing.
*
* @param string $handle
* @param string $mode
2012-08-14 19:42:38 +00:00
* @return bool success
*/
2016-04-19 15:56:41 +00:00
protected function open( $handle, $mode = 'a' ) {
if ( isset( $this->_handles[ $handle ] ) ) {
2012-08-14 19:42:38 +00:00
return true;
}
2012-08-14 19:42:38 +00:00
if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
2012-08-14 19:42:38 +00:00
return true;
}
2012-08-14 19:42:38 +00:00
return false;
}
2012-08-14 19:42:38 +00:00
2016-04-19 15:56:41 +00:00
/**
* Close a handle.
*
* @param string $handle
* @return bool success
*/
protected function close( $handle ) {
$result = false;
if ( is_resource( $this->_handles[ $handle ] ) ) {
$result = fclose( $this->_handles[ $handle ] );
unset( $this->_handles[ $handle ] );
}
return $result;
}
/**
2012-08-14 19:42:38 +00:00
* Add a log entry to chosen file.
*
* @param string $handle
* @param string $message
*
* @return bool
*/
public function add( $handle, $message ) {
$result = false;
2013-01-10 14:56:07 +00:00
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
$time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
2016-04-19 15:56:41 +00:00
$result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
2012-08-14 19:42:38 +00:00
}
2015-07-16 19:55:48 +00:00
do_action( 'woocommerce_log_add', $handle, $message );
2012-08-14 19:42:38 +00:00
2016-04-19 13:00:40 +00:00
return false !== $result;
}
2012-08-14 19:42:38 +00:00
/**
2013-03-03 17:07:31 +00:00
* Clear entries from chosen file.
2012-08-14 19:42:38 +00:00
*
* @param string $handle
*
* @return bool
*/
public function clear( $handle ) {
$result = false;
// Close the file if it's already open.
2016-04-19 15:56:41 +00:00
$this->close( $handle );
/**
* $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file,
* and truncate the file to zero length.
*/
if ( $this->open( $handle, 'w' ) && is_resource( $this->_handles[ $handle ] ) ) {
$result = true;
}
2015-07-16 19:55:48 +00:00
do_action( 'woocommerce_log_clear', $handle );
2016-04-19 13:00:40 +00:00
return $result;
}
2012-11-27 16:22:47 +00:00
2016-04-19 13:00:40 +00:00
}