Browse code

* logging API updated (see doc/logging-api.txt for details)

- LOG(LEVEL, FMT, ARGS...) and the short macro corresponding to
LEVEL level made eqvivalent (eg. LOG(L_DBG, FMT, ARGS...) and
DBG(FMT, ARGS...) prints always the same message)

- changed the format of log messages produced by the macros
to include the log level, module name, filename, line (if applicable)

- added new, internal LOG_(LEVEL, PREFIX, FORMAT, ARGS...) macro

- removed DPrint() and DEBUG() macros, L_DEFAULT log level and dprint()
function

!!!
!!! IMPORTANT! READ ME!
!!!

These changes (mainly the first two) require reformating of the most log
messages in SER core and module source files. This step can be done
automatically by running "scripts/logging/fix-logs-all" script BUT it
was NOT originally performed because it would have generated too many
changes in CVS which was discouraged by Andrei. Instead, the developers
are expected to run it when ready.

Ondrej Martinek authored on 06/01/2009 17:14:12
Showing 10 changed files
... ...
@@ -29,6 +29,8 @@ override static_modules_path=
29 29
 # should be set in Makefile of apart module
30 30
 # INCLUDES += -I$(COREPATH)
31 31
 
32
+DEFS += -DMOD_NAME='"$(MOD_NAME)"'
33
+
32 34
 ifneq ($(makefile_defs_included),1)
33 35
 $(error "the local makefile does not include Makefile.defs!")
34 36
 endif
... ...
@@ -44,7 +44,7 @@
44 44
  *  2006-07-27  dns cache and dns based send address failover support (andrei)
45 45
  *  2006-12-06  on popular request last_retcode set also by module functions
46 46
  *              (andrei)
47
- *  2007-06-14  run_actions & do_action need a ctx or handle now, no more 
47
+ *  2007-06-14  run_actions & do_action need a ctx or handle now, no more
48 48
  *               static vars (andrei)
49 49
  *  2008-12-17  added UDP_MTU_TRY_PROTO_T (andrei)
50 50
  */
... ...
@@ -221,7 +221,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
221 221
 #endif
222 222
 				}
223 223
 
224
-#ifdef HONOR_MADDR				
224
+#ifdef HONOR_MADDR
225 225
 				if (u->maddr_val.s && u->maddr_val.len)
226 226
 					dst_host=&u->maddr_val;
227 227
 				else
... ...
@@ -303,7 +303,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
303 303
 				ret=E_BUG;
304 304
 				break;
305 305
 			}
306
-			LOG(a->val[0].u.number, "%s", a->val[1].u.string);
306
+			LOG_(a->val[0].u.number, "<script>: ", "%s", a->val[1].u.string);
307 307
 			ret=1;
308 308
 			break;
309 309
 
... ...
@@ -707,11 +707,11 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
707 707
 					ret=1;  /*default is continue */
708 708
 					if (v>0) {
709 709
 						if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
710
-							ret=run_actions(h, 
710
+							ret=run_actions(h,
711 711
 										(struct action*)a->val[1].u.data, msg);
712 712
 						}
713 713
 					}else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
714
-							ret=run_actions(h, 
714
+							ret=run_actions(h,
715 715
 										(struct action*)a->val[2].u.data, msg);
716 716
 					}
717 717
 				}
... ...
@@ -794,8 +794,8 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
794 794
 			ret=1; /* continue processing */
795 795
 			break;
796 796
 
797
-	        case ADD_T:
798
-	        case ASSIGN_T:
797
+		case ADD_T:
798
+		case ASSIGN_T:
799 799
 
800 800
 			/* If the left attr was specified without indexing brackets delete
801 801
 			 * existing AVPs before adding new ones
... ...
@@ -992,6 +992,3 @@ error:
992 992
 	h->rec_lev--;
993 993
 	return ret;
994 994
 }
995
-
996
-
997
-
... ...
@@ -46,7 +46,7 @@
46 46
 #include "cfg_core.h"
47 47
 
48 48
 struct cfg_group_core default_core_cfg = {
49
-	L_DEFAULT, /*  print only msg. < L_WARN */
49
+	L_WARN, 	/*  print only msg. < L_WARN */
50 50
 	LOG_DAEMON,	/* log_facility -- see syslog(3) */
51 51
 #ifdef USE_DST_BLACKLIST
52 52
 	/* blacklist */
... ...
@@ -284,13 +284,13 @@ void fix_dns_flags(str *name)
284 284
 #ifdef DNS_SRV_LB
285 285
 		dns_flags|=DNS_SRV_RR_LB;
286 286
 #else
287
-		LOG(L_WARN, "WARING: fix_dns_flags: SRV loadbalaning is set, but"
287
+		LOG(L_WARN, "WARNING: fix_dns_flags: SRV loadbalaning is set, but"
288 288
 					" support for it is not compiled -- ignoring\n");
289 289
 #endif
290 290
 	}
