[libdvbpsi-devel] [PATCH] Introduce dvbpsi_new2() which calls logging callback with extra context parameter

Andrey Utkin andrey.krieger.utkin at gmail.com
Sun May 26 20:09:01 CEST 2013


This does not break original dvbpsi_new() mechanism.
Rationale: when dvbpsi contexts are created in many objects, you may
need to figure out, which object is the owner.
---
 src/dvbpsi.c | 19 +++++++++++++++++++
 src/dvbpsi.h |  8 ++++++++
 2 files changed, 27 insertions(+)

diff --git a/src/dvbpsi.c b/src/dvbpsi.c
index 4d27720..e9a9ad9 100644
--- a/src/dvbpsi.c
+++ b/src/dvbpsi.c
@@ -60,6 +60,19 @@ dvbpsi_t *dvbpsi_new(dvbpsi_message_cb callback, enum dvbpsi_msg_level level)
     return p_dvbpsi;
 }
 
+dvbpsi_t *dvbpsi_new2(void *context, dvbpsi_message_cb2 callback, enum dvbpsi_msg_level level)
+{
+    dvbpsi_t *p_dvbpsi = calloc(1, sizeof(dvbpsi_t));
+    if (p_dvbpsi == NULL)
+        return NULL;
+
+    p_dvbpsi->p_decoder  = NULL;
+    p_dvbpsi->context = context;
+    p_dvbpsi->pf_message2 = callback;
+    p_dvbpsi->i_msg_level = level;
+    return p_dvbpsi;
+}
+
 /*****************************************************************************
  * dvbpsi_delete
  *****************************************************************************/
@@ -537,6 +550,8 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         if (err > DVBPSI_MSG_NONE) {
             if (dvbpsi->pf_message)
                 dvbpsi->pf_message(dvbpsi, level, msg);
+            else if (dvbpsi->pf_message2)
+                dvbpsi->pf_message2(dvbpsi->context, dvbpsi, level, msg);
         }
         free(msg);
     }
@@ -563,6 +578,8 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         if (err > 0) {                                          \
             if (dvbpsi->pf_message)                             \
                 dvbpsi->pf_message(dvbpsi, level, msg);         \
+            else if (dvbpsi->pf_message2)                       \
+                dvbpsi->pf_message2(dvbpsi->context, dvbpsi, level, msg);\
         }                                                       \
         free(msg);                                              \
     } while(0);
@@ -581,6 +598,8 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         if (err > 0) {                                          \
             if (dvbpsi->pf_message)                             \
                 dvbpsi->pf_message(dvbpsi, level, msg);         \
+            else if (dvbpsi->pf_message2)                       \
+                dvbpsi->pf_message2(dvbpsi->context, dvbpsi, level, msg);\
         }                                                       \
         free(msg);                                              \
     } while(0);
diff --git a/src/dvbpsi.h b/src/dvbpsi.h
index 062a9db..cb28b96 100644
--- a/src/dvbpsi.h
+++ b/src/dvbpsi.h
@@ -95,6 +95,11 @@ typedef void (* dvbpsi_message_cb)(dvbpsi_t *handle,
                                    const dvbpsi_msg_level_t level,
                                    const char* msg);
 
+typedef void (* dvbpsi_message_cb2)(void *context,
+                                    dvbpsi_t *handle,
+                                    const dvbpsi_msg_level_t level,
+                                    const char* msg);
+
 /*****************************************************************************
  * dvbpsi_decoder_t
  *****************************************************************************/
@@ -145,6 +150,8 @@ struct dvbpsi_s
                                                           specific decoder */
     /* Messages callback */
     dvbpsi_message_cb             pf_message;           /*!< Log message callback */
+    void                         *context;
+    dvbpsi_message_cb2            pf_message2;          /*!< Log message callback */
     enum dvbpsi_msg_level         i_msg_level;          /*!< Log level */
 
     /* private data pointer for use by caller, not by libdvbpsi itself ! */
@@ -169,6 +176,7 @@ struct dvbpsi_s
  * handle must be freed with dvbpsi_delete().
  */
 dvbpsi_t *dvbpsi_new(dvbpsi_message_cb callback, enum dvbpsi_msg_level level);
+dvbpsi_t *dvbpsi_new2(void *context, dvbpsi_message_cb2 callback, enum dvbpsi_msg_level level);
 
 /*****************************************************************************
  * dvbpsi_delete
-- 
1.8.1.5



More information about the libdvbpsi-devel mailing list