root/eaccelerator/branches/0.9.4/debug.c

Revision 217, 6.8 kB (checked in by bart, 2 years ago)

Removed some includes in debug.c and added on in mm.c

  • 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 static FILE *F_fp = NULL;
40 static int file_no = 0;
41 long eaccelerator_debug = 0;
42
43 /**
44  * Init the debug system. This must be called before any debug
45  * functions are used.
46  */
47 void ea_debug_init (TSRMLS_D)
48 {
49     /* register ini entries */
50     REGISTER_MAIN_LONG_CONSTANT ("EA_LOG", EA_LOG, CONST_PERSISTENT | CONST_CS);
51     REGISTER_MAIN_LONG_CONSTANT ("EA_DEBUG", EA_DEBUG, CONST_PERSISTENT | CONST_CS);
52     REGISTER_MAIN_LONG_CONSTANT ("EA_PROFILE_OPCODES", EA_PROFILE_OPCODES, CONST_PERSISTENT | CONST_CS);
53     REGISTER_MAIN_LONG_CONSTANT ("EA_TEST_PERFORMANCE", EA_TEST_PERFORMANCE, CONST_PERSISTENT | CONST_CS);
54     REGISTER_MAIN_LONG_CONSTANT ("EA_LOG_HASHKEYS", EA_LOG_HASHKEYS, CONST_PERSISTENT | CONST_CS);
55
56     F_fp = fopen (EAG (eaccelerator_log_file), "a");
57     if (!F_fp)
58         F_fp = stderr;
59     file_no = fileno(F_fp);
60 }
61
62 /**
63  * Close the debug system.
64  */
65 void ea_debug_shutdown ()
66 {
67     fflush (F_fp);
68     if (F_fp != stderr)
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         if (F_fp != stderr) {
89                         EACCELERATOR_FLOCK(file_no, LOCK_EX);
90                 }
91                 fputs (output_buf, F_fp);
92                 fflush (F_fp);
93                 if (F_fp != stderr) {
94                         EACCELERATOR_FLOCK(file_no, LOCK_UN);
95                 }
96
97     }
98 }
99
100 /**
101  * Output an error message to stderr. This message are always printed
102  * no matter what log level is used.
103  */
104 void ea_debug_error (char *format, ...)
105 {
106     char output_buf[512];
107     va_list args;
108
109     va_start (args, format);
110     vsnprintf (output_buf, sizeof (output_buf), format, args);
111     va_end (args);
112
113 #ifdef ZEND_WIN32
114     OutputDebugString (output_buf);
115 #else
116     fputs (output_buf, stderr);
117     fflush (stderr);
118 #endif
119 }
120
121 /*
122  * All these functions aren't compiled when eA isn't compiled with DEBUG. They
123  * are replaced with function with no body, so it's optimized away by the compiler.
124  * Even if the debug level is ok.
125  */
126
127 /**
128  * Print a debug message
129  */
130 #ifdef DEBUG
131 void ea_debug_printf (long debug_level, char *format, ...)
132 {
133     if (eaccelerator_debug & debug_level) {
134         char output_buf[512];
135         va_list args;
136
137         va_start (args, format);
138         vsnprintf (output_buf, sizeof (output_buf), format, args);
139         va_end (args);
140        
141         ea_debug_lock();
142         fputs (output_buf, F_fp);
143         fflush (F_fp);
144         ea_debug_unlock();
145     }
146 }
147 #else
148 void ea_debug_printf (long debug_level, char *format, ...)
149 {
150 }
151 #endif
152
153 /**
154  * Put a debug message
155  */
156 #ifdef DEBUG
157 void ea_debug_put (long debug_level, char *message)
158 {
159     if (debug_level & eaccelerator_debug) {
160         ea_debug_lock();
161         fputs (message, F_fp);
162         fflush (F_fp);
163         ea_debug_unlock();
164     }
165 }
166 #else
167 void ea_debug_put (long debug_level, char *message)
168 {
169 }
170 #endif
171
172 /**
173  * Print a binary message
174  */
175 #ifdef DEBUG
176 void ea_debug_binary_print (long debug_level, char *p, int len)
177 {
178     if (eaccelerator_debug & debug_level) {
179         ea_debug_lock();
180         while (len--) {
181             fputc (*p++, F_fp);
182         }
183         fputc ('\n', F_fp);
184         fflush (F_fp);
185         ea_debug_unlock();
186     }
187 }
188 #else
189 void ea_debug_binary_print (long debug_level, char *p, int len)
190 {
191 }
192 #endif
193
194 /**
195  * Log a hashkey
196  */
197 #ifdef DEBUG
198 void ea_debug_log_hashkeys (char *p, HashTable * ht)
199 {
200     if (eaccelerator_debug & EA_LOG_HASHKEYS) {
201         Bucket *b;
202         int i = 0;
203
204         b = ht->pListHead;
205
206         ea_debug_lock();
207         fputs(p, F_fp);
208         fflush(F_fp);
209         ea_debug_unlock();
210         while (b) {
211             fprintf (F_fp, "[%d] ", i);
212             ea_debug_binary_print (EA_LOG_HASHKEYS, b->arKey, b->nKeyLength);
213
214             b = b->pListNext;
215             i++;
216         }
217     }
218 }
219 #else
220 void ea_debug_log_hashkeys (char *p, HashTable * ht)
221 {
222 }
223 #endif
224
225 /**
226  * Pad the message with the current pad level.
227  */
228 #ifdef DEBUG
229 void ea_debug_pad (long debug_level TSRMLS_DC)
230 {
231     if (eaccelerator_debug & debug_level) {
232         ea_debug_lock();
233         int i = EAG (xpad);
234         while (i-- > 0) {
235             fputc ('\t', F_fp);
236         }
237         ea_debug_unlock();
238     }
239 }
240 #else
241 void ea_debug_pad (long debug_level TSRMLS_DC)
242 {
243 }
244 #endif
245
246 void ea_debug_start_time (struct timeval *tvstart)
247 {
248     gettimeofday (tvstart, NULL);
249 }
250
251 long ea_debug_elapsed_time (struct timeval *tvstart)
252 {
253     struct timeval tvend;
254     int sec, usec;
255     gettimeofday (&tvend, NULL);
256     sec = tvend.tv_sec - tvstart->tv_sec;
257     usec = tvend.tv_usec - tvstart->tv_usec;
258     return sec * 1000000 + usec;
259 }
260
261 #endif /* #ifdef HAVE_EACCELERATOR */
Note: See TracBrowser for help on using the browser.