Add log level and log callback support to the RAOP API
authorJuho Vähä-Herttua <juhovh@iki.fi>
Mon, 14 May 2012 10:53:51 +0000 (13:53 +0300)
committerJuho Vähä-Herttua <juhovh@iki.fi>
Wed, 16 May 2012 21:57:07 +0000 (00:57 +0300)
src/include/raop.h
src/lib/logger.c
src/lib/logger.h
src/lib/raop.c
src/test/example.c

index aa1a47f9d7376ec48470721da3444a64a534396b..1a7ebc2325ab43610a8069a8103cbbab89e293a2 100644 (file)
 extern "C" {
 #endif
 
+
+/* Define syslog style log levels */
+#define RAOP_LOG_EMERG       0       /* system is unusable */
+#define RAOP_LOG_ALERT       1       /* action must be taken immediately */
+#define RAOP_LOG_CRIT        2       /* critical conditions */
+#define RAOP_LOG_ERR         3       /* error conditions */
+#define RAOP_LOG_WARNING     4       /* warning conditions */
+#define RAOP_LOG_NOTICE      5       /* normal but significant condition */
+#define RAOP_LOG_INFO        6       /* informational */
+#define RAOP_LOG_DEBUG       7       /* debug-level messages */
+
+
 typedef struct raop_s raop_t;
 
+typedef void (*raop_log_callback_t)(int level, const char *msg);
+
 struct raop_callbacks_s {
        void* cls;
+
+       /* Compulsory callback functions */
        void* (*audio_init)(void *cls, int bits, int channels, int samplerate);
+       void  (*audio_process)(void *cls, void *session, const void *buffer, int buflen);
+       void  (*audio_destroy)(void *cls, void *session);
+
+       /* Optional but recommended callback functions */
+       void  (*audio_flush)(void *cls, void *session);
        void  (*audio_set_volume)(void *cls, void *session, float volume);
        void  (*audio_set_metadata)(void *cls, void *session, const void *buffer, int buflen);
        void  (*audio_set_coverart)(void *cls, void *session, const void *buffer, int buflen);
-       void  (*audio_process)(void *cls, void *session, const void *buffer, int buflen);
-       void  (*audio_flush)(void *cls, void *session);
-       void  (*audio_destroy)(void *cls, void *session);
 };
 typedef struct raop_callbacks_s raop_callbacks_t;
 
 RAOP_API raop_t *raop_init(raop_callbacks_t *callbacks, const char *pemkey);
 RAOP_API raop_t *raop_init_from_keyfile(raop_callbacks_t *callbacks, const char *keyfile);
 
-RAOP_API int raop_is_running(raop_t *raop);
+RAOP_API void raop_set_log_level(raop_t *raop, int level);
+RAOP_API void raop_set_log_callback(raop_t *raop, raop_log_callback_t callback);
 
 RAOP_API int raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen, const char *password);
+RAOP_API int raop_is_running(raop_t *raop);
 RAOP_API void raop_stop(raop_t *raop);
 
 RAOP_API void raop_destroy(raop_t *raop);
index fd87ef41f650e6306534b3570cf6083f2e41645e..451372dfddf9fcc9f5bb088f8adb74014daf56e2 100644 (file)
 #include "logger.h"
 #include "compat.h"
 
-void
-logger_init(logger_t *logger)
+struct logger_s {
+       mutex_handle_t lvl_mutex;
+       mutex_handle_t cb_mutex;
+
+       int level;
+       logger_callback_t callback;
+};
+
+logger_t *
+logger_init()
 {
+       logger_t *logger = calloc(1, sizeof(logger_t));
        assert(logger);
 
+       MUTEX_CREATE(logger->lvl_mutex);
+       MUTEX_CREATE(logger->cb_mutex);
+
        logger->level = LOGGER_DEBUG;
        logger->callback = NULL;
+       return logger;
+}
+
+void
+logger_destroy(logger_t *logger)
+{
+       MUTEX_DESTROY(logger->lvl_mutex);
+       MUTEX_DESTROY(logger->cb_mutex);
+       free(logger);
 }
 
 void
