1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_ImportExport
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Operation abstract class
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_ImportExport_Model_Abstract extends Varien_Object
35: {
36: /**
37: * Enable loging
38: *
39: * @var boolean
40: */
41: protected $_debugMode = false;
42:
43: /**
44: * Loger instance
45: * @var Mage_Core_Model_Log_Adapter
46: */
47: protected $_logInstance;
48:
49: /**
50: * Fields that should be replaced in debug with '***'
51: *
52: * @var array
53: */
54: protected $_debugReplacePrivateDataKeys = array();
55:
56: /**
57: * Contains all log information
58: *
59: * @var array
60: */
61: protected $_logTrace = array();
62:
63: /**
64: * Log debug data to file.
65: * Log file dir: var/log/import_export/%Y/%m/%d/%time%_%operation_type%_%entity_type%.log
66: *
67: * @param mixed $debugData
68: * @return Mage_ImportExport_Model_Abstract
69: */
70: public function addLogComment($debugData)
71: {
72: if (is_array($debugData)) {
73: $this->_logTrace = array_merge($this->_logTrace, $debugData);
74: } else {
75: $this->_logTrace[] = $debugData;
76: }
77: if (!$this->_debugMode) {
78: return $this;
79: }
80:
81: if (!$this->_logInstance) {
82: $dirName = date('Y' . DS .'m' . DS .'d' . DS);
83: $fileName = join('_', array(
84: str_replace(':', '-', $this->getRunAt()),
85: $this->getScheduledOperationId(),
86: $this->getOperationType(),
87: $this->getEntity()
88: ));
89: $dirPath = Mage::getBaseDir('var') . DS . Mage_ImportExport_Model_Scheduled_Operation::LOG_DIRECTORY
90: . $dirName;
91: if (!is_dir($dirPath)) {
92: mkdir($dirPath, 0777, true);
93: }
94: $fileName = substr(strstr(Mage_ImportExport_Model_Scheduled_Operation::LOG_DIRECTORY, DS), 1)
95: . $dirName . $fileName . '.log';
96: $this->_logInstance = Mage::getModel('core/log_adapter', $fileName)
97: ->setFilterDataKeys($this->_debugReplacePrivateDataKeys);
98: }
99: $this->_logInstance->log($debugData);
100: return $this;
101: }
102:
103: /**
104: * Return human readable debug trace.
105: *
106: * @return array
107: */
108: public function getFormatedLogTrace()
109: {
110: $trace = '';
111: $lineNumber = 1;
112: foreach ($this->_logTrace as &$info) {
113: $trace .= $lineNumber++ . ': ' . $info . "\n";
114: }
115: return $trace;
116: }
117: }
118: