summaryrefslogtreecommitdiff
path: root/net/asterisk10/files/patch-suppress_log_dups.diff
blob: 115b69f21ca17b26e26cb89ac7013cdb508f5283 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
Index: main/logger.c
===================================================================
--- main/logger.c	(revision 188505)
+++ main/logger.c	(working copy)
@@ -140,6 +140,17 @@
 AST_THREADSTORAGE(log_buf, log_buf_init);
 #define LOG_BUF_INIT_SIZE       128
 
+/*
+ * Storage for previous log message to prevent log storms
+ */
+static int stored_log_level;
+static int stored_log_dup_count = 0;
+static char *stored_log_msg = NULL;
+static const char *stored_log_file;
+static int stored_log_line;
+static const char *stored_log_function;
+static ast_mutex_t stored_log_msg_lock;
+
 static int make_components(char *s, int lineno)
 {
 	char *w;
@@ -591,6 +602,8 @@
 	char tmp[256];
 	int res = 0;
 
+        ast_mutex_init(&stored_log_msg_lock);
+
 	/* auto rotate if sig SIGXFSZ comes a-knockin */
 	(void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
 
@@ -655,14 +668,14 @@
 	return;
 }
 
-static void __attribute__((format(printf, 5, 0))) ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) 
+static void ast_log_syslog(int level, const char *file, int line, const char *function, const char *msg) 
 {
 	char buf[BUFSIZ];
 	char *s;
 
 	if (level >= SYSLOG_NLEVELS) {
 		/* we are locked here, so cannot ast_log() */
-		fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
+		fprintf(stderr, "ast_log_syslog called with bogus level: %d\n", level);
 		return;
 	}
 	if (level == __LOG_VERBOSE) {
@@ -676,27 +689,20 @@
 			 levels[level], (long)GETTID(), file, line, function);
 	}
 	s = buf + strlen(buf);
-	vsnprintf(s, sizeof(buf) - strlen(buf), fmt, args);
+	snprintf(s, sizeof(buf) - strlen(buf), "%s", msg);
 	term_strip(s, s, strlen(s) + 1);
 	syslog(syslog_level_map[level], "%s", buf);
 }
 
-/*!
- * \brief send log messages to syslog and/or the console
- */
-void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
+static void __ast_log(int level, const char *file, int line, const char *function, char *msg, int msglen)
 {
 	struct logchannel *chan;
-	struct ast_dynamic_str *buf;
 	time_t t;
 	struct tm tm;
 	char date[256];
+        char buf[2048];
+        int res;
 
-	va_list ap;
-
-	if (!(buf = ast_dynamic_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
-		return;
-
 	if (AST_LIST_EMPTY(&logchannels))
 	{
 		/*
@@ -704,35 +710,12 @@
 		 * so just log to stdout
 		*/
 		if (level != __LOG_VERBOSE) {
-			int res;
-			va_start(ap, fmt);
-			res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
-			va_end(ap);
-			if (res != AST_DYNSTR_BUILD_FAILED) {
-				term_filter_escapes(buf->str);
-				fputs(buf->str, stdout);
-			}
+                        term_filter_escapes(msg);
+                        fputs(msg, stdout);
 		}
 		return;
 	}
 
-	/* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
-	   are non-zero; LOG_DEBUG messages can still be displayed if option_debug
-	   is zero, if option_verbose is non-zero (this allows for 'level zero'
-	   LOG_DEBUG messages to be displayed, if the logmask on any channel
-	   allows it)
-	*/
-	if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
-		return;
-
-	/* Ignore anything that never gets logged anywhere */
-	if (!(global_logmask & (1 << level)))
-		return;
-	
-	/* Ignore anything other than the currently debugged file if there is one */
-	if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
-		return;
-
 	time(&t);
 	ast_localtime(&t, &tm, NULL);
 	strftime(date, sizeof(date), dateformat, &tm);
@@ -740,13 +723,8 @@
 	AST_LIST_LOCK(&logchannels);
 
 	if (logfiles.event_log && level == __LOG_EVENT) {
-		va_start(ap, fmt);
-
-		fprintf(eventlog, "%s asterisk[%ld]: ", date, (long)getpid());
-		vfprintf(eventlog, fmt, ap);
+		fprintf(eventlog, "%s asterisk[%ld]: %s", date, (long)getpid(), msg);
 		fflush(eventlog);
-
-		va_end(ap);
 		AST_LIST_UNLOCK(&logchannels);
 		return;
 	}
@@ -756,18 +734,15 @@
 			break;
 		/* Check syslog channels */
 		if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << level))) {
-			va_start(ap, fmt);
-			ast_log_vsyslog(level, file, line, function, fmt, ap);
-			va_end(ap);
+			ast_log_syslog(level, file, line, function, msg);
 		/* Console channels */
 		} else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
 			char linestr[128];
 			char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
 
 			if (level != __LOG_VERBOSE) {
-				int res;
 				sprintf(linestr, "%d", line);
-				ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf,
+				snprintf(buf, sizeof(buf),
 					"[%s] %s[%ld]: %s:%s %s: ",
 					date,
 					term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
@@ -776,23 +751,17 @@
 					term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
 					term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
 				/*filter to the console!*/
-				term_filter_escapes(buf->str);
-				ast_console_puts_mutable(buf->str);
-				
-				va_start(ap, fmt);
-				res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
-				va_end(ap);
-				if (res != AST_DYNSTR_BUILD_FAILED)
-					ast_console_puts_mutable(buf->str);
+				term_filter_escapes(buf);
+				ast_console_puts_mutable(buf);
+				ast_console_puts_mutable(msg);
 			}
 		/* File channels */
 		} else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
