gpgme-tool: Fix chain_id -> chain-id in KEYLIST XML.
[gpgme.git] / src / debug.c
index 6faf4aaf9e0683ee7e1ffaabcaba683a3e730b7d..56effa751869c3732319dbcb3f4ed085f4b2323a 100644 (file)
@@ -1,22 +1,22 @@
 /* debug.c - helpful output in desperate situations
    Copyright (C) 2000 Werner Koch (dd9jn)
    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH
+
    This file is part of GPGME.
 
    GPGME is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2.1 of
    the License, or (at your option) any later version.
-   
+
    GPGME is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
-   
+
    You should have received a copy of the GNU Lesser General Public
    License along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301, USA.  */
 
 #if HAVE_CONFIG_H
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 #include <ctype.h>
 #include <errno.h>
 #include <time.h>
 #ifndef HAVE_DOSISH_SYSTEM
+# ifdef HAVE_SYS_TYPES_H
 #  include <sys/types.h>
+# endif
+# ifdef HAVE_SYS_STAT_H
 #  include <sys/stat.h>
-#  include <fcntl.h>
+# endif
+# include <fcntl.h>
 #endif
 #include <assert.h>
 
@@ -54,6 +60,12 @@ static int debug_level;
 /* The output stream for the debug messages.  */
 static FILE *errfp;
 
+/* If not NULL, this malloced string is used instead of the
+   GPGME_DEBUG envvar.  It must have been set before the debug
+   subsystem has been initialized.  Using it later may or may not have
+   any effect.  */
+static char *envvar_override;
+
 \f
 #ifdef HAVE_TLS
 #define FRAME_NR
@@ -103,6 +115,19 @@ trim_spaces (char *str)
 }
 
 
+/* This is an internal function to set debug info.  The caller must
+   assure that this function is called only by one thread at a time.
+   The function may have no effect if called after the debug system
+   has been initialized.  Returns 0 on success.  */
+int
+_gpgme_debug_set_debug_envvar (const char *value)
+{
+  free (envvar_override);
+  envvar_override = strdup (value);
+  return !envvar_override;
+}
+
+
 static void
 debug_init (void)
 {
@@ -115,16 +140,25 @@ debug_init (void)
       char *e;
       const char *s1, *s2;;
 
+      if (envvar_override)
+        {
+          e = strdup (envvar_override);
+          free (envvar_override);
+          envvar_override = NULL;
+        }
+      else
+        {
 #ifdef HAVE_W32CE_SYSTEM
-      e = _gpgme_w32ce_get_debug_envvar ();
+          e = _gpgme_w32ce_get_debug_envvar ();
 #else /*!HAVE_W32CE_SYSTEM*/
-      err = _gpgme_getenv ("GPGME_DEBUG", &e);
-      if (err)
-       {
-         UNLOCK (debug_lock);
-         return;
-       }
+          err = _gpgme_getenv ("GPGME_DEBUG", &e);
+          if (err)
+            {
+              UNLOCK (debug_lock);
+              return;
+            }
 #endif /*!HAVE_W32CE_SYSTEM*/
+        }
 
       initialized = 1;
       errfp = stderr;
@@ -199,18 +233,28 @@ _gpgme_debug (int level, const char *format, ...)
   saved_errno = errno;
   if (debug_level < level)
     return;
-    
+
   va_start (arg_ptr, format);
   LOCK (debug_lock);
   {
+#ifdef HAVE_W32CE_SYSTEM
+    SYSTEMTIME t;
+
+    GetLocalTime (&t);
+    fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx>  ",
+            t.wYear, t.wMonth, t.wDay,
+            t.wHour, t.wMinute, t.wSecond,
+            (unsigned long long) ath_self ());
+#else
     struct tm *tp;
     time_t atime = time (NULL);
-    
+
     tp = localtime (&atime);
     fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx>  ",
             1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
             tp->tm_hour, tp->tm_min, tp->tm_sec,
             (unsigned long long) ath_self ());
+#endif
   }
 #ifdef FRAME_NR
   {
@@ -326,7 +370,7 @@ _gpgme_debug_buffer (int lvl, const char *const fmt,
       char str[51];
       char *strp = str;
       char *strp2 = &str[34];
-      
+
       for (j = 0; j < 16; j++)
        {
          unsigned char val;