@@ -34,7 +55,9 @@ logger_set_level(logger_t *logger, int level)
 {
        assert(logger);
 
+       MUTEX_LOCK(logger->lvl_mutex);
        logger->level = level;
+       MUTEX_UNLOCK(logger->lvl_mutex);
 }
 
 void
@@ -42,7 +65,9 @@ logger_set_callback(logger_t *logger, logger_callback_t callback)
 {
        assert(logger);
 
+       MUTEX_LOCK(logger->cb_mutex);
        logger->callback = callback;
+       MUTEX_UNLOCK(logger->cb_mutex);
 }
 
 static char *
@@ -81,19 +106,26 @@ logger_log(logger_t *logger, int level, const char *fmt, ...)
        char buffer[4096];
        va_list ap;
 
-       if (level > logger->level)
+       MUTEX_LOCK(logger->lvl_mutex);
+       if (level > logger->level) {
+               MUTEX_UNLOCK(logger->lvl_mutex);
                return;
+       }
+       MUTEX_UNLOCK(logger->lvl_mutex);
 
        buffer[sizeof(buffer)-1] = '\0';
        va_start(ap, fmt);
        vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
        va_end(ap);
 
+       MUTEX_LOCK(logger->cb_mutex);
        if (logger->callback) {
                logger->callback(level, buffer);
+               MUTEX_UNLOCK(logger->cb_mutex);
        } else {
-               char *local = logger_utf8_to_local(buffer);
-
+               char *local;
+               MUTEX_UNLOCK(logger->cb_mutex);
+               local = logger_utf8_to_local(buffer);
                if (local) {
                        fprintf(stderr, "%s\n", local);
                        free(local);
index 52c15e4815428ede40a505b3d1fd6e81c9400f15..45e8b594db80d00faeabda48f60fa821afa97ec5 100644 (file)
 #define LOGGER_INFO        6       /* informational */
 #define LOGGER_DEBUG       7       /* debug-level messages */
 
-typedef void (*logger_callback_t)(int level, char *msg);
+typedef void (*logger_callback_t)(int level, const char *msg);
 
-struct logger_s {
-       int level;
-       logger_callback_t callback;
-};
 typedef struct logger_s logger_t;
 
-void logger_init(logger_t *logger);
+logger_t *logger_init();
+void logger_destroy(logger_t *logger);
+
 void logger_set_level(logger_t *logger, int level);
 void logger_set_callback(logger_t *logger, logger_callback_t callback);
 
index 0d2b11fa3f522d7583d663e782c852f69c710a4d..9ede243911c58bdd9876751085e63d136925a864 100644 (file)
@@ -44,7 +44,7 @@ struct raop_s {
        raop_callbacks_t callbacks;
 
        /* Logger instance */
-       logger_t logger;
+       logger_t *logger;
 
        /* HTTP daemon and RSA key */
        httpd_t *httpd;
@@ -85,21 +85,21 @@ conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remot
        conn->raop_rtp = NULL;
 
        if (locallen == 4) {
-               logger_log(&conn->raop->logger, LOGGER_INFO,
+               logger_log(conn->raop->logger, LOGGER_INFO,
                           "Local: %d.%d.%d.%d",
                           local[0], local[1], local[2], local[3]);
        } else if (locallen == 16) {
-               logger_log(&conn->raop->logger, LOGGER_INFO,
+               logger_log(conn->raop->logger, LOGGER_INFO,
                           "Local: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
                           local[0], local[1], local[2], local[3], local[4], local[5], local[6], local[7],
                           local[8], local[9], local[10], local[11], local[12], local[13], local[14], local[15]);
        }
        if (remotelen == 4) {
-               logger_log(&conn->raop->logger, LOGGER_INFO,
+               logger_log(conn->raop->logger, LOGGER_INFO,
                           "Remote: %d.%d.%d.%d",
                           remote[0], remote[1], remote[2], remote[3]);
        } else if (remotelen == 16) {
-               logger_log(&conn->raop->logger, LOGGER_INFO,
+               logger_log(conn->raop->logger, LOGGER_INFO,
                           "Remote: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
                           remote[0], remote[1], remote[2], remote[3], remote[4], remote[5], remote[6], remote[7],
                           remote[8], remote[9], remote[10], remote[11], remote[12], remote[13], remote[14], remote[15]);
@@ -144,8 +144,8 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
 
                authorization = http_request_get_header(request, "Authorization");
                if (authorization) {
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce);
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization);
                }
                if (!digest_is_valid("AppleTV", raop->password, conn->nonce, method, http_request_get_url(request), authorization)) {
                        char *authstr;
@@ -168,7 +168,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                        http_response_add_header(res, "WWW-Authenticate", authstr);
                        free(authstr);
                } else {
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "AUTHENTICATION SUCCESS!");
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "AUTHENTICATION SUCCESS!");
                }
        }
 
@@ -184,8 +184,8 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                            conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen);
                http_response_add_header(res, "Apple-Response", signature);
 
-               logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s", challenge);
-               logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got response: %s", signature);
+               logger_log(conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s", challenge);
+               logger_log(conn->raop->logger, LOGGER_DEBUG, "Got response: %s", signature);
        }
 
        if (require_auth) {
@@ -211,22 +211,22 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                        aeskeystr = sdp_get_rsaaeskey(sdp);
                        aesivstr = sdp_get_aesiv(sdp);
 
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "connection: %s", remotestr);
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "fmtp: %s", fmtpstr);
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s", aeskeystr);
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesiv: %s", aesivstr);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "connection: %s", remotestr);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "fmtp: %s", fmtpstr);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s", aeskeystr);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "aesiv: %s", aesivstr);
 
                        aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), aeskeystr);
                        aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), aesivstr);
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d", aeskeylen);
-                       logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d", aesivlen);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d", aeskeylen);
+                       logger_log(conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d", aesivlen);
 
                        if (conn->raop_rtp) {
                                /* This should never happen */
                                raop_rtp_destroy(conn->raop_rtp);
                                conn->raop_rtp = NULL;
                        }
-                       conn->raop_rtp = raop_rtp_init(&raop->logger, &raop->callbacks, remotestr, fmtpstr, aeskey, aesiv);
+                       conn->raop_rtp = raop_rtp_init(raop->logger, &raop->callbacks, remotestr, fmtpstr, aeskey, aesiv);
                        sdp_destroy(sdp);
                }
        } else if (!strcmp(method, "SETUP")) {
@@ -239,7 +239,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                transport = http_request_get_header(request, "Transport");
                assert(transport);
 
-               logger_log(&conn->raop->logger, LOGGER_INFO, "Transport: %s", transport);
+               logger_log(conn->raop->logger, LOGGER_INFO, "Transport: %s", transport);
                use_udp = strncmp(transport, "RTP/AVP/TCP", 11);
                if (use_udp) {
                        char *original, *current, *tmpstr;
@@ -252,12 +252,12 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
 
                                        ret = sscanf(tmpstr, "control_port=%hu", &value);
                                        if (ret == 1) {
-                                               logger_log(&conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu", value);
+                                               logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu", value);
                                                remote_cport = value;
                                        }
                                        ret = sscanf(tmpstr, "timing_port=%hu", &value);
                                        if (ret == 1) {
-                                               logger_log(&conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu", value);
+                                               logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu", value);
                                                remote_tport = value;
                                        }
                                }
@@ -276,7 +276,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                                 "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u",
                                 dport);
                }
-               logger_log(&conn->raop->logger, LOGGER_INFO, "Responding with %s", buffer);
+               logger_log(conn->raop->logger, LOGGER_INFO, "Responding with %s", buffer);
                http_response_add_header(res, "Transport", buffer);
                http_response_add_header(res, "Session", "DEADBEEF");
        } else if (!strcmp(method, "SET_PARAMETER")) {
@@ -299,10 +299,10 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
                        }
                        free(datastr);
                } else if (!strcmp(content_type, "image/jpeg")) {
-                       logger_log(&conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen);
+                       logger_log(conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen);
                        raop_rtp_set_coverart(conn->raop_rtp, data, datalen);
                } else if (!strcmp(content_type, "application/x-dmap-tagged")) {
-                       logger_log(&conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen);
+                       logger_log(conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen);
                        raop_rtp_set_metadata(conn->raop_rtp, data, datalen);
                }
        } else if (!strcmp(method, "FLUSH")) {
@@ -311,7 +311,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
 
                rtpinfo = http_request_get_header(request, "RTP-Info");
                if (rtpinfo) {
-                       logger_log(&conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s", rtpinfo);
+                       logger_log(conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s", rtpinfo);
                        if (!strncmp(rtpinfo, "seq=", 4)) {
                                next_seq = strtol(rtpinfo+4, NULL, 10);
                        }
@@ -330,7 +330,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response)
        }
        http_response_finish(res, NULL, 0);
 
-       logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s", method, http_request_get_url(request));
+       logger_log(conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s", method, http_request_get_url(request));
        *response = res;
 }
 
@@ -378,7 +378,7 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey)
        }
 
        /* Initialize the logger */
-       logger_init(&raop->logger);
+       raop->logger = logger_init();
 
        /* Set HTTP callbacks to our handlers */
        memset(&httpd_cbs, 0, sizeof(httpd_cbs));
@@ -388,7 +388,7 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey)
        httpd_cbs.conn_destroy = &conn_destroy;
 
        /* Initialize the http daemon */
-       httpd = httpd_init(&raop->logger, &httpd_cbs, 10, 1);
+       httpd = httpd_init(raop->logger, &httpd_cbs, 10, 1);
        if (!httpd) {
                free(raop);
                return NULL;
@@ -433,6 +433,7 @@ raop_destroy(raop_t *raop)
 
                httpd_destroy(raop->httpd);
                rsakey_destroy(raop->rsakey);
+               logger_destroy(raop->logger);
                free(raop);
 
                /* Cleanup the network */
@@ -448,6 +449,22 @@ raop_is_running(raop_t *raop)
        return httpd_is_running(raop->httpd);
 }
 
+void
+raop_set_log_level(raop_t *raop, int level)
+{
+       assert(raop);
+
+       logger_set_level(raop->logger, level);
+}
+
+void
+raop_set_log_callback(raop_t *raop, raop_log_callback_t callback)
+{
+       assert(raop);
+
+       logger_set_callback(raop->logger, callback);
+}
+
 int
 raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen, const char *password)
 {
index c1ae7f0150f4974f3bc5c8e139e3d8b6498d7cc1..c07b1268c6a2d271b3949028c18e98982f563140 100644 (file)
@@ -66,6 +66,12 @@ audio_destroy(void *cls, void *session)
        fclose(session);
 }
 
+static void
+raop_log_callback(int level, const char *msg)
+{
+       printf("RAOP LOG(%d): %s\n", level, msg);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -87,6 +93,8 @@ main(int argc, char *argv[])
        raop_cbs.audio_destroy = audio_destroy;
 
        raop = raop_init_from_keyfile(&raop_cbs, "airport.key");
+       raop_set_log_level(raop, RAOP_LOG_DEBUG);
+       raop_set_log_callback(raop, &raop_log_callback);
        raop_start(raop, &raop_port, hwaddr, sizeof(hwaddr), "test");
 
        dnssd = dnssd_init(NULL);