使用日志记录功能查看PHP扩展的执行过程
了解過PHP內(nèi)核的同學(xué)都知道,PHP的一次請求的生命周期
1.啟動(dòng)Apache后,PHP解釋程序也隨之啟動(dòng)。PHP調(diào)用各個(gè)擴(kuò)展的MINIT方法,從而使這些擴(kuò)展切換到可用狀態(tài)
2.當(dāng)一個(gè)頁面請求發(fā)生時(shí),SAPI層將控制權(quán)交給PHP層。于是PHP設(shè)置了用于回復(fù)本次請求所需的環(huán)境變量。同時(shí),它還建立一個(gè)變量表,用來存放執(zhí)行過程 中產(chǎn)生的變量名和值。PHP調(diào)用各個(gè)模塊的RINIT方法,即“請求初始化”。RINIT方法可以看作是一個(gè)準(zhǔn)備過程, 在程序執(zhí)行之間就會自動(dòng)啟動(dòng)
3.如同PHP啟動(dòng)一樣,PHP的關(guān)閉也分兩步。一旦頁面執(zhí)行完畢(無論是執(zhí)行到了文件末尾還是用exit或die函數(shù)中止),PHP就會啟動(dòng)清理程序。它會按順序調(diào)用各個(gè)模塊的RSHUTDOWN方法。 RSHUTDOWN用以清除程序運(yùn)行時(shí)產(chǎn)生的符號表,也就是對每個(gè)變量調(diào)用unset函數(shù)。
4.最后,所有的請求都已處理完畢,SAPI也準(zhǔn)備關(guān)閉了,PHP開始執(zhí)行第二步:PHP調(diào)用每個(gè)擴(kuò)展的MSHUTDOWN方法,這是各個(gè)模塊最后一次釋放內(nèi)存的機(jī)會
以上是運(yùn)行在Apache服務(wù)中,而Nginx+FastCgi似乎不是這樣,這一點(diǎn)我會后期深究,今天主要探討日志功能,便也后期學(xué)習(xí)
?
首先我們按照我的這篇博文(在Linux下編寫php擴(kuò)展)生成一個(gè)擴(kuò)展
然后我們在myext.c文件中添加一個(gè)日志函數(shù),然后分別在
PHP_MINIT_FUNCTION,PHP_MSHUTDOWN_FUNCTION,PHP_RINIT_FUNCTION,PHP_RSHUTDOWN_FUNCTION四個(gè)函數(shù)中添加調(diào)用日志的函數(shù)
然后編譯擴(kuò)展,重新服務(wù),允許等操作,看看是否有日志
?
下面我給出一個(gè)完整的文件?
?
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 ////這里寫一個(gè)絕對路徑,請改成一下,大家還得注意權(quán)限問題,如果發(fā)現(xiàn)打印不出內(nèi)容,檢查一下權(quán)限 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 */?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaozong/p/5823380.html
總結(jié)
以上是生活随笔為你收集整理的使用日志记录功能查看PHP扩展的执行过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十天精通CSS3学习笔记 part2
- 下一篇: HDU1716(全排列)