PHP extension-C extension for rolling logging

Source: Internet
Author: User
: This article mainly introduces the PHP extension-C extension to implement rolling logging. if you are interested in the PHP Tutorial, please refer to it. Preface

Everything is hard at the beginning. that's right !!

Before developing PHP extensions, I have always felt that PHP extension development is far away from me. although I have some C \ C ++ basics, however, it was still difficult to look at the PHP source code. now it seems that I have not made up my mind to do this. this time, I finally made up my mind to build a php Extension class library for a weekend, finally, the previous log writing class library is encapsulated into a php extension form, which is also an entry to the development of PHP Extensions. here we will share with you the source code: PHP extension for logging in one singleton mode

Development

I developed a PHP extension to record logs. the function is basically the same as the PHP rolling log of the past few days. it is also run in Singleton mode.

Note the following points during development:

Here is only a small part. for details, you can view my source code. I think the best way to learn is to read the source code. if you don't know how to check the source code, you will have a better memory.

Understand the zval structure. zval is the data structure defined in the PHP kernel.
// Defined in Zend/zend_types.h: 55 and Zend/zend. h: Row 3 typedef struct _ zval_struct zval; struct _ zval_struct {/* Variable information */zvalue_value value;/* value */zend_uint refcount; zend_uchar type; /* active type */zend_uchar is_ref;}; typedef union _ zvalue_value {long lval;/* long value */double dval;/* double value */struct {char * val; int len;} str; HashTable * ht;/* hash table value */zend_object_value obj;} zvalue_value; // zval. type indicates the type of the variable. The basic types are as follows: Zend/zend. h: medium # define functions # define IS_LONG1 # define IS_DOUBLE2 # define IS_BOOL3 # define IS_ARRAY4 # define IS_OBJECT5 # define IS_STRING6 // Some common zval functions or macros, defines the value in the Zend/zend_operators.h: 441 row Z_LVAL_P (zval_p) // gets the value of the zval structure pointed to by the zval_p pointer. The value type is LONGZ_STRVAL_P (zval_p)
How to pass parameters to functions
When receiving parameters in PHP extensions, use the zend_parse_parameters function to pass parameters in a similar way: zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & msg, & msg_len ); the second parameter is the list of parameter types to be passed in, which has the following specifications:

Starting from PHP5.3, the zend_parse_paramters _ function adds the following new type descriptors: f-function or array containing php method call info (returned as zend_fcall_info and zend_fcall_info_cache) h-array or HASH_OF (object) (returned as HashTable) L-long, limits out-of-range numbers to LONG_MAX/LONG_MIN (long) z-the actual zval (zval **)
Common functions used when developing a face-to-face Database
Values (mylogs_ce, ZEND_STRL ("level"), ZEND_ACC_PRIVATE TSRMLS_CC); // similar to the above function, set the variable values (mylogs_ce, ZEND_STRL ("LOG_DEBUG") during class initialization "), 0 TSRMLS_CC); // similar to the above function, set the constant zend_read_property (mylogs_ce, instance, ZEND_STRL ("level"), 0 TSRMLS_CC) during initialization ); // read the variable value zend_hash_find (& ce-> constants_table, ZEND_STRS ("LOG_DEBUG") from the instance, (void **) & _ level ); // read the constant zend_update_property_long (mylogs_ce, instance, ZEND_STRL ("level"), level TSRMLS_CC) from mylogs_ce. // The above variable level is read from instance
Note:
  1. Pay attention to the memory release to prevent memory leakage. after the development is complete, I find that there is a memory leakage. you can use valgrind to check the memory leakage and release the memory that is no longer needed, otherwise, an error may be reported because the memory usage is too high during a large number of loop processing.
  2. There is also a look at the example. the YAF source code of laruence can be used for more reference. this has helped me a lot. in the early stages of learning, it was "taking pictures and Tigers ".
View Results

After the development is complete, I wrote a script and tried it. the circular output logs were 10 million times (30 w, debug, msg, and err each 10 times). The following are the environment and results.
Environment:

Centos virtual machine, 64-bit memory 1 GCPU i5-2410M @ 2.30 GHz # This is my computer's
Times PHP code PHP extension
First time 8.83 s 6.13 s
Second 8.60 s 6.14 s
Average 8.72 s 6.14 s

We can see that the speed of PHP extension can be improved by about 1/4. haha, it's good. my code may have a lot of room for optimization. come on ~~

I put all the code on github:

PHP extension for logging in one singleton mode

References

Use C/C ++ to expand your PHP -- Laruence

Go deep into the PHP variable storage structure-Wen Daxia

5/24/2015 3:10:45

The copyright of this article is owned by the author iforever (luluyrt@163.com), without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.

The above introduces the PHP extension-C extension to implement rolling logging, including some content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.