Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Phalcon\Logger\Adapter compatible with PSR-3 #1873

Merged
merged 7 commits into from Jan 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
- Phalcon\Logger\Adapter::commit() now clears the queue (#1742)
- Phalcon\Logger\Adapter now implements Phalcon\Logger\AdapterInterface (#1852)
- Phalcon\Logger\Formatter now implements Phalcon\Logger\FormatterInterface (#1852)
- Phalcon\Logger\Adapter is now PSR-3 compliant (#1873)
- Phalcon\Mvc:
- Phalcon\Mvc\Application::handle() now checks whether the class exists before include()'ing its file (#812, #818)
- Phalcon\Mvc\Model\Criteria::fromInput() now sets _modelName (#866, #873)
Expand Down
151 changes: 129 additions & 22 deletions ext/logger/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static const zend_function_entry phalcon_logger_adapter_method_entry[] = {
PHP_ME(Phalcon_Logger_Adapter, warning, arginfo_phalcon_logger_adapterinterface_warning, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, alert, arginfo_phalcon_logger_adapterinterface_alert, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter, log, arginfo_phalcon_logger_adapterinterface_log, ZEND_ACC_PUBLIC)
ZEND_FENTRY(logInternal, NULL, arginfo_phalcon_logger_adapter_loginternal, ZEND_ACC_PROTECTED | ZEND_ACC_ABSTRACT)
PHP_FE_END
};

Expand All @@ -90,6 +91,44 @@ PHALCON_INIT_CLASS(Phalcon_Logger_Adapter){
return SUCCESS;
}

static int phalcon_logger_adapter_string_level_to_int(const zval *level)
{
const char *s = Z_STRVAL_P(level);
size_t len = (size_t)(Z_STRLEN_P(level));
size_t i;

struct sl {
const char *str;
size_t len;
int level;
};

static struct sl lookup_table[] = {
{ ZEND_STRL("emergency"), PHALCON_LOGGER_EMERGENCY },
{ ZEND_STRL("alert"), PHALCON_LOGGER_ALERT },
{ ZEND_STRL("critical"), PHALCON_LOGGER_CRITICAL },
{ ZEND_STRL("error"), PHALCON_LOGGER_ERROR },
{ ZEND_STRL("warning"), PHALCON_LOGGER_WARNING },
{ ZEND_STRL("notice"), PHALCON_LOGGER_NOTICE },
{ ZEND_STRL("info"), PHALCON_LOGGER_INFO },
{ ZEND_STRL("debug"), PHALCON_LOGGER_DEBUG }
};

assert(Z_TYPE_P(level) == IS_STRING);

for (i=0; i<sizeof(lookup_table)/sizeof(lookup_table[0]); ++i) {
if (lookup_table[i].len == len && !memcmp(lookup_table[i].str, s, len)) {
return lookup_table[i].level;
}
}

{
TSRMLS_FETCH();
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unrecognized log level '%s'", s);
return PHALCON_LOGGER_CUSTOM;
}
}

/**
* Filters the logs sent to the handlers that are less or equal than a specific level
*
Expand All @@ -98,12 +137,22 @@ PHALCON_INIT_CLASS(Phalcon_Logger_Adapter){
*/
PHP_METHOD(Phalcon_Logger_Adapter, setLogLevel){

zval **level;
zval **level, *lvl;

phalcon_fetch_params_ex(1, 0, &level);
PHALCON_ENSURE_IS_LONG(level);
if (Z_TYPE_PP(level) == IS_STRING) {
PHALCON_ALLOC_GHOST_ZVAL(lvl);
ZVAL_LONG(lvl, phalcon_logger_adapter_string_level_to_int(*level));
}
else if (Z_TYPE_PP(level) != IS_LONG) {
PHALCON_ENSURE_IS_LONG(level);
lvl = *level;
}
else {
lvl = *level;
}

phalcon_update_property_this(this_ptr, SL("_logLevel"), *level TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_logLevel"), lvl TSRMLS_CC);
RETURN_THISW();
}

Expand Down Expand Up @@ -165,7 +214,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, begin){
PHP_METHOD(Phalcon_Logger_Adapter, commit){

zval *transaction, *queue, *message_str = NULL;
zval *type = NULL, *time = NULL;
zval *type = NULL, *time = NULL, *context = NULL;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -193,11 +242,13 @@ PHP_METHOD(Phalcon_Logger_Adapter, commit){
PHALCON_INIT_NVAR(message_str);
PHALCON_INIT_NVAR(type);
PHALCON_INIT_NVAR(time);
PHALCON_INIT_NVAR(context);

phalcon_call_method(message_str, *message, "getmessage");
phalcon_call_method(type, *message, "gettype");
phalcon_call_method(time, *message, "gettime");
phalcon_call_method_p3_noret(this_ptr, "loginternal", message_str, type, time);
phalcon_call_method(context, *message, "getcontext");
phalcon_call_method_p4_noret(this_ptr, "loginternal", message_str, type, time, context);
}

if (Z_REFCOUNT_P(queue) == 1 || Z_ISREF_P(queue)) {
Expand Down Expand Up @@ -239,14 +290,19 @@ PHP_METHOD(Phalcon_Logger_Adapter, rollback){

static void phalcon_logger_adapter_log_helper(INTERNAL_FUNCTION_PARAMETERS, int level)
{
zval *message, *type;
zval **message, **context = NULL, *type;

phalcon_fetch_params(0, 1, 0, &message);
phalcon_fetch_params_ex(1, 1, &message, &context);
PHALCON_ENSURE_IS_STRING(message);

PHALCON_ALLOC_GHOST_ZVAL(type);
ZVAL_LONG(type, level);

if (FAILURE == phalcon_call_method_params(return_value, return_value_ptr, getThis(), SL("log"), zend_inline_hash_func(SS("log")) TSRMLS_CC, 2, message, type)) {
if (!context) {
context = &PHALCON_GLOBAL(z_null);
}

if (FAILURE == phalcon_call_method_params(return_value, return_value_ptr, getThis(), SL("log"), zend_inline_hash_func(SS("log")) TSRMLS_CC, 3, type, *message, *context)) {
if (return_value_ptr && EG(exception)) {
ALLOC_INIT_ZVAL(*return_value_ptr);
}
Expand All @@ -260,6 +316,7 @@ static void phalcon_logger_adapter_log_helper(INTERNAL_FUNCTION_PARAMETERS, int
* Sends/Writes an emergency message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, emergency){
Expand All @@ -271,7 +328,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, emergency){
* Sends/Writes a debug message to the log
*
* @param string $message
* @param ing $type
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, debug){
Expand All @@ -283,6 +340,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, debug){
* Sends/Writes an error message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, error){
Expand All @@ -294,6 +352,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, error){
* Sends/Writes an info message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, info){
Expand All @@ -305,6 +364,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, info){
* Sends/Writes a notice message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, notice){
Expand All @@ -316,6 +376,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, notice){
* Sends/Writes a warning message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, warning){
Expand All @@ -327,6 +388,7 @@ PHP_METHOD(Phalcon_Logger_Adapter, warning){
* Sends/Writes an alert message to the log
*
* @param string $message
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, alert){
Expand All @@ -337,42 +399,87 @@ PHP_METHOD(Phalcon_Logger_Adapter, alert){
/**
* Logs messages to the internal logger. Appends messages to the log
*
* @param mixed type
* @param string $message
* @param int $type
* @param array $context
* @return Phalcon\Logger\Adapter
*/
PHP_METHOD(Phalcon_Logger_Adapter, log){

zval *message, *type = NULL, *timestamp, *transaction;
zval *queue_item, *log_level;
zval **message = NULL, **type, **context = NULL, *timestamp, *transaction;
zval *queue_item, *log_level, *level;
int i_level;

phalcon_fetch_params_ex(1, 2, &type, &message, &context);

/*
* Backwards compatibility.
*
* PSR-3 says:
*
* public function log($level, $message, array $context = array());
*
* Our old definition:
*
* public function log($message, $level = null)
*
* Now we want to implement PSR-3
* Thus:
* - when $message === null, $level is $message and $level is DEBUG
* - when typeof($message) == 'int' && typeof($level) == 'string', then
* $message is $level and $level is $message.
*/
if (message == NULL) {
message = type;
type = NULL;
}
else if (Z_TYPE_PP(message) == IS_LONG && Z_TYPE_PP(type) == IS_STRING) {
zval **tmp = message;
message = type;
type = tmp;
}

PHALCON_MM_GROW();
if (!context) {
context = &PHALCON_GLOBAL(z_null);
}

phalcon_fetch_params(1, 1, 1, &message, &type);

if (!type) {
PHALCON_INIT_VAR(type);
ZVAL_LONG(type, PHALCON_LOGGER_DEBUG);
i_level = PHALCON_LOGGER_DEBUG;
}

else if (Z_TYPE_PP(type) == IS_STRING) {
i_level = phalcon_logger_adapter_string_level_to_int(*type);
}
else {
PHALCON_ENSURE_IS_LONG(type);
i_level = Z_LVAL_PP(type);
}

log_level = phalcon_fetch_nproperty_this(this_ptr, SL("_logLevel"), PH_NOISY_CC);

/* Only log the message if this is allowed by the current log level */
if (PHALCON_GE(log_level, type)) {
if (phalcon_get_intval(log_level) >= i_level) {
PHALCON_MM_GROW();

PHALCON_INIT_VAR(timestamp);
ZVAL_LONG(timestamp, (long)time(NULL));

PHALCON_INIT_VAR(level);
ZVAL_LONG(level, i_level);

transaction = phalcon_fetch_nproperty_this(this_ptr, SL("_transaction"), PH_NOISY_CC);
if (zend_is_true(transaction)) {
PHALCON_INIT_VAR(queue_item);
object_init_ex(queue_item, phalcon_logger_item_ce);
phalcon_call_method_p3_noret(queue_item, "__construct", message, type, timestamp);
phalcon_call_method_p4_noret(queue_item, "__construct", *message, level, timestamp, *context);

phalcon_update_property_array_append(this_ptr, SL("_queue"), queue_item TSRMLS_CC);
}
else {
phalcon_call_method_p3_noret(this_ptr, "loginternal", message, type, timestamp);
phalcon_call_method_p4_noret(this_ptr, "loginternal", *message, level, timestamp, *context);
}

PHALCON_MM_RESTORE();
}

RETURN_THIS();
RETURN_THISW();
}
7 changes: 7 additions & 0 deletions ext/logger/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ extern zend_class_entry *phalcon_logger_adapter_ce;

PHALCON_INIT_CLASS(Phalcon_Logger_Adapter);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_logger_adapter_loginternal, 0, 0, 4)
ZEND_ARG_INFO(0, message)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, time)
ZEND_ARG_INFO(0, context)
ZEND_END_ARG_INFO()

#endif /* PHALCON_LOGGER_ADAPTER_H */
15 changes: 5 additions & 10 deletions ext/logger/adapter/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_logger_adapter_file___construct, 0, 0, 1)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_logger_adapter_file_loginternal, 0, 0, 3)
ZEND_ARG_INFO(0, message)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, time)
ZEND_END_ARG_INFO()

static const zend_function_entry phalcon_logger_adapter_file_method_entry[] = {
PHP_ME(Phalcon_Logger_Adapter_File, __construct, arginfo_phalcon_logger_adapter_file___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Logger_Adapter_File, getFormatter, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter_File, logInternal, arginfo_phalcon_logger_adapter_file_loginternal, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter_File, logInternal, arginfo_phalcon_logger_adapter_loginternal, ZEND_ACC_PROTECTED)
PHP_ME(Phalcon_Logger_Adapter_File, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter_File, getPath, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Logger_Adapter_File, __wakeup, NULL, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -171,15 +165,16 @@ PHP_METHOD(Phalcon_Logger_Adapter_File, getFormatter){
* @param string $message
* @param int $type
* @param int $time
* @param array $context
*/
PHP_METHOD(Phalcon_Logger_Adapter_File, logInternal){

zval *message, *type, *time, *file_handler, *formatter;
zval *message, *type, *time, *file_handler, *formatter, *context;
zval *applied_format;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 3, 0, &message, &type, &time);
phalcon_fetch_params(1, 4, 0, &message, &type, &time, &context);

PHALCON_OBS_VAR(file_handler);
phalcon_read_property_this(&file_handler, this_ptr, SL("_fileHandler"), PH_NOISY_CC);
Expand All @@ -192,7 +187,7 @@ PHP_METHOD(Phalcon_Logger_Adapter_File, logInternal){
phalcon_call_method(formatter, this_ptr, "getformatter");

PHALCON_INIT_VAR(applied_format);
phalcon_call_method_p3(applied_format, formatter, "format", message, type, time);
phalcon_call_method_p4(applied_format, formatter, "format", message, type, time, context);
PHALCON_CALL_FUNCTION_NORET("fwrite", file_handler, applied_format);

PHALCON_MM_RESTORE();
Expand Down
Loading