-			int res;
-			ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf, 
+			snprintf(buf, sizeof(buf), 
 				"[%s] %s[%ld] %s: ",
 				date, levels[level], (long)GETTID(), file);
-			res = fprintf(chan->fileptr, "%s", buf->str);
-			if (res <= 0 && !ast_strlen_zero(buf->str)) {	/* Error, no characters printed */
+			res = fprintf(chan->fileptr, "%s", buf);
+			if (res <= 0 && !ast_strlen_zero(buf)) {	/* Error, no characters printed */
 				fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
 				if (errno == ENOMEM || errno == ENOSPC) {
 					fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
@@ -801,16 +770,10 @@
 				manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
 				chan->disabled = 1;	
 			} else {
-				int res;
 				/* No error message, continue printing */
-				va_start(ap, fmt);
-				res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
-				va_end(ap);
-				if (res != AST_DYNSTR_BUILD_FAILED) {
-					term_strip(buf->str, buf->str, buf->len);
-					fputs(buf->str, chan->fileptr);
-					fflush(chan->fileptr);
-				}
+                                term_strip(msg, msg, msglen);
+                                fputs(msg, chan->fileptr);
+                                fflush(chan->fileptr);
 			}
 		}
 	}
@@ -825,6 +788,65 @@
 	}
 }
 
+/*!
+ * \brief send log messages to syslog and/or the console
+ */
+void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
+{
+	struct ast_dynamic_str *buf;
+        int res;
+	va_list ap;
+
+	if (!(buf = ast_dynamic_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
+		return;
+
+        va_start(ap, fmt);
+        res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
+        va_end(ap);
+        if (res == AST_DYNSTR_BUILD_FAILED)
+                return;
+
+	/* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
+	   are non-zero; LOG_DEBUG messages can still be displayed if option_debug
+	   is zero, if option_verbose is non-zero (this allows for 'level zero'
+	   LOG_DEBUG messages to be displayed, if the logmask on any channel
+	   allows it)
+	*/
+	if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
+		return;
+
+	/* Ignore anything that never gets logged anywhere */
+	if (!(global_logmask & (1 << level)))
+		return;
+	
+	/* Ignore anything other than the currently debugged file if there is one */
+	if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
+		return;
+
+        ast_mutex_lock(&stored_log_msg_lock);
+        if (stored_log_msg == NULL || stored_log_level != level || strcmp(buf->str, stored_log_msg) != 0) {
+                if (stored_log_dup_count > 0) {
+                        char buf2[1024];
+
+                        snprintf(buf2, sizeof(buf2), "Last message repeated %d times\n", stored_log_dup_count);
+                        __ast_log(stored_log_level, stored_log_file, stored_log_line, stored_log_function, buf2, strlen(buf2));
+                }
+                if (stored_log_msg != NULL)
+                        ast_free(stored_log_msg);
+                stored_log_msg = ast_strdup(buf->str);
+                stored_log_level = level;
+                stored_log_dup_count = 0;
+                stored_log_file = file;
+                stored_log_line = line;
+                stored_log_function = function;
+                __ast_log(level, file, line, function, buf->str, buf->len);
+        }
+        else
+                ++stored_log_dup_count;
+
+        ast_mutex_unlock(&stored_log_msg_lock);
+}
+
 void ast_backtrace(void)
 {
 #ifdef linux