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 144
 #	ifdef __SUNPRO_C
145 145
 #		define LOG_(level, prefix, fmt, ...) \
146 146
 			do { \
147
-				if (cfg_get(core, core_cfg, debug) >= (level) && \
148
-						DPRINT_NON_CRIT) { \
147
+				if (unlikely(cfg_get(core, core_cfg, debug) >= (level) && \
148
+						DPRINT_NON_CRIT)) { \
149 149
 					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__); \
150
+					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
151
+						if (unlikely(log_stderr)) { \
152
+							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
153
+									process_no, my_pid(), \
154
+									LOG_LEVEL2NAME(level), (prefix), \
155
+									__VA_ARGS__); \
156
+						} else { \
157
+							syslog(LOG2SYSLOG_LEVEL(level) | \
158
+									cfg_get(core, core_cfg, log_facility),\
159
+									"%s: %s" fmt, LOG_LEVEL2NAME(level),\
160
+									(prefix), __VA_ARGS__); \
161
+						} \
155 162
 					} 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__); \
163
+						if (log_stderr) { \
164
+							fprintf(stderr, "%2d(%d) %s" fmt, \
165
+									process_no, my_pid(), \
166
+									(prefix),  __VA_ARGS__); \
167
+						} else { \
168
+							if ((level)<L_ALERT) \
169
+								syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
170
+										cfg_get(core, core_cfg, log_facility),\
171
+										"%s" fmt, (prefix), __VA_ARGS__); \
172
+							else \
173
+								syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
174
+										cfg_get(core, core_cfg, log_facility),\
175
+										"%s" fmt, (prefix), __VA_ARGS__); \
176
+						} \
160 177
 					} \
161 178
 					DPRINT_CRIT_EXIT; \
162 179
 				} \
... ...
@@ -164,22 +182,38 @@ int log_facility_fixup(void *handle, str *name, void **val);
164 164
 			
165 165
 #		define LOG(level, fmt, ...)  LOG_((level), LOC_INFO, fmt, __VA_ARGS__)
166 166
 
167
-#	else
167
+#	else /* ! __SUNPRO_C */
168 168
 #		define LOG_(level, prefix, fmt, args...) \
169 169
 			do { \
170 170
 				if (cfg_get(core, core_cfg, debug) >= (level) && \
171 171
 						DPRINT_NON_CRIT) { \
172 172
 					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) |\
173
+					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
174
+						if (unlikely(log_stderr)) { \
175
+							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
176
+									process_no, my_pid(), \
177
+									LOG_LEVEL2NAME(level),(prefix), ## args);\
178
+						} else { \
179
+							syslog(LOG2SYSLOG_LEVEL(level) |\
180 180
 									cfg_get(core, core_cfg, log_facility), \
181
-						 		"%s: %s" fmt, LOG_LEVEL2NAME(level),\
182
-								(prefix), ## args); \
181
+									"%s: %s" fmt, LOG_LEVEL2NAME(level),\
182
+									(prefix), ## args); \
183
+						} \
184
+					} else { \
185
+						if (log_stderr) { \
186
+							fprintf(stderr, "%2d(%d) %s" fmt, \
187
+										process_no, my_pid(), \
188
+										(prefix), ## args); \
189
+						} else { \
190
+							if ((level)<L_ALERT) \
191
+								syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
192
+										cfg_get(core, core_cfg, log_facility),\
193
+										"%s" fmt, (prefix), ## args); \
194
+							else \
195
+								syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
196
+										cfg_get(core, core_cfg, log_facility),\
197
+										"%s" fmt, (prefix), ## args); \
198
+						} \
183 199
 					} \
184 200
 					DPRINT_CRIT_EXIT; \
185 201
 				} \