291 291
 	if (cfg_get(core, core_cfg, dns_try_naptr)) {
292 292
 #ifndef USE_NAPTR
293
-	LOG(L_WARN, "WARING: fix_dns_flags: NAPTR support is enabled, but"
293
+	LOG(L_WARN, "WARNING: fix_dns_flags: NAPTR support is enabled, but"
294 294
 				" support for it is not compiled -- ignoring\n");
295 295
 #endif
296 296
 		dns_flags|=DNS_TRY_NAPTR;
297 297
new file mode 100644
... ...
@@ -0,0 +1,134 @@
0
+  _                      _                  _    ____ ___ 
1
+ | |    ___   __ _  __ _(_)_ __   __ _     / \  |  _ \_ _|
2
+ | |   / _ \ / _` |/ _` | | '_ \ / _` |   / _ \ | |_) | | 
3
+ | |__| (_) | (_| | (_| | | | | | (_| |  / ___ \|  __/| | 
4
+ |_____\___/ \__, |\__, |_|_| |_|\__, | /_/   \_\_|  |___|
5
+            |___/ |___/         |___/                    
6
+                         Ondrej Martinek <ondra@iptel.org>
7
+                                              January 2009
8
+
9
+This document contains the short description of the logging API in SER
10
+for developers.
11
+
12
+Source files:
13
+    dprint.h
14
+    dprint.c
15
+
16
+ Compile-time control macros
17
+=============================
18
+
19
+    NO_LOG
20
+	If defined, logging is completely disabled in SER and no messages
21
+        are produced at all
22
+	       
23
+    NO_DEBUG
24
+	If defined, logging messages do not include the source filename and
25
+	line location info
26
+
27
+ Logging levels
28
+================
29
+
30
+    L_DBG   ... Debugging message (the lowest level)
31
+    L_INFO  ... Info message
32
+    L_WARN  ... Warning message
33
+    L_ERR   ... Error message
34
+    L_CRIT  ... Critical message
35
+    L_ALERT ... Alert message (the highest level)
36
+
37
+    The levels are implemented as integer macros.
38
+
39
+ Related variables
40
+===================
41
+
42
+    debug
43
+	The config.framework setting that contains the current logging level.
44
+	The initial value can be specified by "debug" parameter in ser.cfg or
45
+	by -d options on the command-line.  The default value is L_WARN.
46
+
47
+    log_stderror
48
+	The global variable which specifies whether the log messages should be
49
+	send to the standard error output or syslog (equals to zero).
50
+	Its value can be specified by "log_stderr" parameter in ser.cfg or
51
+	-E option on the command-line.
52
+	
53
+    log_facility
54
+	The config.framework setting that contains the current facility for
55
+	logging to syslog.
56
+	The initial value can be specified by "log_facility" parameter in
57
+	ser.cfg.  The default value is LOG_DAEMON.
58
+
59
+ Macro functions
60
+=================
61
+
62
+    * short macro aliases:
63
+	DBG(FMT, ARGS...)   alias for LOG(L_DBG, FMT, ARGS...)
64
+        INFO(FMT, ARGS...)  alias for LOG(L_INFO, FMT, ARGS...)
65
+        WARN(FMT, ARGS...)  alias for LOG(L_WARN, FMT, ARGS...)
66
+        ERR(FMT, ARGS...)   alias for LOG(L_ERR, FMT, ARGS...)
67
+        BUG(FMT, ARGS...)   alias for LOG(L_CRIT, FMT, ARGS...)
68
+        ALERT(FMT, ARGS...) alias for LOG(L_ALERT, FMT, ARGS...)
69
+
70
+    * LOG(LEVEL, FMT, ARGS...) macro
71
+	Prints the log message on stderr or syslog if the current debug level
72
+	is greater or equal to LEVEL.  The message has the following format:
73
+
74
+          - for messages by core:
75
+              PROC(PID) LEVEL: <core> [FILE:LINE]: MESSAGE
76
+
77
+          - for messages by modules:
78
+              PROC(PID) LEVEL: MODULE [FILE:LINE]: MESSAGE
79
+	      
80
+          - for messages by log(), xlog(), xdbg() script funcitons:
81
+              PROC(PID) LEVEL: <script>: MESSAGE
82
+
83
+	PROC is the SER process number and PID is the linux process ID.
84
+        LEVEL is one of "DEBUG", "INFO", "NOTICE", "WARNING", "ERROR",
85
+	"ALERT" and "BUG" strings.  MESSAGE is constructed from printf-like
86
+	arguments FMT and ARGS.
87
+
88
+        [FILE:LINE] location info is not present if NO_DEBUG macro is defined.
89
+  
90
+	Use of shorter aliases is preferred if LEVEL is a preprocess-time
91
+	constant.
92
+	
93
+    * LOG_(LEVEL, PREFIX, FMT, ARGS...) macro
94
+	Prints the log message on stderr or syslog if the current debug level
95
+	is greater or equal to LEVEL.  The message has the following format:
96
+	
97
+              PROC(PID) LEVEL: PREFIXMESSAGE
98
+
99
+	This is an internal macro try to avoid using it.
100
+
101
+
102
+--------------------------------------------------------------------------------
103
+
104
+ APPENDIX: Summary of the changes to the original API
105
+======================================================
106
+
107
+  - LOG(LEVEL, FMT, ARGS...) and the short macro corresponding to LEVEL level
108
+    made eqvivalent (eg. LOG(L_DBG, FMT, ARGS...) and DBG(FMT, ARGS...) prints
109
+    always the same message)
110
+
111
+  - changed the format of log messages produced by the macros to include
112
+    the log level, module name, filename, line (if applicable)
113
+     
114
+  - added new, internal LOG_(LEVEL, PREFIX, FORMAT, ARGS...) macro
115
+
116
+  - removed DPrint() and DEBUG() macros, L_DEFAULT log level and dprint()
117
+    function
118
+
119
+!!!
120
+!!! IMPORTANT! READ ME!
121
+!!!
122
+!!!  These changes (mainly the first two) require reformating of the most log
123
+!!!  messages in SER core and module source files.  This step can be done
124
+!!!  automatically by running "scripts/logging/fix-logs-all" script BUT it
125
+!!!  was NOT originally performed because it would have generated too many
126
+!!!  changes in CVS which was discouraged by Andrei.  Instead, the developers
127
+!!!  are expected to run it when ready.
128
+!!!
129
+!!! IMPORTANT! READ ME!
130
+!!!  
131
+
132
+--
133
+$Id$
... ...
@@ -29,48 +29,47 @@
29 29
  */
30 30
 
31 31
  
32
-#include "dprint.h"
33 32
 #include "globals.h"
34
-#include "pt.h"
33
+#include "dprint.h"
35 34
  
36 35
 #include <stdarg.h>
37 36
 #include <stdio.h>
38 37
 #include <strings.h>
39 38
 
40
-volatile int dprint_crit=0; /* signal protection: !=0 when dprint/LOG/DBG are
41
-								printing */
39
+#ifndef NO_SIG_DEBUG
40
+/* signal protection: !=0 when LOG/DBG/... are printing */
41
+volatile int dprint_crit = 0; 
42
+#endif
42 43
 
43 44
 static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
44
-					"LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
45
-					"LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
46
-					"LOG_LOCAL6","LOG_LOCAL7","LOG_LPR","LOG_MAIL",
47
-					"LOG_NEWS","LOG_USER","LOG_UUCP",
45
+			"LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
46
+			"LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
47
+			"LOG_LOCAL6","LOG_LOCAL7","LOG_LPR","LOG_MAIL",
48
+			"LOG_NEWS","LOG_USER","LOG_UUCP",
48 49
 #ifndef __OS_solaris
49
-					"LOG_AUTHPRIV","LOG_FTP","LOG_SYSLOG",
50
+			"LOG_AUTHPRIV","LOG_FTP","LOG_SYSLOG",
50 51
 #endif
51
-					0};
52
+			0};
53
+			
52 54
 static int int_fac[]={LOG_AUTH ,  LOG_CRON , LOG_DAEMON ,
53
-					LOG_KERN , LOG_LOCAL0 , LOG_LOCAL1 ,
54
-					LOG_LOCAL2 , LOG_LOCAL3 , LOG_LOCAL4 , LOG_LOCAL5 ,
55
-					LOG_LOCAL6 , LOG_LOCAL7 , LOG_LPR , LOG_MAIL ,
56
-					LOG_NEWS , LOG_USER , LOG_UUCP
55
+		      LOG_KERN , LOG_LOCAL0 , LOG_LOCAL1 ,
56
+		      LOG_LOCAL2 , LOG_LOCAL3 , LOG_LOCAL4 , LOG_LOCAL5 ,
57
+		      LOG_LOCAL6 , LOG_LOCAL7 , LOG_LPR , LOG_MAIL ,
58
+		      LOG_NEWS , LOG_USER , LOG_UUCP,
57 59
 #ifndef __OS_solaris
58
-					,LOG_AUTHPRIV,LOG_FTP,LOG_SYSLOG
60
+		      LOG_AUTHPRIV,LOG_FTP,LOG_SYSLOG,
59 61
 #endif
60
-					};
61
-
62
-
63
-void dprint(char * format, ...)
64
-{
65
-	va_list ap;
66
-
67
-	fprintf(stderr, "%2d(%d) ", process_no, my_pid());
68
-	va_start(ap, format);
69
-	vfprintf(stderr,format,ap);
70
-	fflush(stderr);
71
-	va_end(ap);
72
-}
62
+		      0};
73 63
 
64
+struct log_level_info log_level_info[] = {
65
+	{"ALERT", LOG_ALERT},	  /* L_ALERT */
66
+	{"BUG", LOG_CRIT},        /* L_CRIT */
67
+	{"ERROR", LOG_ERR},       /* L_ERR */
68
+	{"WARNING", LOG_WARNING}, /* L_WARN */
69
+	{"NOTICE", LOG_NOTICE},   /* L_NOTICE */
70
+	{"INFO", LOG_INFO},       /* L_INFO */
71
+	{"DEBUG", LOG_DEBUG}	  /* L_DBG */
72
+};
74 73
 
75 74
 int str2facility(char *s)
76 75
 {
... ...
@@ -25,232 +25,201 @@
25 25
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 26
  */
27 27
 
28
-
29
-
30 28
 #ifndef dprint_h
31 29
 #define dprint_h
32 30
 
31
+#include <assert.h>
33 32
 #include <syslog.h>
34
-#include "cfg_core.h"
35
-
33
+#include <stdio.h> /* stderr, fprintf() */
36 34
 
37
-#define L_ALERT -3
38
-#define L_CRIT  -2
39
-#define L_ERR   -1
40
-#define L_DEFAULT 0
41
-#define L_WARN   1
42
-#define L_NOTICE 2
43
-#define L_INFO   3
44
-#define L_DBG    4
45
-
46
-/* vars:*/
35
+#include "cfg_core.h"
47 36
 
48
-extern int log_stderr;
49
-extern volatile int dprint_crit; /* protection against "simultaneous"
50
-									printing from signal handlers */
51 37
 
52
-#ifdef NO_SIG_DEBUG
53
-#define DPRINT_NON_CRIT		(1)
54
-#define DPRINT_CRIT_ENTER
55
-#define DPRINT_CRIT_EXIT
38
+/* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
39
+#if __STDC_VERSION__ < 199901L
40
+#	if __GNUC__ >= 2
41
+#		define _FUNC_NAME_ __FUNCTION__
42
+#	else
43
+#		define _FUNC_NAME_ ""
44
+#	endif
56 45
 #else
57
-#define DPRINT_NON_CRIT		(dprint_crit==0)
58
-#define DPRINT_CRIT_ENTER	(dprint_crit++)
59
-#define DPRINT_CRIT_EXIT	(dprint_crit--)
46
+#	define _FUNC_NAME_ __func__
60 47
 #endif
61 48
 
62
-#define DPRINT_LEV	1
63
-/* priority at which we log */
64
-#define DPRINT_PRIO LOG_DEBUG
49
+#ifdef NO_DEBUG
50
+#	ifdef MOD_NAME
51
+#		define LOC_INFO		MOD_NAME ": "
52
+#	else
53
+#		define LOC_INFO		"<core>: "
54
+#	endif
55
+#else
56
+#	define XCT2STR(i) #i
57
+#	define CT2STR(l)  XCT2STR(l)
58
+#
59
+#	ifdef MOD_NAME
60
+#		define LOC_INFO		MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
61
+#	else
62
+#		define LOC_INFO		"<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
63
+#	endif
64
+#
65
+#	ifdef NO_LOG
66
+#		undef NO_LOG
67
+#	endif
68
+#endif /* NO_DEBUG */
65 69
 
66 70
 
67
-void dprint (char* format, ...);
71
+/*
72
+ * Log levels
73
+ */
74
+#define L_ALERT		-3
75
+#define L_CRIT  	-2
76
+#define L_ERR   	-1
77
+#define L_WARN   	0
78
+#define L_NOTICE 	1
79
+#define L_INFO   	2
80
+#define L_DBG    	3
68 81
 
69
-int str2facility(char *s);
70
-int log_facility_fixup(void *handle, str *name, void **val);
82
+#define LOG_LEVEL2NAME(level)	(log_level_info[(level) - (L_ALERT)].name)
83
+#define LOG2SYSLOG_LEVEL(level)	(log_level_info[(level) - (L_ALERT)].syslog_level)
71 84
 
72
-/* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
73
-#if __STDC_VERSION__ < 199901L
74
-# if __GNUC__ >= 2
75
-#  define _FUNC_NAME_ __FUNCTION__
76
-# else
77
-#  define _FUNC_NAME_ ""
78
-# endif
79
-#else
80
-# define _FUNC_NAME_ __func__
81
-#endif
82 85
 
86
+/* my_pid(), process_no are from pt.h but we cannot #include it here
87
+   because of circular dependencies */
88
+extern int process_no;
89
+extern int my_pid();
83 90
 
84
-#define XCT2STR(i) #i
85
-#define CT2STR(l) XCT2STR(l)
91
+/* non-zero if logging to stderr instead to the syslog */
92
+extern int log_stderr;
86 93
 
87
-#define LOC_INFO	__FILE__ ":" CT2STR(__LINE__) ": "
94
+/* maps log levels to their string name and corresponding syslog level */
88 95
 
96
+struct log_level_info {
97
+ 	char *name;
98
+	int syslog_level;
99
+};
89 100
 
101
+extern struct log_level_info log_level_info[];
90 102
 
91
-#ifdef NO_DEBUG
92
-	#ifdef __SUNPRO_C
93
-		#define DPrint(...)
94
-	#else
95
-		#define DPrint(fmt, args...)
96
-	#endif
97
-#else
98
-	#ifdef __SUNPRO_C
99
-		#define DPrint( ...) \
100
-			do{ \
101
-				if ((cfg_get(core, core_cfg, debug)>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
102
-					DPRINT_CRIT_ENTER; \
103
-					if (log_stderr){ \
104
-						dprint (__VA_ARGS__); \
105
-					}else{ \
106
-						syslog(DPRINT_LEV|cfg_get(core, core_cfg, log_facility), \
107
-							__VA_ARGS__); \
108
-					}\
109
-					DPRINT_CRIT_EXIT; \
110
-				} \
111
-			}while(0)
112
-	#else
113
-			#define DPrint(fmt,args...) \
114
-			do{ \
115
-				if ((cfg_get(core, core_cfg, debug)>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
116
-					DPRINT_CRIT_ENTER; \
117
-					if (log_stderr){ \
118
-						dprint (fmt, ## args); \
119
-					}else{ \
120
-						syslog(DPRINT_LEV|cfg_get(core, core_cfg, log_facility), \
121
-							fmt, ## args); \
122
-					}\
123
-					DPRINT_CRIT_EXIT; \
124
-				} \
125
-			}while(0)
126
-	#endif
127
-
103
+#ifndef NO_SIG_DEBUG
104
+/* protection against "simultaneous" printing from signal handlers */
105
+extern volatile int dprint_crit; 
128 106
 #endif
129 107
 
130
-#ifndef NO_DEBUG
131
-	#undef NO_LOG
132
-#endif
108
+int str2facility(char *s);
109
+int log_facility_fixup(void *handle, str *name, void **val);
110
+
133 111
 
112
+/*
113
+ * General logging macros
114
+ *
115
+ * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to stderr (if
116
+ *	`log_stderr' is non-zero) to syslog.  Note that `fmt' must be constant.
117
+ *      `prefix' is added to the beginning of the message.
118
+ *
119
+ * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
120
+ */
134 121
 #ifdef NO_LOG
135
-	#ifdef __SUNPRO_C
136
-		#define LOG(lev, ...)
137
-	#else
138
-		#define LOG(lev, fmt, args...)
139
-	#endif
122
+
123
+#	ifdef __SUNPRO_C
124
+#		define LOG_(level, prefix, fmt, ...)
125
+#		define LOG(level, fmt, ...)
126
+#	else
127
+#		define LOG_(level, prefix, fmt, args...)
128
+#		define LOG(level, fmt, args...)
129
+#	endif
130
+
140 131
 #else
141
-	#ifdef __SUNPRO_C
142
-		#define LOG(lev, ...) \
132
+
133
+#	ifdef NO_SIG_DEBUG
134
+#		define DPRINT_NON_CRIT		(1)
135
+#		define DPRINT_CRIT_ENTER
136
+#		define DPRINT_CRIT_EXIT
137
+#	else
138
+#		define DPRINT_NON_CRIT		(dprint_crit==0)
139
+#		define DPRINT_CRIT_ENTER	(dprint_crit++)
140
+#		define DPRINT_CRIT_EXIT		(dprint_crit--)
141
+#	endif
142
+
143
+#	ifdef __SUNPRO_C
144
+#		define LOG_(level, prefix, fmt, ...) \
143 145
 			do { \
144
-				if ((cfg_get(core, core_cfg, debug)>=(lev)) && DPRINT_NON_CRIT){ \
146
+				if (cfg_get(core, core_cfg, debug) >= (level) && DPRINT_NON_CRIT) { \
145 147
 					DPRINT_CRIT_ENTER; \
146
-					if (log_stderr) dprint (__VA_ARGS__); \
147
-					else { \
148
-						switch(lev){ \
149
-							case L_CRIT: \
150
-								syslog(LOG_CRIT|cfg_get(core, core_cfg, log_facility), \
151
-									__VA_ARGS__); \
152
-								break; \
153
-							case L_ALERT: \
154
-								syslog(LOG_ALERT|cfg_get(core, core_cfg, log_facility), \
155
-									__VA_ARGS__); \
156
-								break; \
157
-							case L_ERR: \
158
-								syslog(LOG_ERR|cfg_get(core, core_cfg, log_facility), \
159
-									__VA_ARGS__); \
160
-								break; \
161
-							case L_WARN: \
162
-								syslog(LOG_WARNING|cfg_get(core, core_cfg, log_facility), \
163
-									__VA_ARGS__);\
164
-								break; \
165
-							case L_NOTICE: \
166
-								syslog(LOG_NOTICE|cfg_get(core, core_cfg, log_facility), \
167
-									__VA_ARGS__); \
168
-								break; \
169
-							case L_INFO: \
170
-								syslog(LOG_INFO|cfg_get(core, core_cfg, log_facility), \
171
-									__VA_ARGS__); \
172
-								break; \
173
-							case L_DBG: \
174
-								syslog(LOG_DEBUG|cfg_get(core, core_cfg, log_facility), \
175
-									__VA_ARGS__); \
176
-								break; \
177
-						} \
148
+					assert(((level) >= L_ALERT) && ((level) <= L_DBG)); \
149
+					if (log_stderr) { \
150
+						fprintf(stderr, "%2d(%d) %s: %s" fmt, \
151
+					            process_no, my_pid(), LOG_LEVEL2NAME(level), (prefix), __VA_ARGS__); \
152
+					} else { \
153
+						syslog(LOG2SYSLOG_LEVEL(level) | cfg_get(core, core_cfg, log_facility), \
154
+						       "%s: %s" fmt, log_level_info[(level)].name, (prefix), __VA_ARGS__); \
178 155
 					} \
179 156
 					DPRINT_CRIT_EXIT; \
180 157
 				} \
181
-			}while(0)
182
-	#else
183
-		#define LOG(lev, fmt, args...) \
158
+			} while(0)
159
+			
160
+#		define LOG(level, fmt, ...)  LOG_((level), LOC_INFO, fmt, __VA_ARGS__)
161
+
162
+#	else
163
+#		define LOG_(level, prefix, fmt, args...) \
184 164
 			do { \
185
-				if ((cfg_get(core, core_cfg, debug)>=(lev)) && DPRINT_NON_CRIT){ \
165
+				if (cfg_get(core, core_cfg, debug) >= (level) && DPRINT_NON_CRIT) { \
186 166
 					DPRINT_CRIT_ENTER; \
187
-					if (log_stderr) dprint (fmt, ## args); \
188
-					else { \
189
-						switch(lev){ \
190
-							case L_CRIT: \
191
-								syslog(LOG_CRIT|cfg_get(core, core_cfg, log_facility), \
192
-									fmt, ##args); \
193
-								break; \
194
-							case L_ALERT: \
195
-								syslog(LOG_ALERT|cfg_get(core, core_cfg, log_facility), \
196
-									fmt, ##args); \
197
-								break; \
198
-							case L_ERR: \
199
-								syslog(LOG_ERR|cfg_get(core, core_cfg, log_facility), \
200
-									fmt, ##args); \
201
-								break; \
202
-							case L_WARN: \
203
-								syslog(LOG_WARNING|cfg_get(core, core_cfg, log_facility), \
204
-									fmt, ##args);\
205
-								break; \
206
-							case L_NOTICE: \
207
-								syslog(LOG_NOTICE|cfg_get(core, core_cfg, log_facility), \
208
-									fmt, ##args); \
209
-								break; \
210
-							case L_INFO: \
211
-								syslog(LOG_INFO|cfg_get(core, core_cfg, log_facility), \
212
-									fmt, ##args); \
213
-								break; \
214
-							case L_DBG: \
215
-								syslog(LOG_DEBUG|cfg_get(core, core_cfg, log_facility), \
216
-									fmt, ##args); \
217
-								break; \
218
-						} \
167
+					assert(((level) >= L_ALERT) && ((level) <= L_DBG)); \
168
+					if (log_stderr) { \
169
+						fprintf(stderr, "%2d(%d) %s: %s" fmt, \
170
+					            process_no, my_pid(), LOG_LEVEL2NAME(level), (prefix), ## args); \
171
+					} else { \
172
+						syslog(LOG2SYSLOG_LEVEL(level) | cfg_get(core, core_cfg, log_facility), \
173
+						       "%s: %s" fmt, log_level_info[(level)].name, (prefix), ## args); \
219 174
 					} \
220 175
 					DPRINT_CRIT_EXIT; \
221 176
 				} \
222
-			}while(0)
223
-	#endif /*SUN_PRO_C*/
224
-#endif
177
+			} while(0)
178
+			
179
+#		define LOG(level, fmt, args...)  LOG_((level), LOC_INFO, fmt, ## args)
180
+		
181
+#	endif /* __SUNPRO_C */
182
+#endif /* NO_LOG */
225 183
 
226 184
 
227
-#ifdef NO_DEBUG
228
-	#ifdef __SUNPRO_C
229
-		#define DBG(...)
230
-	#else
231
-		#define DBG(fmt, args...)
232
-	#endif
233
-#else
234
-	#ifdef __SUNPRO_C
235
-		#define DBG(...) LOG(L_DBG, __VA_ARGS__)
236
-	#else
237
-		#define DBG(fmt, args...) LOG(L_DBG, fmt, ## args)
238
-	#endif
239
-#endif
240
-
185
+/*
186
+ * Simplier, prefered logging macros for constant log level
187
+ */
241 188
 #ifdef __SUNPRO_C
242
-		#define DEBUG(...) DBG("DEBUG: "          LOC_INFO __VA_ARGS__)
243
-		#define ERR(...)  LOG(L_ERR, "ERROR: "    LOC_INFO __VA_ARGS__)
244
-		#define WARN(...) LOG(L_WARN, "WARNING: " LOC_INFO __VA_ARGS__)
245
-		#define INFO(...) LOG(L_INFO, "INFO: "    LOC_INFO __VA_ARGS__)
246
-		#define BUG(...) LOG(L_CRIT, "BUG: "      LOC_INFO __VA_ARGS__)
189
+#	define ALERT(...)  LOG(L_ALERT,  __VA_ARGS__)
190
+#	define BUG(...)    LOG(L_CRIT,   __VA_ARGS__)
191
+#	define ERR(...)    LOG(L_ERR,    __VA_ARGS__)
192
+#	define WARN(...)   LOG(L_WARN,   __VA_ARGS__)
193
+#	define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
194
+#	define INFO(...)   LOG(L_INFO,   __VA_ARGS__)
195
+
196
+#	ifdef NO_DEBUG
197
+#		define DBG(...)
198
+#	else
199
+#		define DBG(...)    LOG(L_DBG, __VA_ARGS__)
200
+#	endif		
201
+
202
+/* obsolete, do not use */
203
+#	define DEBUG(...) DBG(__VA_ARGS__)
204
+		
247 205
 #else
248
-		#define DEBUG(fmt, args...) DBG("DEBUG: "       LOC_INFO fmt, ## args)
249
-		#define ERR(fmt, args...) LOG(L_ERR, "ERROR: "  LOC_INFO fmt, ## args)
250
-		#define WARN(fmt, args...) LOG(L_WARN, "WARN: " LOC_INFO fmt, ## args)
251
-		#define INFO(fmt, args...) LOG(L_INFO, "INFO: " LOC_INFO fmt, ## args)
252
-		#define BUG(fmt, args...) LOG(L_CRIT, "BUG: "   LOC_INFO fmt, ## args)
253
-#endif
254
-
255
-
256
-#endif /* ifndef dprint_h */
206
+#	define ALERT(fmt, args...)  LOG(L_ALERT,  fmt, ## args)
207
+#	define BUG(fmt, args...)    LOG(L_CRIT,   fmt, ## args)
208
+#	define ERR(fmt, args...)    LOG(L_ERR,    fmt, ## args)
209
+#	define WARN(fmt, args...)   LOG(L_WARN,   fmt, ## args)
210
+#	define NOTICE(fmt, args...) LOG(L_NOTICE, fmt, ## args)
211
+#	define INFO(fmt, args...)   LOG(L_INFO,   fmt, ## args)
212
+
213
+#	ifdef NO_DEBUG
214
+#		define DBG(fmt, args...)
215
+#	else
216
+#		define DBG(fmt, args...)    LOG(L_DBG, fmt, ## args)
217
+#	endif		
218
+
219
+/* obsolete, do not use */
220
+#	define DEBUG(fmt, args...) DBG(fmt, ## args)
221
+		
222
+#endif /* __SUNPRO_C */
223
+
224
+
225
+#endif /* !dprint_h */
... ...
@@ -641,7 +641,7 @@ void handle_sigs()
641 641
 				DBG("SIGTERM received, program terminates\n");
642 642
 			/* shutdown/kill all the children */
643 643
 			shutdown_children(SIGTERM, 1);
644
-			dprint("Thank you for flying " NAME "\n");
644
+			LOG(L_NOTICE, "Thank you for flying " NAME "\n");
645 645
 			exit(0);
646 646
 			break;
647 647
 
... ...
@@ -767,32 +767,32 @@ int install_sigs()
767 767
 {
768 768
 	/* added by jku: add exit handler */
769 769
 	if (set_sig_h(SIGINT, sig_usr) == SIG_ERR ) {
770
-		DPrint("ERROR: no SIGINT signal handler can be installed\n");
770
+		ERR("no SIGINT signal handler can be installed\n");
771 771
 		goto error;
772 772
 	}
773 773
 	/* if we debug and write to a pipe, we want to exit nicely too */
774 774
 	if (set_sig_h(SIGPIPE, sig_usr) == SIG_ERR ) {
775
-		DPrint("ERROR: no SIGINT signal handler can be installed\n");
775
+		ERR("no SIGINT signal handler can be installed\n");
776 776
 		goto error;
777 777
 	}
778 778
 	if (set_sig_h(SIGUSR1, sig_usr)  == SIG_ERR ) {
779
-		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
779
+		ERR("no SIGUSR1 signal handler can be installed\n");
780 780
 		goto error;
781 781
 	}
782 782
 	if (set_sig_h(SIGCHLD , sig_usr)  == SIG_ERR ) {
783
-		DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
783
+		ERR("no SIGCHLD signal handler can be installed\n");
784 784
 		goto error;
785 785
 	}
786 786
 	if (set_sig_h(SIGTERM , sig_usr)  == SIG_ERR ) {
787
-		DPrint("ERROR: no SIGTERM signal handler can be installed\n");
787
+		ERR("no SIGTERM signal handler can be installed\n");
788 788
 		goto error;
789 789
 	}
790 790
 	if (set_sig_h(SIGHUP , sig_usr)  == SIG_ERR ) {
791
-		DPrint("ERROR: no SIGHUP signal handler can be installed\n");
791
+		ERR("no SIGHUP signal handler can be installed\n");
792 792
 		goto error;
793 793
 	}
794 794
 	if (set_sig_h(SIGUSR2 , sig_usr)  == SIG_ERR ) {
795
-		DPrint("ERROR: no SIGUSR2 signal handler can be installed\n");
795
+		ERR("no SIGUSR2 signal handler can be installed\n");
796 796
 		goto error;
797 797
 	}
798 798
 	return 0;
799 799
new file mode 100755
... ...
@@ -0,0 +1,155 @@
0
+#!/usr/bin/perl -w
1
+#
2
+# Usage: fix-log [MODULE-NAME] < INFILE > OUTFILE
3
+#
4
+# Fixes logging macros and messages in the SER source file INFILE
5
+# to match the recent updates in the logging API.
6
+#
7
+# Specify MODULE_NAME if INFILE source file is a part of a SER module.
8
+#
9
+# See doc/logging-api.txt for details.
10
+#
11
+# $Id$
12
+
13
+#
14
+# What *exactly* does this script do?
15
+#
16
+#   - replaces LOG(L_*, ...) with the short macro corresponding to L_* level
17
+#
18
+#   - replaces DEBUG() with DBG() macro
19
+#
20
+#   - removes MODULE and the level string prefixes from FMT arguments of macros
21
+#     where FMT looks like "X:...", "X:Y:..." or "X:Y:Z:...", white spaces are
22
+#     ignored and preserved, string matching is case-insensitive
23
+#
24
+#     In addition, if the level string found in FMT argument doesn't match the actual
25
+#     level of the macro, the macro level is fixed.
26
+#
27
+#     Examples:
28
+#        ERR("ERROR:tm: blah\n")           becomes ERR("blah\n")
29
+#        DBG("Debug: blah\n")              becomes DBG("blah\n")
30
+#        LOG(L_ERR, "tm: INFO: blah\n")    becomes INFO("blah\n")
31
+#
32
+#   - removes 'MODULE_NAME ":' string from the beggining of FMT arguments of macros
33
+#     in module source files (a common special case)
34
+#
35
+#     Example:
36
+#        LOG(L_ERR, MODULE_NAME ": tm:Info:blah\n")   becomes INFO("blah\n")
37
+#
38
+
39
+# Map a text string to L_* log level macro
40
+my %text2level = (
41
+    "BUG"         => "L_CRIT",
42
+    "CRIT"        => "L_CRIT",
43
+    "CRITICAL"    => "L_CRIT",
44
+
45
+    "ALERT"       => "L_ALERT",
46
+
47
+    "ERR"         => "L_ERR",
48
+    "ERROR"       => "L_ERR",
49
+
50
+    "WARN"        => "L_WARN",
51
+    "WARNING"     => "L_WARN",
52
+
53
+    "NOTICE"      => "L_NOTICE",
54
+
55
+    "INFO"        => "L_INFO",
56
+
57
+    "DBG"         => "L_DBG",
58
+    "DEBUG"       => "L_DBG",
59
+);
60
+
61
+#
62
+short2level
63
+
64
+
65
+# Strip the leading and trailing whitespaces and upper-case a text
66
+sub norm {
67
+    my $text = ($_[0] || "");
68
+    $text =~ s/^\s*//;
69
+    $text =~ s/\s*$//;
70
+    uc($text);
71
+}
72
+
73
+my $module_name = norm($ARGV[0]);
74
+
75
+sub fix_log_prefix {
76
+    my ($prefix, $level) = ($_[0], $_[1]);
77
+
78
+    # delete prefix if it contains module name
79
+    if ($module_name) {
80
+        if (!$text || (norm($text) eq $module_name)) {
81
+            return ("", $level);
82
+        }
83
+    }
84
+
85
+    # delete prefix if it contains text level
86
+    my $prefix_level = $text2level{norm($prefix)};
87
+    if ($prefix_level) {
88
+	$prefix = "";
89
+	
90
+	# change level if does not match prefix level
91
+	if ($level =~ /^L_(DBG|INFO|NOTICE|WARN|ERR|CRIT|ALERT)$/ && 
92
+	    $level ne $prefix_level) {
93
+    	    return ("", $prefix_level);
94
+	}
95
+    }
96
+
97
+    return ($prefix . ":", $level);
98
+}
99
+
100
+sub fix_log {
101
+    my $level = $_[0];
102
+    my $prefix1 = $_[1];
103
+    my $prefix2 = $_[2];
104
+    my $prefix3 = $_[3];
105
+    my $space = $_[4];
106
+
107
+    ($prefix1, $level) = fix_log_prefix($prefix1, $level) if $prefix1;
108
+    ($prefix2, $level) = fix_log_prefix($prefix2, $level) if $prefix2;
109
+    ($prefix3, $level) = fix_log_prefix($prefix3, $level) if $prefix3;
110
+
111
+    my $prefix = $prefix1 . $prefix2 . $prefix3 . $space;
112
+    $prefix =~ s/^\s*//;
113
+    
114
+    "LOG($level, \"$prefix";
115
+}
116
+
117
+while (<STDIN>) {
118
+AGAIN:
119
+    # replace DEBUG() by DBG()
120
+    s/DEBUG\(/DBG\(/g;
121
+
122
+    # ...in case the statement spans more lines
123
+    if (/(DBG|INFO|NOTICE|WARN|ERR|BUG|ALERT)\(\s*$/ || /LOG\(([^,]*,)?\s*$/) {
124
+        $_ .= <STDIN>;
125
+        goto AGAIN;
126
+    }
127
+
128
+    # one common special case used in several modules
129
+    if ($module_name) {
130
+	s/LOG\(\s*([^,]+)\s*,\s*MODULE_NAME\s*"\s*:\s*/LOG($1, "/g;
131
+	s/(DBG|INFO|NOTICE|WARN|ERR|BUG|ALERT)\(\s*MODULE_NAME\s*"\s*:\s*/$1("/g;
132
+    }
133
+
134
+    # module name and level prefix removing magic, may change macro level
135
+    # if different one found in the message
136
+    $id='\s*[a-zA-Z0-9_]+\s*';
137
+    
138
+    s/LOG\(\s*(([A-Z_]+)|[^,]+)\s*,\s*"($id):(($id):(($id):)?)?(\s*)/
139
+        fix_log($1, $3, $5, $7, $8);
140
+    /eg;
141
+
142
+    s/(DBG|INFO|NOTICE|WARN|ERR|BUG|ALERT)\(\s*"($id):(($id):(($id):)?)?(\s*)/
143
+        $1 = "CRIT" if $1 eq "BUG";
144
+        fix_log("L_$1", $2, $4, $6, $7);
145
+    /eg;
146
+
147
+    # prefer shorter static-level macros
148
+    s/LOG\(\s*L_(DBG|INFO|NOTICE|WARN|ERR|CRIT|ALERT)\s*,\s*/
149
+	$1 = "BUG" if $1 eq "CRIT";
150
+	"$1\(";
151
+    /eg;
152
+
153
+    print;
154
+}
0 155
new file mode 100755
... ...
@@ -0,0 +1,37 @@
0
+#!/bin/sh
1
+#
2
+# Usage: fix-logs-all [DIR]
3
+#
4
+# Fixes logging macros and messages in SER source files in DIR
5
+# directory (recursively).
6
+#
7
+# See doc/logging-api.txt and fix-logs script for details.
8
+#
9
+# $Id$
10
+
11
+# <filename>.ORIG backup files is created for each processed file.
12
+# If TEST is set, it only prints a "patch" file.
13
+#
14
+
15
+find ${1:-.} -type f \( -name "*.[chy]" -o -name "*.lex" -o -name "*.cc" \) | \
16
+    grep -v "/dprint\.[hc]$" | \
17
+	while read file; do
18
+	    echo "=== $file"
19
+
20
+	    if ! test "$TEST"; then
21
+		mv "$file" "$file.ORIG"
22
+	    fi
23
+
24
+	    if expr match "$file" ".*/modules/" >/dev/null; then
25
+		module=$(basename $(dirname $file))
26
+	    fi
27
+
28
+	    if ! test "$TEST"; then
29
+		fix-logs "$module" < "$file.ORIG" > "$file"
30
+	    else
31
+		fix-logs "$module" < "$file" > "$file.NEW"
32
+
33
+		diff "$file.NEW" "$file"
34
+		rm "$file.NEW"
35
+	    fi
36
+	done