Debug: indent lookup operations
[exim.git] / src / src / debug.c
index 4b076514758996fb50a997a05425706a26411a43..eb62157def1dfc6a90d4830225bf2279cf0d288d 100644 (file)
@@ -14,6 +14,26 @@ static int     debug_prefix_length = 0;
 
 
 
+const uschar * rc_names[] = {          /* Mostly for debug output */
+  [OK] =               US"OK",
+  [DEFER] =            US"DEFER",
+  [FAIL] =             US"FAIL",
+  [ERROR] =            US"ERROR",
+  [FAIL_FORCED] =      US"FAIL_FORCED",
+  [DECLINE] =          US"DECLINE",
+  [PASS] =             US"PASS",
+  [DISCARD] =          US"DISCARD",
+  [SKIP] =             US"SKIP",
+  [REROUTED] =         US"REROUTED",
+  [PANIC] =            US"PANIC",
+  [BAD64] =            US"BAD64",
+  [UNEXPECTED] =       US"UNEXPECTED",
+  [CANCELLED] =                US"CANCELLED",
+  [FAIL_SEND] =                US"FAIL_SEND",
+  [FAIL_DROP] =                US"FAIL_DROP"
+};
+
+
 /*************************************************
 *               Print tree                       *
 *************************************************/
@@ -39,9 +59,8 @@ Returns:     nothing
 static void
 tree_printsub(tree_node *p, int pos, int barswitch)
 {
-int i;
 if (p->right) tree_printsub(p->right, pos+2, 1);
-for (i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]);
+for (int i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]);
 debug_printf("-->%s [%d]\n", p->name, p->balance);
 tree_printline[pos] = barswitch? '|' : ' ';
 if (p->left)
@@ -56,8 +75,7 @@ if (p->left)
 void
 debug_print_tree(tree_node *p)
 {
-int i;
-for (i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' ';
+for (int i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' ';
 if (!p) debug_printf("Empty Tree\n"); else tree_printsub(p, 0, 0);
 debug_printf("---- End of tree ----\n");
 }
@@ -129,6 +147,17 @@ debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s,
   (long int)getegid());
 }
 
+/************************************************/
+
+/* Give a string for a return-code */
+
+const uschar *
+rc_to_string(int rc)
+{
+return rc < 0 || rc >= nelem(rc_names) ? US"?" : rc_names[rc];
+}
+
+
 
 
 
@@ -210,8 +239,7 @@ if (debug_ptr == debug_buffer)
 
 if (indent > 0)
   {
-  int i;
-  for (i = indent >> 2; i > 0; i--)
+  for (int i = indent >> 2; i > 0; i--)
     DEBUG(D_noutf8)
       {
       Ustrcpy(debug_ptr, "   !");
@@ -233,19 +261,27 @@ if (indent > 0)
 /* Use the checked formatting routine to ensure that the buffer
 does not overflow. Ensure there's space for a newline at the end. */
 
-if (!string_vformat(debug_ptr,
-     sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1, format, ap))
   {
-  uschar *s = US"**** debug string too long - truncated ****\n";
-  uschar *p = debug_buffer + Ustrlen(debug_buffer);
-  int maxlen = sizeof(debug_buffer) - Ustrlen(s) - 3;
-  if (p > debug_buffer + maxlen) p = debug_buffer + maxlen;
-  if (p > debug_buffer && p[-1] != '\n') *p++ = '\n';
-  Ustrcpy(p, s);
+  gstring gs = { .size = (int)sizeof(debug_buffer) - 1,
+               .ptr = debug_ptr - debug_buffer,
+               .s = debug_buffer };
+  if (!string_vformat(&gs, FALSE, format, ap))
+    {
+    uschar * s = US"**** debug string too long - truncated ****\n";
+    uschar * p = gs.s + gs.ptr;
+    int maxlen = gs.size - Ustrlen(s) - 2;
+    if (p > gs.s + maxlen) p = gs.s + maxlen;
+    if (p > gs.s && p[-1] != '\n') *p++ = '\n';
+    Ustrcpy(p, s);
+    while(*debug_ptr) debug_ptr++;
+    }
+  else
+    {
+    string_from_gstring(&gs);
+    debug_ptr = gs.s + gs.ptr;
+    }
   }
 
-while(*debug_ptr != 0) debug_ptr++;
-
 /* Output the line if it is complete. If we added any prefix data and there
 are internal newlines, make sure the prefix is on the continuation lines,
 as long as there is room in the buffer. We want to do just a single fprintf()