Deprecated log.h and created a new ylog.h
[yaz-moved-to-github.git] / src / odr.c
index 7a0dab1..964193e 100644 (file)
--- a/src/odr.c
+++ b/src/odr.c
@@ -2,9 +2,15 @@
  * Copyright (c) 1995-2004, Index Data
  * See the file LICENSE for details.
  *
- * $Id: odr.c,v 1.2 2004-08-11 12:15:38 adam Exp $
+ * $Id: odr.c,v 1.9 2004-11-18 15:18:13 heikki Exp $
  *
  */
+
+/**
+ * \file odr.c
+ * \brief Implements fundamental ODR functionality
+ */
+
 #if HAVE_CONFIG_H
 #include <config.h>
 #endif
 #include <stdarg.h>
 
 #include <yaz/xmalloc.h>
+#include <yaz/ylog.h>
 #include "odr-priv.h"
 
+static int log_level=0;
+static int log_level_initialized=0;
+
 Odr_null *ODR_NULLVAL = (Odr_null *) "NULL";  /* the presence of a null value */
 
 Odr_null *odr_nullval (void)
@@ -69,11 +79,16 @@ int odr_geterrorx(ODR o, int *x)
     return o->error;
 }
 
-char *odr_getelement(ODR o)
+const char *odr_getelement(ODR o)
 {
     return o->op->element;
 }
 
+const char **odr_get_element_path(ODR o)
+{
+    return o->op->stack_names;
+}
+
 void odr_seterror(ODR o, int error, int id)
 {
     o->error = error;
@@ -90,9 +105,36 @@ void odr_setelement(ODR o, const char *element)
     }
 }
 
-void odr_FILE_puts(void *handle, const char *strz)
+void odr_FILE_write(ODR o, void *handle, int type,
+                   const char *buf, int len)
 {
-    fputs(strz, (FILE*) handle);
+    int i;
+#if 0
+    if (type  == ODR_OCTETSTRING)
+    {
+       const char **stack_names = odr_get_element_path(o);
+       for (i = 0; stack_names[i]; i++)
+           fprintf((FILE*) handle, "[%s]", stack_names[i]);
+       fputs("\n", (FILE*) handle);
+    }
+#endif
+    for (i = 0; i<len; i++)
+    {
+       unsigned c = ((const unsigned char *) buf)[i];
+       if (i == 2000 && len > 3100)
+       {
+           fputs(" ..... ", (FILE*) handle);
+               i = len - 1000;
+       }
+       if (strchr("\r\n\f\t", c) || (c >= ' ' && c <= 126))
+           putc(c, (FILE*) handle);
+       else
+       {
+           char x[5];
+           sprintf(x, "\\X%02X", c);
+           fputs(x, (FILE*) handle);
+       }
+    }
 }
 
 void odr_FILE_close(void *handle)
@@ -104,15 +146,17 @@ void odr_FILE_close(void *handle)
 
 void odr_setprint(ODR o, FILE *file)
 {
-    odr_set_stream(o, file, odr_FILE_puts, odr_FILE_close);
+    odr_set_stream(o, file, odr_FILE_write, odr_FILE_close);
 }
 
 void odr_set_stream(ODR o, void *handle,
-                   void (*stream_puts)(void *handle, const char *strz),
+                   void (*stream_write)(ODR o, 
+                                        void *handle, int type,
+                                        const char *buf, int len),
                    void (*stream_close)(void *handle))
 {
-    o->print = handle;
-    o->op->stream_puts = stream_puts;
+    o->print = (FILE*) handle;
+    o->op->stream_write = stream_write;
     o->op->stream_close = stream_close;
 }
 
@@ -130,11 +174,15 @@ int odr_set_charset(ODR o, const char *to, const char *from)
     return 0;
 }
 
-#include <yaz/log.h>
 
 ODR odr_createmem(int direction)
 {
     ODR o;
+    if (!log_level_initialized)
+    {
+        log_level=yaz_log_module_level("odr");
+        log_level_initialized=1;
+    }
 
     if (!(o = (ODR)xmalloc(sizeof(*o))))
         return 0;
@@ -149,12 +197,18 @@ ODR odr_createmem(int direction)
     o->op->iconv_handle = 0;
     odr_setprint(o, stderr);
     odr_reset(o);
-    yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, o);
+    yaz_log (log_level, "odr_createmem dir=%d o=%p", direction, o);
     return o;
 }
 
 void odr_reset(ODR o)
 {
+    if (!log_level_initialized)
+    {
+        log_level=yaz_log_module_level("odr");
+        log_level_initialized=1;
+    }
+
     odr_seterror(o, ONONE, 0);
     o->bp = o->buf;
     odr_seek(o, ODR_S_SET, 0);
@@ -168,7 +222,7 @@ void odr_reset(ODR o)
     o->lenlen = 1;
     if (o->op->iconv_handle != 0)
         yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0);
-    yaz_log (LOG_DEBUG, "odr_reset o=%p", o);
+    yaz_log (log_level, "odr_reset o=%p", o);
 }
     
 void odr_destroy(ODR o)
@@ -182,11 +236,12 @@ void odr_destroy(ODR o)
         yaz_iconv_close (o->op->iconv_handle);
     xfree(o->op);
     xfree(o);
-    yaz_log (LOG_DEBUG, "odr_destroy o=%p", o);
+    yaz_log (log_level, "odr_destroy o=%p", o);
 }
 
 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
 {
+    odr_seterror(o, ONONE, 0);
     o->bp = (unsigned char *) buf;
 
     o->buf = (unsigned char *) buf;
@@ -218,6 +273,6 @@ void odr_printf(ODR o, const char *fmt, ...)
     vsprintf(buf, fmt, ap);
 #endif
 #endif
-    o->op->stream_puts(o->print, buf);
+    o->op->stream_write(o, o->print, ODR_VISIBLESTRING, buf, strlen(buf));
     va_end(ap);
 }