使用日志记录功能查看PHP扩展的执行过程

了解过PHP内核的同学都知道,PHP的一次请求的生命周期

1.启动Apache后,PHP解释程序也随之启动。PHP调用各个扩展的MINIT方法,从而使这些扩展切换到可用状态

2.当一个页面请求发生时,SAPI层将控制权交给PHP层。于是PHP设置了用于回复本次请求所需的环境变量。同时,它还建立一个变量表,用来存放执行过程 中产生的变量名和值。PHP调用各个模块的RINIT方法,即“请求初始化”。RINIT方法可以看作是一个准备过程, 在程序执行之间就会自动启动

3.如同PHP启动一样,PHP的关闭也分两步。一旦页面执行完毕(无论是执行到了文件末尾还是用exit或die函数中止),PHP就会启动清理程序。它会按顺序调用各个模块的RSHUTDOWN方法。 RSHUTDOWN用以清除程序运行时产生的符号表,也就是对每个变量调用unset函数。

4.最后,所有的请求都已处理完毕,SAPI也准备关闭了,PHP开始执行第二步:PHP调用每个扩展的MSHUTDOWN方法,这是各个模块最后一次释放内存的机会

以上是运行在Apache服务中,而Nginx+FastCgi似乎不是这样,这一点我会后期深究,今天主要探讨日志功能,便也后期学习


首先我们按照我的这篇博文(在Linux下编写php扩展)生成一个扩展

然后我们在myext.c文件中添加一个日志函数,然后分别在

PHP_MINIT_FUNCTION,PHP_MSHUTDOWN_FUNCTION,PHP_RINIT_FUNCTION,PHP_RSHUTDOWN_FUNCTION四个函数中添加调用日志的函数

然后编译扩展,重新服务,允许等操作,看看是否有日志

