Allow user provided functions for printing messages
authorturran <turran>
Fri, 8 Aug 2008 10:39:47 +0000 (10:39 +0000)
committerturran <turran@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 8 Aug 2008 10:39:47 +0000 (10:39 +0000)
Make the stdout print mgs function exportable
Add a FILE * print msg function (useful for logging)

git-svn-id: http://svn.enlightenment.org/svn/e/trunk/e17/proto/eina@35404 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_error.h
src/lib/eina_error.c

index e6cbbc3..d1bc5e6 100644 (file)
@@ -19,6 +19,8 @@
 #ifndef EINA_ERROR_H_
 #define EINA_ERROR_H_
 
+#include <stdarg.h>
+
 #include "eina_types.h"
 
 #define EINA_ERROR_PERR(fmt, ...) \
@@ -43,6 +45,9 @@ typedef enum _Eina_Error_Level
 } Eina_Error_Level;
 
 typedef int Eina_Error;
+typedef void (*Eina_Error_Print_Cb)(Eina_Error_Level level, const char *file,
+                const char *fnc, int line, const char *fmt, void *data,
+               va_list args);
 
 EAPI extern int EINA_ERROR_OUT_OF_MEMORY;
 
@@ -54,6 +59,13 @@ EAPI void eina_error_set(Eina_Error err);
 EAPI const char * eina_error_msg_get(Eina_Error error);
 EAPI void eina_error_print(Eina_Error_Level level, const char *file,
                const char *function, int line, const char *fmt, ...);
+EAPI void eina_error_print_cb_stdout(Eina_Error_Level level, const char *file,
+                const char *fnc, int line, const char *fmt, void *data,
+               va_list args);
+EAPI void eina_error_print_cb_file(Eina_Error_Level level, const char *file,
+                const char *fnc, int line, const char *fmt, void *data,
+                va_list args);
+EAPI void eina_error_print_cb_set(Eina_Error_Print_Cb cb, void *data);
 EAPI void eina_error_log_level_set(Eina_Error_Level level);
 
 #endif /*EINA_ERROR_H_*/
index 35a5b11..10bca52 100644 (file)
@@ -33,6 +33,8 @@
 static int _init_count = 0;
 static Eina_List *_error_list;
 static Eina_Error _err;
+static Eina_Error_Print_Cb _print_cb = eina_error_print_cb_stdout;
+static void *_print_cb_data = NULL;
 
 #define RED     "\033[31;1m"
 #define GREEN   "\033[32;1m"
@@ -55,17 +57,7 @@ static char *_colors[EINA_ERROR_LEVELS] = {
        [EINA_ERROR_LEVEL_DBG] = GREEN,
 };
 
-static void _error_print(Eina_Error_Level level, const char *file,
-               const char *fnc, int line, const char *fmt, va_list args)
-{
-       if (level <= _error_level)
-       {
-               printf("%s", _colors[level]);
-               printf("[%s:%d] %s() ", file, line, fnc);
-               printf("%s", _colors[EINA_ERROR_LEVEL_INFO]);
-               vprintf(fmt, args);
-       }
-}
+
 /*============================================================================*
  *                                   API                                      * 
  *============================================================================*/
@@ -141,20 +133,53 @@ EAPI const char * eina_error_msg_get(int error)
        return eina_list_nth(_error_list, error - 1);
 }
 /**
- * 
+ *
  */
-EAPI void eina_error_print(Eina_Error_Level level, const char *file, 
+EAPI void eina_error_print(Eina_Error_Level level, const char *file,
                const char *fnc, int line, const char *fmt, ...)
 {
        va_list args;
 
        va_start(args, fmt);
-       _error_print(level, file, fnc, line, fmt, args);
+       if (level <= _error_level)
+               _print_cb(level, file, fnc, line, fmt, _print_cb_data, args);
        va_end(args);
 }
 /**
  *
  */
+EAPI void eina_error_print_cb_stdout(Eina_Error_Level level, const char *file,
+               const char *fnc, int line, const char *fmt, void *data,
+               va_list args)
+{
+       printf("%s", _colors[level]);
+       printf("[%s:%d] %s() ", file, line, fnc);
+       printf("%s", _colors[EINA_ERROR_LEVEL_INFO]);
+       vprintf(fmt, args);
+}
+/**
+ *
+ */
+EAPI void eina_error_print_cb_file(Eina_Error_Level level, const char *file,
+               const char *fnc, int line, const char *fmt, void *data,
+               va_list args)
+{
+       FILE *f = data;
+
+       fprintf(f, "[%s:%d] %s() ", file, line, fnc);
+       vfprintf(f, fmt, args);
+}
+/**
+ *
+ */
+EAPI void eina_error_print_cb_set(Eina_Error_Print_Cb cb, void *data)
+{
+       _print_cb = cb;
+       _print_cb_data = data;
+}
+/**
+ *
+ */
 EAPI void eina_error_log_level_set(Eina_Error_Level level)
 {
        _error_level = level;