api work
[oweals/gnunet.git] / src / util / common_logging.c
index 4068ed94ba03182c3da6e346fa3c80b9b72a9ebf..a1a60b800bd23395871cc96215217ee6e16a6a32 100644 (file)
@@ -123,6 +123,8 @@ static struct CustomLogger *loggers;
  */
 static unsigned int skip_log;
 
+static FILE *GNUNET_stderr;
+
 /**
  * Convert a textual description of a loglevel
  * to the respective GNUNET_GE_KIND.
@@ -163,9 +165,9 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", logfile);
       return GNUNET_SYSERR;
     }
-  if (stderr != NULL)
-    fclose (stderr);
-  stderr = altlog;
+  if (GNUNET_stderr != NULL)
+    fclose (GNUNET_stderr);
+  GNUNET_stderr = altlog;
   return GNUNET_OK;
 }
 
@@ -220,8 +222,8 @@ output_message (enum GNUNET_ErrorType kind,
                 const char *comp, const char *datestr, const char *msg)
 {
   struct CustomLogger *pos;
-  if (stderr != NULL)
-    fprintf (stderr, "%s %s %s %s", datestr, comp,
+  if (GNUNET_stderr != NULL)
+    fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
              GNUNET_error_type_to_string (kind), msg);
   pos = loggers;
   while (pos != NULL)
@@ -378,6 +380,24 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
 }
 
 
+/**
+ * Convert a hash to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param pid the peer identity
+ * @return string form; will be overwritten by next call to GNUNET_h2s.
+ */
+const char *
+GNUNET_h2s (const GNUNET_HashCode *pid)
+{
+  static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
+  GNUNET_CRYPTO_hash_to_enc (pid, &ret);
+  ret.encoding[8] = '\0';
+  return (const char *) ret.encoding;
+}
+
+
 /**
  * Convert a peer identity to a string (for printing debug messages).
  * This is one of the very few calls in the entire API that is
@@ -398,4 +418,60 @@ GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
 
 
 
+/**
+ * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
+ * (for printing debug messages).  This is one of the very few calls
+ * in the entire API that is NOT reentrant!
+ *
+ * @param addr the address
+ * @param addrlen the length of the address
+ * @return nicely formatted string for the address
+ *  will be overwritten by next call to GNUNET_a2s.
+ */
+const char *GNUNET_a2s (const struct sockaddr *addr,
+                       socklen_t addrlen)
+{
+  static char buf[INET6_ADDRSTRLEN+8];
+  static char b2[6];
+  const struct sockaddr_in * v4;
+  const struct sockaddr_in6 *v6;
+
+  if (addr == NULL)
+    return _("unknown address");
+  switch (addr->sa_family)
+    {
+    case AF_INET:
+      v4 = (const struct sockaddr_in*)addr;
+      inet_ntop(AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
+      if (0 == ntohs(v4->sin_port))
+       return buf;     
+      strcat (buf, ":");
+      sprintf (b2, "%u", ntohs(v4->sin_port));
+      strcat (buf, b2);
+      return buf;
+    case AF_INET6:
+      v6 = (const struct sockaddr_in6*)addr;
+      buf[0] = '[';
+      inet_ntop(AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
+      if (0 == ntohs(v6->sin6_port))
+       return &buf[1]; 
+      strcat (buf, "]:");
+      sprintf (b2, "%u", ntohs(v6->sin6_port));
+      strcat (buf, b2);
+      return buf;      
+    default:
+      return _("invalid address");
+    }
+}
+
+
+/**
+ * Initializer
+ */
+void __attribute__ ((constructor))
+GNUNET_util_cl_init()
+{
+  GNUNET_stderr = stderr;
+}
+
 /* end of common_logging.c */