下面我给出一个完整的文件 

  1 /*
  2   +----------------------------------------------------------------------+
  3   | PHP Version 5                                                        |
  4   +----------------------------------------------------------------------+
  5   | Copyright (c) 1997-2016 The PHP Group                                |
  6   +----------------------------------------------------------------------+
  7   | This source file is subject to version 3.01 of the PHP license,      |
  8   | that is bundled with this package in the file LICENSE, and is        |
  9   | available through the world-wide-web at the following url:           |
 10   | http://www.php.net/license/3_01.txt                                  |
 11   | If you did not receive a copy of the PHP license and are unable to   |
 12   | obtain it through the world-wide-web, please send a note to          |
 13   | license@php.net so we can mail you a copy immediately.               |
 14   +----------------------------------------------------------------------+
 15   | Author:                                                              |
 16   +----------------------------------------------------------------------+
 17 */
 18 
 19 /* $Id$ */
 20 
 21 #ifdef HAVE_CONFIG_H
 22 #include "config.h"
 23 #endif
 24 
 25 #include "php.h"
 26 #include "php_ini.h"
 27 #include "ext/standard/info.h"
 28 #include "php_myext.h"
 29 
 30 /* If you declare any globals in php_myext.h uncomment this:
 31 ZEND_DECLARE_MODULE_GLOBALS(myext)
 32 */
 33 
 34 /* True global resources - no need for thread safety here */
 35 static int le_myext;
 36 void save_log();
 37 /* {{{ PHP_INI
 38  */
 39 /* Remove comments and fill if you need to have entries in php.ini
 40 PHP_INI_BEGIN()
 41     STD_PHP_INI_ENTRY("myext.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myext_globals, myext_globals)
 42     STD_PHP_INI_ENTRY("myext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myext_globals, myext_globals)
 43 PHP_INI_END()
 44 */
 45 /* }}} */
 46 
 47 /* Remove the following function when you have successfully modified config.m4
 48    so that your module can be compiled into PHP, it exists only for testing
 49    purposes. */
 50 
 51 /* Every user-visible function in PHP should document itself in the source */
 52 /* {{{ proto string confirm_myext_compiled(string arg)
 53    Return a string to confirm that the module is compiled in */
 54 PHP_FUNCTION(confirm_myext_compiled)
 55 {
 56     char *arg = NULL;
 57     int arg_len, len;
 58     char *strg;
 59 
 60     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
 61         return;
 62     }
 63 
 64     len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "myext", arg);
 65     RETURN_STRINGL(strg, len, 0);
 66 }
 67 
 68 void save_log(char *str)
 69 {
 70     time_t rawtime;
 71     struct tm * timeinfo;
 72     time ( &rawtime );
 73     timeinfo = localtime ( &rawtime );
 74 
 75     char buf[512];
 76     sprintf(buf, "%s", asctime (timeinfo));
 77 
 78     
 79     FILE *fp = fopen("/Users/zongshuai/log/c.log", "a+");
 80     ////这里写一个绝对路径,请改成一下,大家还得注意权限问题,如果发现打印不出内容,检查一下权限
 81     if (fp==0) {
 82         printf("can't open file
");
 83         return;
 84     }
 85 
 86      strcat(str,"
");
 87 
 88     fseek(fp, 0,SEEK_END);
 89     fwrite(buf, strlen(buf) - 1, 1, fp);
 90     fwrite("    ", 4, 1, fp);
 91     fwrite(str, strlen(str) * sizeof(char), 1, fp);
 92     fclose(fp);
 93     return;
 94 }
 95 
 96 PHP_FUNCTION(sum_two_num)
 97 {
 98     long x,y,z;
 99     
100     if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &x,&y) == FAILURE) {
101         RETURN_FALSE;
102     }
103 
104     char str[80] = "EXECED MYFUNCTION";
105     save_log(str);
106 
107     z = x * y;
108 
109     RETURN_LONG(z);
110 }
111 /* }}} */
112 /* The previous line is meant for vim and emacs, so it can correctly fold and 
113    unfold functions in source code. See the corresponding marks just before 
114    function definition, where the functions purpose is also documented. Please 
115    follow this convention for the convenience of others editing your code.
116 */
117 
118 
119 /* {{{ php_myext_init_globals
120  */
121 /* Uncomment this function if you have INI entries
122 static void php_myext_init_globals(zend_myext_globals *myext_globals)
123 {
124     myext_globals->global_value = 0;
125     myext_globals->global_string = NULL;
126 }
127 */
128 /* }}} */
129 
130 /* {{{ PHP_MINIT_FUNCTION
131  */
132 PHP_MINIT_FUNCTION(myext)
133 {
134     /* If you have INI entries, uncomment these lines 
135     REGISTER_INI_ENTRIES();
136     */
137     char str[80] = "EXECED PHP_MINIT_FUNCTION";
138     save_log(str);
139     return SUCCESS;
140 }
141 /* }}} */
142 
143 /* {{{ PHP_MSHUTDOWN_FUNCTION
144  */
145 PHP_MSHUTDOWN_FUNCTION(myext)
146 {
147     /* uncomment this line if you have INI entries
148     UNREGISTER_INI_ENTRIES();
149     */
150     char str[80] = "EXECED PHP_MSHUTDOWN_FUNCTION";
151     save_log(str);
152     return SUCCESS;
153 }
154 /* }}} */
155 
156 /* Remove if there's nothing to do at request start */
157 /* {{{ PHP_RINIT_FUNCTION
158  */
159 PHP_RINIT_FUNCTION(myext)
160 {
161     char str[80] = "EXECED PHP_RINIT_FUNCTION";
162     save_log(str);
163     return SUCCESS;
164 }
165 /* }}} */
166 
167 /* Remove if there's nothing to do at request end */
168 /* {{{ PHP_RSHUTDOWN_FUNCTION
169  */
170 PHP_RSHUTDOWN_FUNCTION(myext)
171 {
172     char str[80] = "EXECED PHP_RSHUTDOWN_FUNCTION";
173     save_log(str);
174     return SUCCESS;
175 }
176 /* }}} */
177 
178 /* {{{ PHP_MINFO_FUNCTION
179  */
180 PHP_MINFO_FUNCTION(myext)
181 {
182     php_info_print_table_start();
183     php_info_print_table_header(2, "myext support", "enabled");
184     php_info_print_table_end();
185 
186     /* Remove comments if you have entries in php.ini
187     DISPLAY_INI_ENTRIES();
188     */
189 }
190 /* }}} */
191 
192 /* {{{ myext_functions[]
193  *
194  * Every user visible function must have an entry in myext_functions[].
195  */
196 const zend_function_entry myext_functions[] = {
197     PHP_FE(confirm_myext_compiled,    NULL)        /* For testing, remove later. */
198     PHP_FE(sum_two_num,    NULL)
199     PHP_FE_END    /* Must be the last line in myext_functions[] */
200 };
201 /* }}} */
202 
203 /* {{{ myext_module_entry
204  */
205 zend_module_entry myext_module_entry = {
206     STANDARD_MODULE_HEADER,
207     "myext",
208     myext_functions,
209     PHP_MINIT(myext),
210     PHP_MSHUTDOWN(myext),
211     PHP_RINIT(myext),        /* Replace with NULL if there's nothing to do at request start */
212     PHP_RSHUTDOWN(myext),    /* Replace with NULL if there's nothing to do at request end */
213     PHP_MINFO(myext),
214     PHP_myext_VERSION,
215     STANDARD_MODULE_PROPERTIES
216 };
217 /* }}} */
218 
219 #ifdef COMPILE_DL_myext
220 ZEND_GET_MODULE(myext)
221 #endif
222 
223 /*
224  * Local variables:
225  * tab- 4
226  * c-basic-offset: 4
227  * End:
228  * vim600: noet sw=4 ts=4 fdm=marker
229  * vim<600: noet sw=4 ts=4
230  */
原文地址:https://www.cnblogs.com/xiaozong/p/5823380.html