Imported Upstream version 2.1.13
[platform/upstream/gpg2.git] / common / logging.c
index 2932c3d..b6bafc7 100644 (file)
@@ -54,7 +54,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <assert.h>
-
+/* #include <execinfo.h> */
 
 #define GNUPG_COMMON_NEED_AFLOCAL 1
 #include "util.h"
@@ -195,7 +195,7 @@ parse_portno (const char *str, unsigned short *r_port)
 }
 
 
-static ssize_t
+static gpgrt_ssize_t
 fun_writer (void *cookie_arg, const void *buffer, size_t size)
 {
   struct fun_cookie_s *cookie = cookie_arg;
@@ -204,7 +204,7 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size)
      avoids the ugly mix of fd and estream code.  */
 
   /* Note that we always try to reconnect to the socket but print
-     error messages only the first time an error occured.  If
+     error messages only the first time an error occurred.  If
      RUNNING_DETACHED is set we don't fall back to stderr and even do
      not print any error messages.  This is needed because detached
      processes often close stderr and by writing to file descriptor 2
@@ -391,11 +391,11 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size)
           DWORD nwritten;
 
           WriteFile ((HANDLE)cookie->fd, buffer, size, &nwritten, NULL);
-          return (ssize_t)size; /* Okay.  */
+          return (gpgrt_ssize_t)size; /* Okay.  */
         }
 #endif
       if (!writen (cookie->fd, buffer, size, cookie->is_socket))
-        return (ssize_t)size; /* Okay. */
+        return (gpgrt_ssize_t)size; /* Okay. */
     }
 
   if (!running_detached && cookie->fd != -1
@@ -415,7 +415,7 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size)
       log_socket = -1;
     }
 
-  return (ssize_t)size;
+  return (gpgrt_ssize_t)size;
 }
 
 
@@ -715,7 +715,21 @@ do_logv (int level, int ignore_arg_ptr, const char *fmt, va_list arg_ptr)
   if (fmt)
     {
       if (ignore_arg_ptr)
-        es_fputs_unlocked (fmt, logstream);
+        { /* This is used by log_string and comes with the extra
+           * feature that after a LF the next line is indent at the
+           * length of the prefix.  Note that we do not yet include
+           * the length of the timestamp and pid in the indent
+           * computation.  */
+          const char *p, *pend;
+
+          for (p = fmt; (pend = strchr (p, '\n')); p = pend+1)
+            es_fprintf_unlocked (logstream, "%*s%.*s",
+                                 (int)((p != fmt
+                                        && (with_prefix || force_prefixes))
+                                       ?strlen (prefix_buffer)+2:0), "",
+                                 (int)(pend - p)+1, p);
+          es_fputs_unlocked (p, logstream);
+        }
       else
         es_vfprintf_unlocked (logstream, fmt, arg_ptr);
       if (*fmt && fmt[strlen(fmt)-1] != '\n')
@@ -734,6 +748,19 @@ do_logv (int level, int ignore_arg_ptr, const char *fmt, va_list arg_ptr)
       if (missing_lf)
         es_putc_unlocked ('\n', logstream );
       es_funlockfile (logstream);
+      /* Using backtrace requires a configure test and to pass
+       * -rdynamic to gcc.  Thus we do not enable it now.  */
+      /* { */
+      /*   void *btbuf[20]; */
+      /*   int btidx, btlen; */
+      /*   char **btstr; */
+
+      /*   btlen = backtrace (btbuf, DIM (btbuf)); */
+      /*   btstr = backtrace_symbols (btbuf, btlen); */
+      /*   if (btstr) */
+      /*     for (btidx=0; btidx < btlen; btidx++) */
+      /*       log_debug ("[%d] %s\n", btidx, btstr[btidx]); */
+      /* } */
       abort ();
     }
   else
@@ -769,12 +796,13 @@ do_log_ignore_arg (int level, const char *str, ...)
 }
 
 
+/* Log STRING at LEVEL but indent from the second line on by the
+ * length of the prefix.  */
 void
 log_string (int level, const char *string)
 {
   /* We need a dummy arg_ptr, but there is no portable way to create
-     one.  So we call the do_logv function through a variadic wrapper.
-     MB: Why not just use "%s"?  */
+   * one.  So we call the do_logv function through a variadic wrapper. */
   do_log_ignore_arg (level, string);
 }
 
@@ -919,18 +947,37 @@ log_clock (const char *string)
 }
 
 
-#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
+#ifdef GPGRT_HAVE_MACRO_FUNCTION
 void
 bug_at( const char *file, int line, const char *func )
 {
-  log_log (GPGRT_LOG_BUG, ("... this is a bug (%s:%d:%s)\n"), file, line, func);
+  log_log (GPGRT_LOG_BUG, "... this is a bug (%s:%d:%s)\n", file, line, func);
   abort (); /* Never called; just to make the compiler happy.  */
 }
-#else
+#else /*!GPGRT_HAVE_MACRO_FUNCTION*/
 void
 bug_at( const char *file, int line )
 {
-  log_log (GPGRT_LOG_BUG, _("you found a bug ... (%s:%d)\n"), file, line);
+  log_log (GPGRT_LOG_BUG, "you found a bug ... (%s:%d)\n", file, line);
   abort (); /* Never called; just to make the compiler happy.  */
 }
-#endif
+#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/
+
+
+#ifdef GPGRT_HAVE_MACRO_FUNCTION
+void
+_log_assert (const char *expr, const char *file, int line, const char *func)
+{
+  log_log (GPGRT_LOG_BUG, "Assertion \"%s\" in %s failed (%s:%d)\n",
+           expr, func, file, line);
+  abort (); /* Never called; just to make the compiler happy.  */
+}
+#else /*!GPGRT_HAVE_MACRO_FUNCTION*/
+void
+_log_assert (const char *expr, const char *file, int line)
+{
+  log_log (GPGRT_LOG_BUG, "Assertion \"%s\" failed (%s:%d)\n",
+           file, line, func);
+  abort (); /* Never called; just to make the compiler happy.  */
+}
+#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/