root/eaccelerator/tags/0.9.4-rc1/debug.c

Revision 123, 6.5 kB (checked in by zoeloelip, 3 years ago)

* Renamed some macros from MMC (mmcache) to EA (eg MMCG is now EAG)
* Added eA file header to mm.c, mm.h and x86-spinlocks.h
* Readded a hack to the loader to prevent zend2 segfaults

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2    +----------------------------------------------------------------------+
3    | eAccelerator project                                                 |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2004 - 2005 eAccelerator                               |
6    | http://eaccelerator.net                                              |
7    +----------------------------------------------------------------------+
8    | This program is free software; you can redistribute it and/or        |
9    | modify it under the terms of the GNU General Public License          |
10    | as published by the Free Software Foundation; either version 2       |
11    | of the License, or (at your option) any later version.               |
12    |                                                                      |
13    | This program is distributed in the hope that it will be useful,      |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
16    | GNU General Public License for more details.                         |
17    |                                                                      |
18    | You should have received a copy of the GNU General Public License    |
19    | along with this program; if not, write to the Free Software          |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston,               |
21    | MA  02111-1307, USA.                                                 |
22    |                                                                      |
23    | A copy is availble at http://www.gnu.org/copyleft/gpl.txt            |
24    +----------------------------------------------------------------------+
25    | Author(s): Dmitry Stogov <dstogov@users.sourceforge.net>             |
26    |            Bart Vanbrabant <zoeloelip@users.sourceforge.net>         |
27    +----------------------------------------------------------------------+
28    $Id$
29 */
30
31 #include "eaccelerator.h"
32
33 #ifdef HAVE_EACCELERATOR
34
35 #include "debug.h"
36 #include <ctype.h>
37 #include <stdio.h>
38
39 FILE *F_fp = NULL;
40 long eaccelerator_debug = 0;
41
42 /**
43  * Init the debug system. This must be called before any debug
44  * functions are used.
45  */
46 void ea_debug_init (TSRMLS_D)
47 {
48     /* register ini entries */
49     REGISTER_MAIN_LONG_CONSTANT ("EA_LOG", EA_LOG, CONST_PERSISTENT | CONST_CS);
50     REGISTER_MAIN_LONG_CONSTANT ("EA_DEBUG", EA_DEBUG, CONST_PERSISTENT | CONST_CS);
51     REGISTER_MAIN_LONG_CONSTANT ("EA_PROFILE_OPCODES", EA_PROFILE_OPCODES,
52                             CONST_PERSISTENT | CONST_CS);
53     REGISTER_MAIN_LONG_CONSTANT ("EA_TEST_PERFORMANCE", EA_TEST_PERFORMANCE,
54                             CONST_PERSISTENT | CONST_CS);
55     REGISTER_MAIN_LONG_CONSTANT ("EA_LOG_HASHKEYS", EA_LOG_HASHKEYS,
56                             CONST_PERSISTENT | CONST_CS);
57
58     F_fp = fopen (EAG (eaccelerator_log_file), "a");
59     if (!F_fp)
60         F_fp = stderr;
61 }
62
63 /**
64  * Close the debug system.
65  */
66 void ea_debug_shutdown ()
67 {
68     fflush (F_fp);
69     fclose (F_fp);
70     F_fp = NULL;
71 }
72
73 /**
74  * Print a log message that will be print when the debug level is
75  * equal to EA_LOG. This function is always called even if ea isn't
76  * compiled with DEBUG and the log level is not equal to EA_LOG.
77  */
78 void ea_debug_log (char *format, ...)
79 {
80     if (eaccelerator_debug & EA_LOG) {
81         char output_buf[512];
82         va_list args;
83
84         va_start (args, format);
85         vsnprintf (output_buf, sizeof (output_buf), format, args);
86         va_end (args);
87
88 #ifdef ZEND_WIN32
89         OutputDebugString (output_buf);
90 #else
91         fputs (output_buf, F_fp);
92         fflush (F_fp);
93 #endif
94     }
95 }
96
97 /**
98  * Output an error message to stderr. This message are always printed
99  * no matter what log level is used.
100  */
101 void ea_debug_error (char *format, ...)
102 {
103     char output_buf[512];
104     va_list args;
105
106     va_start (args, format);
107     vsnprintf (output_buf, sizeof (output_buf), format, args);
108     va_end (args);
109
110 #ifdef ZEND_WIN32
111     OutputDebugString (output_buf);
112 #else
113     fputs (output_buf, stderr);
114     fflush (stderr);
115 #endif
116 }
117
118 /*
119  * All these functions aren't compiled when eA isn't compiled with DEBUG. They
120  * are replaced with function with no body, so it's optimized away by the compiler.
121  * Even if the debug level is ok.
122  */
123
124 /**
125  * Print a debug message
126  */
127 #ifdef DEBUG
128 void ea_debug_printf (long debug_level, char *format, ...)
129 {
130     if (eaccelerator_debug & debug_level) {
131         char output_buf[512];
132         va_list args;
133
134         va_start (args, format);
135         vsnprintf (output_buf, sizeof (output_buf), format, args);
136         va_end (args);
137
138         fputs (output_buf, F_fp);
139         fflush (F_fp);
140     }
141 }
142 #else
143 void ea_debug_printf (long debug_level, char *format, ...)
144 {
145 }
146 #endif
147
148 /**
149  * Put a debug message
150  */
151 #ifdef DEBUG
152 void ea_debug_put (long debug_level, char *message)
153 {
154     if (debug_level & eaccelerator_debug) {
155         fputs (message, F_fp);
156         fflush (F_fp);
157     }
158 }
159 #else
160 void ea_debug_put (long debug_level, char *message)
161 {
162 }
163 #endif
164
165 /**
166  * Print a binary message
167  */
168 #ifdef DEBUG
169 void ea_debug_binary_print (long debug_level, char *p, int len)
170 {
171     if (eaccelerator_debug & debug_level) {
172         while (len--) {
173             fputc (*p++, F_fp);
174         }
175         fputc ('\n', F_fp);
176         fflush (F_fp);
177     }
178 }
179 #else
180 void ea_debug_binary_print (long debug_level, char *p, int len)
181 {
182 }
183 #endif
184
185 /**
186  * Log a hashkey
187  */
188 #ifdef DEBUG
189 void ea_debug_log_hashkeys (char *p, HashTable * ht)
190 {
191     if (eaccelerator_debug & EA_LOG_HASHKEYS) {
192         Bucket *b;
193         int i = 0;
194
195         b = ht->pListHead;
196
197         fputs (p, F_fp);
198         while (b) {
199             fprintf (F_fp, "[%d] ", i);
200             ea_debug_binary_print (EA_LOG_HASHKEYS, b->arKey, b->nKeyLength);
201
202             b = b->pListNext;
203             i++;
204         }
205         fflush (F_fp);
206     }
207 }
208 #else
209 void ea_debug_log_hashkeys (char *p, HashTable * ht)
210 {
211 }
212 #endif
213
214 /**
215  * Pad the message with the current pad level.
216  */
217 #ifdef DEBUG
218 void ea_debug_pad (long debug_level TSRMLS_DC)
219 {
220     if (eaccelerator_debug & debug_level) {
221         int i = EAG (xpad);
222         while (i-- > 0) {
223             fputc ('\t', F_fp);
224         }
225     }
226 }
227 #else
228 void ea_debug_pad (long debug_level TSRMLS_DC)
229 {
230 }
231 #endif
232
233 void ea_debug_start_time (struct timeval *tvstart)
234 {
235     gettimeofday (tvstart, NULL);
236 }
237
238 long ea_debug_elapsed_time (struct timeval *tvstart)
239 {
240     struct timeval tvend;
241     int sec, usec;
242     gettimeofday (&tvend, NULL);
243     sec = tvend.tv_sec - tvstart->tv_sec;
244     usec = tvend.tv_usec - tvstart->tv_usec;
245     return sec * 1000000 + usec;
246 }
247
248 #endif /* #ifdef HAVE_EACCELERATOR */
Note: See TracBrowser for help on using the browser.