使用日志記錄功能查看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\n");
 83         return;
 84     }
 85 
 86      strcat(str,"\r\n");
 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-width: 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  */

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM