]> git.tuebingen.mpg.de Git - osl.git/blobdiff - osl.c
Loglevel adjustments.
[osl.git] / osl.c
diff --git a/osl.c b/osl.c
index 957e9a7edefa7fb7ebda9ab2eff424482fcc4d77..f751302abc38a4327ed76c789954046279890592 100644 (file)
--- a/osl.c
+++ b/osl.c
@@ -10,6 +10,7 @@
 
 
 #include "log.h"
 
 
 #include "log.h"
+#include "osl.h"
 #include "error.h"
 #include "fd.h"
 #include "list.h"
 #include "error.h"
 #include "fd.h"
 #include "list.h"
@@ -60,6 +61,45 @@ static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ..
        return p;
 }
 
        return p;
 }
 
+/* Taken from Drepper: How to write shared libraries, Appendix B. */
+#include <stddef.h>
+#define MSGSTRFIELD(line) MSGSTRFIELD1(line)
+#define MSGSTRFIELD1(line) str##line
+static const union msgstr_t {
+       struct {
+#define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
+#include "errtab.h"
+#undef _S
+       };
+       char str[0];
+} msgstr = { {
+#define _S(n, s) s,
+#include "errtab.h"
+#undef _S
+} };
+static const unsigned int errmsgidx[] = {
+#define _S(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
+#include "errtab.h"
+#undef _S
+};
+
+__export const char *osl_strerror(int num)
+{
+       if (IS_SYSTEM_ERROR(num))
+               return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
+       return msgstr.str + errmsgidx[num];
+}
+
+static int loglevel;
+
+static void __attribute ((constructor)) init_loglevel(void)
+{
+       char *p = getenv("OSL_LOGLEVEL");
+
+       /* don't log anything if unset */
+       loglevel = p? atoi(p) : EMERG + 1;
+}
+
 /**
  * The log function.
  *
 /**
  * The log function.
  *
@@ -76,7 +116,7 @@ __printf_2_3 void __log(int ll, const char* fmt,...)
        time_t t1;
        char str[255] = "";
 
        time_t t1;
        char str[255] = "";
 
-       if (ll < 2)
+       if (ll < loglevel)
                return;
        outfd = stderr;
        time(&t1);
                return;
        outfd = stderr;
        time(&t1);
@@ -242,28 +282,6 @@ static int verify_name(const char *name)
        return 1;
 }
 
        return 1;
 }
 
-/**
- * Compare two osl objects pointing to unsigned integers of 32 bit size.
- *
- * \param obj1 Pointer to the first integer.
- * \param obj2 Pointer to the second integer.
- *
- * \return The values required for an osl compare function.
- *
- * \sa osl_compare_func, osl_hash_compare().
- */
-int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
-{
-       uint32_t d1 = read_u32((const char *)obj1->data);
-       uint32_t d2 = read_u32((const char *)obj2->data);
-
-       if (d1 < d2)
-               return 1;
-       if (d1 > d2)
-               return -1;
-       return 0;
-}
-
 /**
  * Compare two osl objects pointing to hash values.
  *
 /**
  * Compare two osl objects pointing to hash values.
  *
@@ -491,7 +509,7 @@ int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
                return -E_OSL_VERSION_MISMATCH;
        desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
        desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
                return -E_OSL_VERSION_MISMATCH;
        desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
        desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
-       DEBUG_LOG("%u columns\n", desc->num_columns);
+       INFO_LOG("%u columns\n", desc->num_columns);
        if (!desc->num_columns)
                return -E_OSL_NO_COLUMNS;
        header_size = read_u16(buf + IDX_HEADER_SIZE);
        if (!desc->num_columns)
                return -E_OSL_NO_COLUMNS;
        header_size = read_u16(buf + IDX_HEADER_SIZE);
@@ -596,7 +614,7 @@ static int compare_table_descriptions(struct osl_table *t)
                if (strcmp(cd1->name, cd2->name))
                        goto out;
        }
                if (strcmp(cd1->name, cd2->name))
                        goto out;
        }
-       DEBUG_LOG("table description of '%s' matches on-disk data, good\n",
+       INFO_LOG("table description of '%s' matches on-disk data, good\n",
                t->desc->name);
        ret = 1;
 out:
                t->desc->name);
        ret = 1;
 out:
@@ -765,7 +783,7 @@ int unmap_table(struct osl_table *t, enum osl_close_flags flags)
 
        if (!t->num_mapped_columns) /* can this ever happen? */
                return 1;
 
        if (!t->num_mapped_columns) /* can this ever happen? */
                return 1;
-       DEBUG_LOG("unmapping table '%s'\n", t->desc->name);
+       INFO_LOG("unmapping table '%s'\n", t->desc->name);
        if (!t->index_map.data)
                return -E_OSL_NOT_MAPPED;
        if (flags & OSL_MARK_CLEAN)
        if (!t->index_map.data)
                return -E_OSL_NOT_MAPPED;
        if (flags & OSL_MARK_CLEAN)
@@ -829,7 +847,7 @@ int map_table(struct osl_table *t, enum map_table_flags flags)
        filename = index_filename(t->desc);
        if (!filename)
                return -ERRNO_TO_ERROR(ENOMEM);
        filename = index_filename(t->desc);
        if (!filename)
                return -ERRNO_TO_ERROR(ENOMEM);
-       DEBUG_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
+       INFO_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
        ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
                O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
        free(filename);
        ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
                O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
        free(filename);
@@ -1113,6 +1131,7 @@ __export int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
 
        if (!t)
                return -E_OSL_BAD_TABLE;
 
        if (!t)
                return -E_OSL_BAD_TABLE;
+       NOTICE_LOG("closing table %s\n", t->desc->name);
        free_volatile_objects(t, flags);
        clear_rbtrees(t);
        ret = unmap_table(t, flags);
        free_volatile_objects(t, flags);
        clear_rbtrees(t);
        ret = unmap_table(t, flags);
@@ -1225,7 +1244,7 @@ __export int osl_open_table(const struct osl_table_description *table_desc,
        struct osl_table *t;
        const struct osl_column_description *cd;
 
        struct osl_table *t;
        const struct osl_column_description *cd;
 
-       INFO_LOG("opening table %s\n", table_desc->name);
+       NOTICE_LOG("opening table %s\n", table_desc->name);
        ret = init_table_structure(table_desc, &t);
        if (ret < 0)
                return ret;
        ret = init_table_structure(table_desc, &t);
        if (ret < 0)
                return ret;
@@ -1731,7 +1750,7 @@ __export int osl_get_row(const struct osl_table *t, unsigned col_num,
        return 1;
 }
 
        return 1;
 }
 
-static int rbtree_loop(struct osl_column *col,  void *private_data,
+static int rbtree_loop(struct osl_column *col, void *private_data,
                osl_rbtree_loop_func *func)
 {
        struct rb_node *n, *tmp;
                osl_rbtree_loop_func *func)
 {
        struct rb_node *n, *tmp;
@@ -1741,14 +1760,13 @@ static int rbtree_loop(struct osl_column *col,  void *private_data,
                        n;
                        n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
                struct osl_row *r = get_row_pointer(n, col->rbtree_num);
                        n;
                        n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
                struct osl_row *r = get_row_pointer(n, col->rbtree_num);
-               int ret = func(r, private_data);
-               if (ret < 0)
-                       return ret;
+               if (func(r, private_data) < 0)
+                       return -E_OSL_LOOP;
        }
        return 1;
 }
 
        }
        return 1;
 }
 
-static int rbtree_loop_reverse(struct osl_column *col,  void *private_data,
+static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
                osl_rbtree_loop_func *func)
 {
        struct rb_node *n, *tmp;
                osl_rbtree_loop_func *func)
 {
        struct rb_node *n, *tmp;
@@ -1758,9 +1776,8 @@ static int rbtree_loop_reverse(struct osl_column *col,  void *private_data,
                        n;
                        n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
                struct osl_row *r = get_row_pointer(n, col->rbtree_num);
                        n;
                        n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
                struct osl_row *r = get_row_pointer(n, col->rbtree_num);
-               int ret = func(r, private_data);
-               if (ret < 0)
-                       return ret;
+               if (func(r, private_data) < 0)
+                       return -E_OSL_LOOP;
        }
        return 1;
 }
        }
        return 1;
 }
@@ -1784,7 +1801,7 @@ static int rbtree_loop_reverse(struct osl_column *col,  void *private_data,
  *
  *
  * \return Standard. If the termination of the loop was caused by \a func
  *
  *
  * \return Standard. If the termination of the loop was caused by \a func
- * returning a negative value, this value is returned.
+ * returning a negative value, \p -E_OSL_LOOP is returned.
  *
  * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
  */
  *
  * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
  */