root/eaccelerator/branches/0.9.5/debug.c

Revision 250, 7.0 kB (checked in by bart, 2 years ago)

Fixed project files for win32 and made the debug version build again

  • Property svn:eol-style set to native
  • Property svn:keywords set to svn:eol-style
Line 
1 /*
2    +----------------------------------------------------------------------+
3    | eAccelerator project                                                 |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2004 - 2006 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    $Id: debug.c 176 2006-03-05 12:18:54Z bart $
26 */
27
28 #include "eaccelerator.h"
29
30 #ifdef HAVE_EACCELERATOR
31
32 #include "debug.h"
33 #include <ctype.h>
34 #include <stdio.h>
35
36 static FILE *F_fp = NULL;
37 static int file_no = 0;
38 long eaccelerator_debug = 0;
39
40 /**
41  * Init the debug system. This must be called before any debug
42  * functions are used.
43  */
44 void ea_debug_init (TSRMLS_D)
45 {
46     F_fp = fopen (EAG (eaccelerator_log_file), "a");
47     if (!F_fp)
48         F_fp = stderr;
49     file_no = fileno(F_fp);
50 }
51
52 /**
53  * Close the debug system.
54  */
55 void ea_debug_shutdown ()
56 {
57     fflush (F_fp);
58     if (F_fp != stderr)
59         fclose (F_fp);
60     F_fp = NULL;
61 }
62
63 /**
64  * Print a log message that will be print when the debug level is
65  * equal to EA_LOG. This function is always called even if ea isn't
66  * compiled with DEBUG and the log level is not equal to EA_LOG.
67  */
68 void ea_debug_log (char *format, ...)
69 {
70     if (eaccelerator_debug & EA_LOG) {
71         char output_buf[512];
72         va_list args;
73
74         va_start (args, format);
75         vsnprintf (output_buf, sizeof (output_buf), format, args);
76         va_end (args);
77
78         if (F_fp != stderr) {
79                 EACCELERATOR_FLOCK(file_no, LOCK_EX);
80         }
81         fputs (output_buf, F_fp);
82         fflush (F_fp);
83         if (F_fp != stderr) {
84                 EACCELERATOR_FLOCK(file_no, LOCK_UN);
85         }
86     }
87 }
88
89 /**
90  * Output an error message to stderr. This message are always printed
91  * no matter what log level is used.
92  */
93 void ea_debug_error (char *format, ...)
94 {
95     char output_buf[512];
96     va_list args;
97
98     va_start (args, format);
99     vsnprintf (output_buf, sizeof (output_buf), format, args);
100     va_end (args);
101
102     fputs (output_buf, stderr);
103     fflush (stderr);
104 }
105
106 /*
107  * All these functions aren't compiled when eA isn't compiled with DEBUG. They
108  * are replaced with function with no body, so it's optimized away by the compiler.
109  * Even if the debug level is ok.
110  */
111
112 /**
113  * Print a debug message
114  */
115 void ea_debug_printf (long debug_level, char *format, ...)
116 {
117     if (eaccelerator_debug & debug_level) {
118         char output_buf[512];
119         va_list args;
120
121         va_start (args, format);
122         vsnprintf (output_buf, sizeof (output_buf), format, args);
123         va_end (args);
124        
125         if (F_fp != stderr) {
126                 EACCELERATOR_FLOCK(file_no, LOCK_EX);
127         }
128         fputs (output_buf, F_fp);
129         fflush (F_fp);
130         if (F_fp != stderr) {
131                 EACCELERATOR_FLOCK(file_no, LOCK_UN);
132         }
133     }
134 }
135
136 /**
137  * Put a debug message
138  */
139 void ea_debug_put (long debug_level, char *message)
140 {
141     if (debug_level & eaccelerator_debug) {
142         if (F_fp != stderr) {
143                 EACCELERATOR_FLOCK(file_no, LOCK_EX);
144         }
145         fputs (message, F_fp);
146         fflush (F_fp);
147         if (F_fp != stderr) {
148                 EACCELERATOR_FLOCK(file_no, LOCK_UN);
149         }
150     }
151 }
152
153 /**
154  * Print a binary message
155  */
156 void ea_debug_binary_print (long debug_level, char *p, int len)
157 {
158     if (eaccelerator_debug & debug_level) {
159         if (F_fp != stderr) {
160                 EACCELERATOR_FLOCK(file_no, LOCK_EX);
161         }
162         while (len--) {
163             fputc (*p++, F_fp);
164         }
165         fputc ('\n', F_fp);
166         fflush (F_fp);
167         if (F_fp != stderr) {
168                 EACCELERATOR_FLOCK(file_no, LOCK_UN);
169         }
170     }
171 }
172
173 /**
174  * Log a hashkey
175  */
176 void ea_debug_log_hashkeys (char *p, HashTable * ht)
177 {
178     if (eaccelerator_debug & EA_LOG_HASHKEYS) {
179         Bucket *b;
180         int i = 0;
181
182         b = ht->pListHead;
183
184         if (F_fp != stderr) {
185                 EACCELERATOR_FLOCK(file_no, LOCK_EX);
186         }
187         fputs(p, F_fp);
188         fflush(F_fp);
189         while (b) {
190             fprintf (F_fp, "[%d] ", i);
191             ea_debug_binary_print (EA_LOG_HASHKEYS, b->arKey, b->nKeyLength);
192
193             b = b->pListNext;
194             i++;
195         }
196         if (F_fp != stderr) {
197                 EACCELERATOR_FLOCK(file_no, LOCK_UN);
198         }
199     }
200 }
201
202 /**
203  * Pad the message with the current pad level.
204  */
205 void ea_debug_pad (long debug_level TSRMLS_DC)
206 {
207 #ifdef DEBUG /* This ifdef is still req'd because xpad is N/A in a non-debug compile */
208     if (eaccelerator_debug & debug_level) {
209                 int i;
210         if (F_fp != stderr) {
211                 EACCELERATOR_FLOCK(file_no, LOCK_EX);
212         }
213         i = EAG (xpad);
214         while (i-- > 0) {
215             fputc ('\t', F_fp);
216         }
217         if (F_fp != stderr) {
218                 EACCELERATOR_FLOCK(file_no, LOCK_UN);
219         }
220     }
221 #endif
222 }
223
224 void ea_debug_start_time (struct timeval *tvstart)
225 {
226     gettimeofday (tvstart, NULL);
227 }
228
229 long ea_debug_elapsed_time (struct timeval *tvstart)
230 {
231     struct timeval tvend;
232     int sec, usec;
233     gettimeofday (&tvend, NULL);
234     sec = tvend.tv_sec - tvstart->tv_sec;
235     usec = tvend.tv_usec - tvstart->tv_usec;
236     return sec * 1000000 + usec;
237 }
238
239 /*
240  * This dumps a HashTable to debug output. Taken from zend_hash.c and slightly adapted.
241  */
242 void ea_debug_hash_display(HashTable * ht)
243 {
244         Bucket *p;
245         uint i;
246
247         fprintf(F_fp, "ht->nTableSize: %d\n", ht->nTableSize);
248         fprintf(F_fp, "ht->nNumOfElements: %d\n", ht->nNumOfElements);
249
250         for (i = 0; i < ht->nTableSize; i++) {
251                 p = ht->arBuckets[i];
252                 while (p != NULL) {
253                         fprintf(F_fp, "%s <==> 0x%lX\n", p->arKey, p->h);
254                         p = p->pNext;
255                 }
256         }
257
258         p = ht->pListTail;
259         while (p != NULL) {
260                 fprintf(F_fp, "%s <==> 0x%lX\n", p->arKey, p->h);
261                 p = p->pListLast;
262         }
263         fflush(F_fp);
264 }
265
266 #endif /* #ifdef HAVE_EACCELERATOR */
Note: See TracBrowser for help on using the browser.