1 <?php
2 /**
3 * Allows log files to be written to for debugging purposes.
4 *
5 * @class WC_Logger
6 * @version 1.6.4
7 * @package WooCommerce/Classes
8 * @category Class
9 * @author WooThemes
10 */
11 class WC_Logger {
12
13 /**
14 * @var array Stores open file _handles.
15 * @access private
16 */
17 private $_handles;
18
19 /**
20 * Constructor for the logger.
21 *
22 * @access public
23 * @return void
24 */
25 public function __construct() {
26 $this->_handles = array();
27 }
28
29
30 /**
31 * Destructor.
32 *
33 * @access public
34 * @return void
35 */
36 public function __destruct() {
37 foreach ( $this->_handles as $handle )
38 @fclose( escapeshellarg( $handle ) );
39 }
40
41
42 /**
43 * Open log file for writing.
44 *
45 * @access private
46 * @param mixed $handle
47 * @return bool success
48 */
49 private function open( $handle ) {
50
51 if ( isset( $this->_handles[ $handle ] ) )
52 return true;
53
54 if ( $this->_handles[ $handle ] = @fopen( WC()->plugin_path() . '/logs/' . $this->file_name( $handle ) . '.txt', 'a' ) )
55 return true;
56
57 return false;
58 }
59
60
61 /**
62 * Add a log entry to chosen file.
63 *
64 * @access public
65 * @param mixed $handle
66 * @param mixed $message
67 * @return void
68 */
69 public function add( $handle, $message ) {
70 if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
71 $time = date_i18n( 'm-d-Y @ H:i:s -' ); //Grab Time
72 @fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
73 }
74 }
75
76
77 /**
78 * Clear entries from chosen file.
79 *
80 * @access public
81 * @param mixed $handle
82 * @return void
83 */
84 public function clear( $handle ) {
85
86 if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) )
87 @ftruncate( $this->_handles[ $handle ], 0 );
88 }
89
90
91 /**
92 * file_name function.
93 *
94 * @access private
95 * @param mixed $handle
96 * @return string
97 */
98 private function file_name( $handle ) {
99 return $handle . '-' . sanitize_file_name( wp_hash( $handle ) );
100 }
101
102 }