Browse code

log/dbg: level is not limited anymore

The recent changes to LOG() added a new limitation: the log level
was restricted to one of the L_ macros. Using another level (e.g.
L_DBG+1) would trigger an assert(). Now any level is allowed. If
the level is not among the defined range (L_ALERT - L_DBG), the
prefix will be skipped (e.g. "DBG") and the syslog level will be
set to either the L_ALERT or the L_DBG one. This fixes problems
with log() from the script, or when using a very high debug level
and something like memdbg=7.

Andrei Pelinescu-Onciul authored on 26/02/2009 23:14:12
Showing 2 changed files
... ...
@@ -2207,7 +2207,8 @@ cmd:
2207 2207
 	| RETURN NUMBER			{$$=mk_action(DROP_T, 2, NUMBER_ST, (void*)$2, NUMBER_ST, (void*)RETURN_R_F);}
2208 2208
 	| RETURN RETCODE		{$$=mk_action(DROP_T, 2, RETCODE_ST, 0, NUMBER_ST, (void*)RETURN_R_F);}
2209 2209
 	| BREAK				{$$=mk_action(DROP_T, 2, NUMBER_ST, 0, NUMBER_ST, (void*)RETURN_R_F); }
2210
-	| LOG_TOK LPAREN STRING RPAREN	{$$=mk_action(LOG_T, 2, NUMBER_ST, (void*)4, STRING_ST, $3); }
2210
+	| LOG_TOK LPAREN STRING RPAREN	{$$=mk_action(LOG_T, 2, NUMBER_ST,
2211
+										(void*)(L_DBG+1), STRING_ST, $3); }
2211 2212
 	| LOG_TOK LPAREN NUMBER COMMA STRING RPAREN	{$$=mk_action(LOG_T, 2, NUMBER_ST, (void*)$3, STRING_ST, $5); }
2212 2213
 	| LOG_TOK error 		{ $$=0; yyerror("missing '(' or ')' ?"); }
2213 2214
 	| LOG_TOK LPAREN error RPAREN	{ $$=0; yyerror("bad log argument"); }
... ...
@@ -32,6 +32,7 @@
32 32
 #include <syslog.h>
33 33
 #include <stdio.h> /* stderr, fprintf() */
34 34
 
35
+#include "compiler_opt.h"
35 36
 #include "cfg_core.h"
36 37
 
37 38
 
... ...
@@ -144,19 +145,36 @@ int log_facility_fixup(void *handle, str *name, void **val);
144 145
 #	ifdef __SUNPRO_C
145 146
 #		define LOG_(level, prefix, fmt, ...) \
146 147
 			do { \
147
-				if (cfg_get(core, core_cfg, debug) >= (level) && \
148
-						DPRINT_NON_CRIT) { \
148
+				if (unlikely(cfg_get(core, core_cfg, debug) >= (level) && \
149
+						DPRINT_NON_CRIT)) { \
149 150
 					DPRINT_CRIT_ENTER; \
150
-					assert(((level) >= L_ALERT) && ((level) <= L_DBG)); \
151
-					if (log_stderr) { \
152
-						fprintf(stderr, "%2d(%d) %s: %s" fmt, \
153
-								process_no, my_pid(), LOG_LEVEL2NAME(level),\
154
-								(prefix), __VA_ARGS__); \
151
+					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
152
+						if (unlikely(log_stderr)) { \
153
+							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
154
+									process_no, my_pid(), \
155
+									LOG_LEVEL2NAME(level), (prefix), \
156
+									__VA_ARGS__); \
157
+						} else { \
158
+							syslog(LOG2SYSLOG_LEVEL(level) | \
159
+									cfg_get(core, core_cfg, log_facility),\
160
+									"%s: %s" fmt, LOG_LEVEL2NAME(level),\
161
+									(prefix), __VA_ARGS__); \
162
+						} \
155 163
 					} else { \
156
-						syslog(LOG2SYSLOG_LEVEL(level) | \
157
-									cfg_get(core, core_cfg, log_facility), \
158
-								"%s: %s" fmt, LOG_LEVEL2NAME(level),\
159
-								(prefix), __VA_ARGS__); \
164
+						if (log_stderr) { \
165
+							fprintf(stderr, "%2d(%d) %s" fmt, \
166
+									process_no, my_pid(), \
167
+									(prefix),  __VA_ARGS__); \
168
+						} else { \
169
+							if ((level)<L_ALERT) \
170
+								syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
171
+										cfg_get(core, core_cfg, log_facility),\
172
+										"%s" fmt, (prefix), __VA_ARGS__); \
173
+							else \
174
+								syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
175
+										cfg_get(core, core_cfg, log_facility),\
176
+										"%s" fmt, (prefix), __VA_ARGS__); \
177
+						} \
160 178
 					} \
161 179
 					DPRINT_CRIT_EXIT; \
162 180
 				} \
... ...
@@ -164,22 +182,38 @@ int log_facility_fixup(void *handle, str *name, void **val);
164 182
 			
165 183
 #		define LOG(level, fmt, ...)  LOG_((level), LOC_INFO, fmt, __VA_ARGS__)
166 184
 
167
-#	else
185
+#	else /* ! __SUNPRO_C */
168 186
 #		define LOG_(level, prefix, fmt, args...) \
169 187
 			do { \
170 188
 				if (cfg_get(core, core_cfg, debug) >= (level) && \
171 189
 						DPRINT_NON_CRIT) { \
172 190
 					DPRINT_CRIT_ENTER; \
173
-					assert(((level) >= L_ALERT) && ((level) <= L_DBG)); \
174
-					if (log_stderr) { \
175
-						fprintf(stderr, "%2d(%d) %s: %s" fmt, \
176
-								process_no, my_pid(), LOG_LEVEL2NAME(level),\
177
-								(prefix), ## args); \
178
-					} else { \
179
-						syslog(LOG2SYSLOG_LEVEL(level) |\
191
+					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
192
+						if (unlikely(log_stderr)) { \
193
+							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
194
+									process_no, my_pid(), \
195
+									LOG_LEVEL2NAME(level),(prefix), ## args);\
196
+						} else { \
197
+							syslog(LOG2SYSLOG_LEVEL(level) |\
180 198
 									cfg_get(core, core_cfg, log_facility), \
181
-						 		"%s: %s" fmt, LOG_LEVEL2NAME(level),\
182
-								(prefix), ## args); \
199
+									"%s: %s" fmt, LOG_LEVEL2NAME(level),\
200
+									(prefix), ## args); \
201
+						} \
202
+					} else { \
203
+						if (log_stderr) { \
204
+							fprintf(stderr, "%2d(%d) %s" fmt, \
205
+										process_no, my_pid(), \
206
+										(prefix), ## args); \
207
+						} else { \
208
+							if ((level)<L_ALERT) \
209
+								syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
210
+										cfg_get(core, core_cfg, log_facility),\
211
+										"%s" fmt, (prefix), ## args); \
212
+							else \
213
+								syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
214
+										cfg_get(core, core_cfg, log_facility),\
215
+										"%s" fmt, (prefix), ## args); \
216
+						} \
183 217
 					} \
184 218
 					DPRINT_CRIT_EXIT; \
185 219
 				} \