]> arthur.barton.de Git - netatalk.git/commitdiff
Flooding
authorFrank Lahm <franklahm@googlemail.com>
Sun, 19 Sep 2010 18:08:16 +0000 (20:08 +0200)
committerFrank Lahm <franklahm@googlemail.com>
Sun, 19 Sep 2010 18:08:16 +0000 (20:08 +0200)
libatalk/util/logger.c
libatalk/util/test/logger_test.c

index 1b3e9a2fa13af3ee2c3577d461133405108aa5e0..5ad984084fc1d9ff391abe0a83e300fdaac3e3eb 100644 (file)
@@ -92,8 +92,10 @@ UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = {
 };
 
 /* We use this in order to track the last n log messages in order to prevent flooding */
-#define LOG_FLOODING_MAXCOUNT 10 /* this controls after how many consecutive messages we force a 
-                                    "repeated x times" message */
+#define LOG_FLOODING_MINCOUNT 5 /* this controls after how many consecutive messages must be detected
+                                   before we start to hide them */
+#define LOG_FLOODING_MAXCOUNT 1000 /* this controls after how many consecutive messages we force a 
+                                      "repeated x times" message */
 #define LOG_FLOODING_ARRAY_SIZE 3 /* this contols how many messages in flow we track */
 struct log_flood_entry {
     int count;
@@ -538,7 +540,7 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
     }
 
     /* Prevent flooding: hash the message and check if we got the same one recently */
-    int hash = hash_message(temp_buffer);
+    int hash = hash_message(temp_buffer) + log_src_linenumber;
 
     /* Search for the same message by hash */
     for (int i = log_flood_entries - 1; i >= 0; i--) {
@@ -552,7 +554,7 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
                 /* yes, log it and remove from array */
 
                 /* reusing log_details_buffer */
-                sprintf(log_details_buffer, "message repeated %i times: ", log_flood_array[i].count);
+                sprintf(log_details_buffer, "message repeated %i times: ", LOG_FLOODING_MAXCOUNT - 1);
                 iov[0].iov_base = log_details_buffer;
                 iov[0].iov_len = strlen(log_details_buffer);
                 iov[1].iov_base = temp_buffer;
@@ -571,6 +573,11 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
                     log_flood_array[j-1] = log_flood_array[j];
                 log_flood_entries--;
             }
+
+            if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
+                /* log it */
+                goto log;
+            /* discard it */
             goto exit;
         } /* if */
     }  /* for */
@@ -578,26 +585,23 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
     /* No matching message found, add this message to array*/
     if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
         /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
-        if (log_flood_array[0].count > 1) {
-                /* reusing log_details_buffer */
-                sprintf(log_details_buffer, "message repeated %i times: ", log_flood_array[0].count);
-                iov[0].iov_base = log_details_buffer;
-                iov[0].iov_len = strlen(log_details_buffer);
-                iov[1].iov_base = temp_buffer;
-                iov[1].iov_len = strlen(temp_buffer);
-
-                /* Write "message repeated x times: ..." to log */
-                writev( fd, iov, 2);
+        if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
+            /* reusing log_details_buffer */
+            sprintf(log_details_buffer, "message %i lines before repeated %i times\n",
+                    LOG_FLOODING_ARRAY_SIZE , log_flood_array[0].count - 1);
+            /* Write "message repeated x times: ..." to log */
+            write(fd, log_details_buffer, strlen(log_details_buffer));
         }
         for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
             log_flood_array[i-1] = log_flood_array[i];
         }
         log_flood_entries--;
     }
-    log_flood_array[log_flood_entries-1].count = 1;
-    log_flood_array[log_flood_entries-1].hash = hash;
+    log_flood_array[log_flood_entries].count = 1;
+    log_flood_array[log_flood_entries].hash = hash;
     log_flood_entries++;
 
+log:
     if ( ! log_config.console) {
         generate_message_details(log_details_buffer, sizeof(log_details_buffer),
                                  type_configs[logtype].set ?
index cd00dbf9fb2b4148dee10db8f1e578089c66e5c9..6ee579bc7884f3afd4fd67b4513d9ea7263f6a09 100644 (file)
@@ -36,38 +36,45 @@ int main(int argc, char *argv[])
   LOG(log_maxdebug, logtype_dsi, "This should still log.");
 
   /* flooding prevention check */
-  LOG(log_debug, logtype_default, "Flooding 12x1");
-  for (int i = 0; i < 12; i++) {
-      LOG(log_error, logtype_default, "flooding 12x1 1");
+  LOG(log_debug, logtype_default, "Flooding 3x");
+  for (int i = 0; i < 3; i++) {
+      LOG(log_debug, logtype_default, "Flooding...");
   }
+  LOG(log_debug, logtype_default, "1");
+  LOG(log_debug, logtype_default, "2");
+  LOG(log_debug, logtype_default, "3");
 
-  LOG(log_debug, logtype_default, "=============");
-  LOG(log_debug, logtype_default, "Flooding 12x3");
-  for (int i = 0; i < 12; i++) {
-      LOG(log_error, logtype_default, "flooding 12x3 1");
-      LOG(log_error, logtype_default, "flooding 12x3 2");
-      LOG(log_error, logtype_default, "flooding 12x3 3");
+  LOG(log_debug, logtype_default, "0============");
+  LOG(log_debug, logtype_default, "Flooding 11x1");
+  for (int i = 0; i < 11; i++) {
+      LOG(log_error, logtype_default, "flooding 11x1 1");
   }
 
-  LOG(log_debug, logtype_default, "=============");
-  LOG(log_debug, logtype_default, "Flooding 12x4");
-  for (int i = 0; i < 12; i++) {
-      LOG(log_error, logtype_default, "flooding 12x4 1");
-      LOG(log_error, logtype_default, "flooding 12x4 2");
-      LOG(log_error, logtype_default, "flooding 12x4 3");
-      LOG(log_error, logtype_default, "flooding 12x4 4");
+  LOG(log_debug, logtype_default, "1============");
+  LOG(log_debug, logtype_default, "Flooding 11x2");
+  for (int i = 0; i < 11; i++) {
+      LOG(log_error, logtype_default, "flooding 11x2 1");
+      LOG(log_error, logtype_default, "flooding 11x2 2");
   }
 
-  LOG(log_debug, logtype_default, "=============");
-  LOG(log_debug, logtype_default, "Flooding 12x5");
-  for (int i = 0; i < 12; i++) {
-      LOG(log_error, logtype_default, "flooding 12x5 1");
-      LOG(log_error, logtype_default, "flooding 12x5 2");
-      LOG(log_error, logtype_default, "flooding 12x5 3");
-      LOG(log_error, logtype_default, "flooding 12x5 4");
-      LOG(log_error, logtype_default, "flooding 12x5 5");
+  LOG(log_debug, logtype_default, "2============");
+  LOG(log_debug, logtype_default, "Flooding 11x3");
+  for (int i = 0; i < 11; i++) {
+      LOG(log_error, logtype_default, "flooding 11x3 1");
+      LOG(log_error, logtype_default, "flooding 11x3 2");
+      LOG(log_error, logtype_default, "flooding 11x3 3");
   }
 
+  LOG(log_debug, logtype_default, "3============");
+  LOG(log_debug, logtype_default, "Flooding 11x4");
+  for (int i = 0; i < 11; i++) {
+      LOG(log_error, logtype_default, "flooding 11x4 1");
+      LOG(log_error, logtype_default, "flooding 11x4 2");
+      LOG(log_error, logtype_default, "flooding 11x4 3");
+      LOG(log_error, logtype_default, "flooding 11x4 4");
+  }
+
+
   return 0;
 }