Browse code

core: updated dprint api to enable support for debug level per module

- a callback can be registered to return the log level based on module
name

Daniel-Constantin Mierla authored on 17/05/2013 07:56:34
Showing 2 changed files
... ...
@@ -115,12 +115,29 @@ int log_facility_fixup(void *handle, str *gname, str *name, void **val)
115 115
 
116 116
 /* the local debug log level */
117 117
 static int _local_debug_level = UNSET_LOCAL_DEBUG_LEVEL;
118
+/* callback to get per module debug level */
119
+static get_module_debug_level_f _module_debug_level = NULL;
120
+
121
+/**
122
+ * @brief set callback function for per module debug level
123
+ */
124
+void set_module_debug_level_cb(get_module_debug_level_f f)
125
+{
126
+	_module_debug_level = f;
127
+}
118 128
 
119 129
 /**
120 130
  * @brief return the log level - the local one if it set,
121 131
  *   otherwise the global value
122 132
  */
123
-int get_debug_level(void) {
133
+int get_debug_level(char *mname, int mnlen) {
134
+	int mlevel = L_DBG;
135
+	/*important -- no LOGs inside, because it will loop */
136
+	if(unlikely(_module_debug_level!=NULL && mnlen>0)) {
137
+		if(_module_debug_level(mname, mnlen, &mlevel)==0) {
138
+			return mlevel;
139
+		}
140
+	}
124 141
 	return (_local_debug_level != UNSET_LOCAL_DEBUG_LEVEL) ?
125 142
 				_local_debug_level : cfg_get(core, core_cfg, debug);
126 143
 }
... ...
@@ -59,8 +59,10 @@
59 59
 #ifdef NO_DEBUG
60 60
 #	ifdef MOD_NAME
61 61
 #		define LOC_INFO		MOD_NAME ": "
62
+#		define LOG_MNAME	MOD_NAME
62 63
 #	else
63 64
 #		define LOC_INFO		"<core>: "
65
+#		define LOG_MNAME	"core"
64 66
 #	endif
65 67
 #else
66 68
 #	define XCT2STR(i) #i
... ...
@@ -68,8 +70,10 @@
68 70
 #
69 71
 #	ifdef MOD_NAME
70 72
 #		define LOC_INFO		MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
73
+#		define LOG_MNAME	MOD_NAME
71 74
 #	else
72 75
 #		define LOC_INFO		"<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
76
+#		define LOG_MNAME	"core"
73 77
 #	endif
74 78
 #
75 79
 #	ifdef NO_LOG
... ...
@@ -77,6 +81,7 @@
77 81
 #	endif
78 82
 #endif /* NO_DEBUG */
79 83
 
84
+#define LOG_MNAME_LEN		(sizeof(LOG_MNAME)-1)
80 85
 
81 86
 /*
82 87
  * Log levels
... ...
@@ -121,11 +126,13 @@ struct log_level_info {
121 126
 };
122 127
 
123 128
 /** @brief per process debug level handling */
124
-int get_debug_level(void);
129
+int get_debug_level(char *mname, int mnlen);
125 130
 void set_local_debug_level(int level);
126 131
 void reset_local_debug_level(void);
132
+typedef int (*get_module_debug_level_f)(char *mname, int mnlen, int *mlevel);
133
+void set_module_debug_level_cb(get_module_debug_level_f f);
127 134
 
128
-#define is_printable(level) (get_debug_level()>=(level))
135
+#define is_printable(level) (get_debug_level(LOG_MNAME, LOG_MNAME_LEN)>=(level))
129 136
 extern struct log_level_info log_level_info[];
130 137
 extern char *log_name;
131 138
 
... ...
@@ -179,7 +186,7 @@ void dprint_term_color(char f, char b, str *obuf);
179 186
 #	ifdef __SUNPRO_C
180 187
 #		define LOG_(facility, level, prefix, fmt, ...) \
181 188
 			do { \
182
-				if (unlikely(get_debuglevel() >= (level) && \
189
+				if (unlikely(get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
183 190
 						DPRINT_NON_CRIT)) { \
184 191
 					DPRINT_CRIT_ENTER; \
185 192
 					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
... ...
@@ -245,7 +252,7 @@ void dprint_term_color(char f, char b, str *obuf);
245 252
 #	else /* ! __SUNPRO_C */
246 253
 #		define LOG_(facility, level, prefix, fmt, args...) \
247 254
 			do { \
248
-				if (get_debug_level() >= (level) && \
255
+				if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
249 256
 						DPRINT_NON_CRIT) { \
250 257
 					DPRINT_CRIT_ENTER; \
251 258
 					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \