1 
2 #ifdef HAVE_CONFIG_H
3 #include "../../../ext_config.h"
4 #endif
5 
6 #include <php.h>
7 #include "../../../php_ext.h"
8 #include "../../../ext.h"
9 
10 #include <Zend/zend_operators.h>
11 #include <Zend/zend_exceptions.h>
12 #include <Zend/zend_interfaces.h>
13 
14 #include "kernel/main.h"
15 #include "kernel/operators.h"
16 #include "kernel/array.h"
17 #include "kernel/memory.h"
18 #include "kernel/fcall.h"
19 #include "kernel/object.h"
20 #include "kernel/exception.h"
21 
22 
23 /**
24  * Phalcon\Logger\Adapter\Syslog
25  *
26  * Sends logs to the system logger
27  *
28  * <code>
29  * use Phalcon\Logger;
30  * use Phalcon\Logger\Adapter\Syslog;
31  *
32  * // LOG_USER is the only valid log type under Windows operating systems
33  * $logger = new Syslog(
34  *     "ident",
35  *     [
36  *         "option"   => LOG_CONS | LOG_NDELAY | LOG_PID,
37  *         "facility" => LOG_USER,
38  *     ]
39  * );
40  *
41  * $logger->log("This is a message");
42  * $logger->log(Logger::ERROR, "This is an error");
43  * $logger->error("This is another error");
44  *</code>
45  */
ZEPHIR_INIT_CLASS(Phalcon_Logger_Adapter_Syslog)46 ZEPHIR_INIT_CLASS(Phalcon_Logger_Adapter_Syslog) {
47 
48 	ZEPHIR_REGISTER_CLASS_EX(Phalcon\\Logger\\Adapter, Syslog, phalcon, logger_adapter_syslog, phalcon_logger_adapter_ce, phalcon_logger_adapter_syslog_method_entry, 0);
49 
50 	zend_declare_property_bool(phalcon_logger_adapter_syslog_ce, SL("_opened"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
51 
52 	return SUCCESS;
53 
54 }
55 
56 /**
57  * Phalcon\Logger\Adapter\Syslog constructor
58  *
59  * @param string name
60  * @param array options
61  */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog,__construct)62 PHP_METHOD(Phalcon_Logger_Adapter_Syslog, __construct) {
63 
64 	zend_long ZEPHIR_LAST_CALL_STATUS;
65 	zval *name, *options = NULL, *option = NULL, *facility = NULL;
66 
67 	ZEPHIR_MM_GROW();
68 	zephir_fetch_params(1, 1, 1, &name, &options);
69 
70 	if (!options) {
71 		options = ZEPHIR_GLOBAL(global_null);
72 	}
73 
74 
75 	if (zephir_is_true(name)) {
76 		ZEPHIR_OBS_VAR(option);
77 		if (!(zephir_array_isset_string_fetch(&option, options, SS("option"), 0 TSRMLS_CC))) {
78 			ZEPHIR_INIT_NVAR(option);
79 			ZVAL_LONG(option, 4);
80 		}
81 		ZEPHIR_OBS_VAR(facility);
82 		if (!(zephir_array_isset_string_fetch(&facility, options, SS("facility"), 0 TSRMLS_CC))) {
83 			ZEPHIR_INIT_NVAR(facility);
84 			ZVAL_LONG(facility, 8);
85 		}
86 		ZEPHIR_CALL_FUNCTION(NULL, "openlog", NULL, 313, name, option, facility);
87 		zephir_check_call_status();
88 		if (1) {
89 			zephir_update_property_this(getThis(), SL("_opened"), ZEPHIR_GLOBAL(global_true) TSRMLS_CC);
90 		} else {
91 			zephir_update_property_this(getThis(), SL("_opened"), ZEPHIR_GLOBAL(global_false) TSRMLS_CC);
92 		}
93 	}
94 	ZEPHIR_MM_RESTORE();
95 
96 }
97 
98 /**
99  * Returns the internal formatter
100  */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog,getFormatter)101 PHP_METHOD(Phalcon_Logger_Adapter_Syslog, getFormatter) {
102 
103 	zval *_0, *_1$$3;
104 	zend_long ZEPHIR_LAST_CALL_STATUS;
105 
106 	ZEPHIR_MM_GROW();
107 
108 	ZEPHIR_OBS_VAR(_0);
109 	zephir_read_property_this(&_0, this_ptr, SL("_formatter"), PH_NOISY_CC);
110 	if (Z_TYPE_P(_0) != IS_OBJECT) {
111 		ZEPHIR_INIT_VAR(_1$$3);
112 		object_init_ex(_1$$3, phalcon_logger_formatter_syslog_ce);
113 		if (zephir_has_constructor(_1$$3 TSRMLS_CC)) {
114 			ZEPHIR_CALL_METHOD(NULL, _1$$3, "__construct", NULL, 0);
115 			zephir_check_call_status();
116 		}
117 		zephir_update_property_this(getThis(), SL("_formatter"), _1$$3 TSRMLS_CC);
118 	}
119 	RETURN_MM_MEMBER(getThis(), "_formatter");
120 
121 }
122 
123 /**
124  * Writes the log to the stream itself
125  */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog,logInternal)126 PHP_METHOD(Phalcon_Logger_Adapter_Syslog, logInternal) {
127 
128 	zval *context = NULL;
129 	zend_long type, time, ZEPHIR_LAST_CALL_STATUS;
130 	zval *message_param = NULL, *type_param = NULL, *time_param = NULL, *context_param = NULL, *appliedFormat = NULL, *_0 = NULL, *_1, *_2, *_3, *_4;
131 	zval *message = NULL;
132 
133 	ZEPHIR_MM_GROW();
134 	zephir_fetch_params(1, 4, 0, &message_param, &type_param, &time_param, &context_param);
135 
136 	zephir_get_strval(message, message_param);
137 	type = zephir_get_intval(type_param);
138 	time = zephir_get_intval(time_param);
139 	zephir_get_arrval(context, context_param);
140 
141 
142 	ZEPHIR_CALL_METHOD(&_0, this_ptr, "getformatter", NULL, 0);
143 	zephir_check_call_status();
144 	ZEPHIR_INIT_VAR(_1);
145 	ZVAL_LONG(_1, type);
146 	ZEPHIR_INIT_VAR(_2);
147 	ZVAL_LONG(_2, time);
148 	ZEPHIR_CALL_METHOD(&appliedFormat, _0, "format", NULL, 0, message, _1, _2, context);
149 	zephir_check_call_status();
150 	if (Z_TYPE_P(appliedFormat) != IS_ARRAY) {
151 		ZEPHIR_THROW_EXCEPTION_DEBUG_STR(phalcon_logger_exception_ce, "The formatted message is not valid", "phalcon/logger/adapter/syslog.zep", 101);
152 		return;
153 	}
154 	zephir_array_fetch_long(&_3, appliedFormat, 0, PH_NOISY | PH_READONLY, "phalcon/logger/adapter/syslog.zep", 104 TSRMLS_CC);
155 	zephir_array_fetch_long(&_4, appliedFormat, 1, PH_NOISY | PH_READONLY, "phalcon/logger/adapter/syslog.zep", 104 TSRMLS_CC);
156 	ZEPHIR_CALL_FUNCTION(NULL, "syslog", NULL, 314, _3, _4);
157 	zephir_check_call_status();
158 	ZEPHIR_MM_RESTORE();
159 
160 }
161 
162 /**
163  * Closes the logger
164  */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog,close)165 PHP_METHOD(Phalcon_Logger_Adapter_Syslog, close) {
166 
167 	zval *_0;
168 	zend_long ZEPHIR_LAST_CALL_STATUS;
169 
170 	ZEPHIR_MM_GROW();
171 
172 	_0 = zephir_fetch_nproperty_this(this_ptr, SL("_opened"), PH_NOISY_CC);
173 	if (!(zephir_is_true(_0))) {
174 		RETURN_MM_BOOL(1);
175 	}
176 	ZEPHIR_RETURN_CALL_FUNCTION("closelog", NULL, 315);
177 	zephir_check_call_status();
178 	RETURN_MM();
179 
180 }
181 
182