From 2ff6cdd0dbbdd5adc8847eff6d647b5744a0ca86 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Fri, 12 Apr 2013 17:50:03 +0300 Subject: [PATCH] resource-native: let clients override default logging. --- src/Makefile.am | 3 +- .../resource-native/libmurphy-resource/message.c | 20 +++---- .../libmurphy-resource/resource-api.h | 30 ++++++++++ .../libmurphy-resource/resource-log.c | 70 ++++++++++++++++++++++ .../libmurphy-resource/resource-private.h | 21 +++++++ .../resource-native/libmurphy-resource/resource.c | 42 ++++++------- .../resource-native/libmurphy-resource/rset.c | 20 +++---- 7 files changed, 164 insertions(+), 42 deletions(-) create mode 100644 src/plugins/resource-native/libmurphy-resource/resource-log.c diff --git a/src/Makefile.am b/src/Makefile.am index 839dade..a071494 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -912,7 +912,8 @@ libmurphy_resource_la_SOURCES = \ plugins/resource-native/libmurphy-resource/attribute.c \ plugins/resource-native/libmurphy-resource/rset.c \ plugins/resource-native/libmurphy-resource/string_array.c \ - plugins/resource-native/libmurphy-resource/message.c + plugins/resource-native/libmurphy-resource/message.c \ + plugins/resource-native/libmurphy-resource/resource-log.c libmurphy_resource_la_CFLAGS = \ $(AM_CFLAGS) diff --git a/src/plugins/resource-native/libmurphy-resource/message.c b/src/plugins/resource-native/libmurphy-resource/message.c index 084ad31..68e7e45 100644 --- a/src/plugins/resource-native/libmurphy-resource/message.c +++ b/src/plugins/resource-native/libmurphy-resource/message.c @@ -287,7 +287,7 @@ mrp_res_resource_set_t *resource_query_response(mrp_msg_t *msg, goto failed; if (status != 0) - mrp_log_error("Resource query failed (%u): %s", status, strerror(status)); + mrp_res_error("Resource query failed (%u): %s", status, strerror(status)); else { dim = 0; @@ -299,7 +299,7 @@ mrp_res_resource_set_t *resource_query_response(mrp_msg_t *msg, goto failed; if (!(rdef[dim].attrs = mrp_attribute_array_dup(n_attrs, attrs))) { - mrp_log_error("failed to duplicate attributes"); + mrp_res_error("failed to duplicate attributes"); goto failed; } @@ -348,7 +348,7 @@ mrp_res_resource_set_t *resource_query_response(mrp_msg_t *msg, return arr; failed: - mrp_log_error("malformed reply to resource query"); + mrp_res_error("malformed reply to resource query"); free_resource_set(arr); return NULL; @@ -363,12 +363,12 @@ mrp_res_string_array_t *class_query_response(mrp_msg_t *msg, void **pcursor) if (!fetch_status(msg, pcursor, &status) || (status == 0 && !fetch_mrp_str_array(msg, pcursor, RESPROTO_CLASS_NAME, &arr))) { - mrp_log_error("ignoring malformed response to class query"); + mrp_res_error("ignoring malformed response to class query"); return NULL; } if (status) { - mrp_log_error("class query failed with error code %u", status); + mrp_res_error("class query failed with error code %u", status); mrp_res_free_string_array(arr); return NULL; } @@ -386,12 +386,12 @@ bool create_resource_set_response(mrp_msg_t *msg, if (!fetch_status(msg, pcursor, &status) || (status == 0 && !fetch_resource_set_id(msg, pcursor, &rset_id))) { - mrp_log_error("ignoring malformed response to resource set creation"); + mrp_res_error("ignoring malformed response to resource set creation"); goto error; } if (status) { - mrp_log_error("creation of resource set failed. error code %u",status); + mrp_res_error("creation of resource set failed. error code %u",status); goto error; } @@ -413,12 +413,12 @@ mrp_res_resource_set_t *acquire_resource_set_response(mrp_msg_t *msg, if (!fetch_resource_set_id(msg, pcursor, &rset_id) || !fetch_status(msg, pcursor, &status)) { - mrp_log_error("ignoring malformed response to resource set"); + mrp_res_error("ignoring malformed response to resource set"); goto error; } if (status) { - mrp_log_error("acquiring of resource set failed. error code %u",status); + mrp_res_error("acquiring of resource set failed. error code %u",status); goto error; } @@ -428,7 +428,7 @@ mrp_res_resource_set_t *acquire_resource_set_response(mrp_msg_t *msg, rset = mrp_htbl_lookup(cx->priv->rset_mapping, u_to_p(rset_id)); if (!rset) { - mrp_log_error("no rset found!"); + mrp_res_error("no rset found!"); goto error; } diff --git a/src/plugins/resource-native/libmurphy-resource/resource-api.h b/src/plugins/resource-native/libmurphy-resource/resource-api.h index d7fa2f9..6da1da6 100644 --- a/src/plugins/resource-native/libmurphy-resource/resource-api.h +++ b/src/plugins/resource-native/libmurphy-resource/resource-api.h @@ -30,6 +30,8 @@ #ifndef __MURPHY_RESOURCE_API_H__ #define __MURPHY_RESOURCE_API_H__ +#include + #include typedef struct mrp_res_context_private_s mrp_res_context_private_t; @@ -385,4 +387,32 @@ int mrp_res_set_attribute_double(mrp_res_context_t *cx, */ void mrp_res_free_string_array(mrp_res_string_array_t *arr); + +/** + * Prototype for an external logger. + * + * @param level log level. + * @param file source file (__FILE__) he log message originated from. + * @param line source line (__LINE__) the log message originated from. + * @param func function (__func__) the log message originated from. + * + * @return none. + */ +typedef void (*mrp_res_logger_t) (mrp_log_level_t level, const char *file, + int line, const char *func, + const char *format, va_list args); + +/** + * Set an external logger for the resource library. All log messages + * produced by the library will be handed to this function. If you + * want to suppress all logs by the library, set the logger to NULL. + * + * @param logger the logger function to use. + * + * @return pointer to the previously active logger function. + */ + +mrp_res_logger_t mrp_res_set_logger(mrp_res_logger_t logger); + + #endif /* __MURPHY_RESOURCE_API_H__ */ diff --git a/src/plugins/resource-native/libmurphy-resource/resource-log.c b/src/plugins/resource-native/libmurphy-resource/resource-log.c new file mode 100644 index 0000000..d5433f9 --- /dev/null +++ b/src/plugins/resource-native/libmurphy-resource/resource-log.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2012, 2013 Intel Corporation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "resource-api.h" +#include "resource-private.h" + +static void default_logger(mrp_log_level_t level, const char *file, + int line, const char *func, + const char *format, va_list args) +{ + va_list ap; + + va_copy(ap, args); + mrp_log_msgv(level, file, line, func, format, ap); + va_end(ap); +} + + +static mrp_res_logger_t __res_logger = default_logger; + +mrp_res_logger_t mrp_res_set_logger(mrp_res_logger_t logger) +{ + mrp_res_logger_t old = __res_logger; + + __res_logger = logger; + + return old; +} + + +void mrp_res_log_msg(mrp_log_level_t level, const char *file, + int line, const char *func, const char *format, ...) +{ + va_list ap; + + if (__res_logger != NULL) { + va_start(ap, format); + __res_logger(level, file, line, func, format, ap); + va_end(ap); + } +} diff --git a/src/plugins/resource-native/libmurphy-resource/resource-private.h b/src/plugins/resource-native/libmurphy-resource/resource-private.h index de7eb4a..abdb74b 100644 --- a/src/plugins/resource-native/libmurphy-resource/resource-private.h +++ b/src/plugins/resource-native/libmurphy-resource/resource-private.h @@ -30,6 +30,10 @@ #ifndef __MURPHY_RESOURCE_API_PRIVATE_H__ #define __MURPHY_RESOURCE_API_PRIVATE_H__ +#include + +#include + #include "resource-api.h" typedef struct { @@ -118,5 +122,22 @@ struct mrp_res_context_private_s { uint32_t p_to_u(const void *p); void *u_to_p(uint32_t u); +/* + * logging macros + */ + +#define __LOCATION__ __FILE__,__LINE__,__FUNCTION__ + +#define mrp_res_info(format, args...) \ + mrp_res_log_msg(MRP_LOG_INFO, __LOCATION__, format, ## args) + +#define mrp_res_warning(format, args...) \ + mrp_res_log_msg(MRP_LOG_WARNING, __LOCATION__, format, ## args) + +#define mrp_res_error(format, args...) \ + mrp_res_log_msg(MRP_LOG_ERROR, __LOCATION__, format, ## args) + +void mrp_res_log_msg(mrp_log_level_t level, const char *file, int line, + const char *func, const char *format, ...); #endif /* __MURPHY_RESOURCE_API_PRIVATE_H__ */ diff --git a/src/plugins/resource-native/libmurphy-resource/resource.c b/src/plugins/resource-native/libmurphy-resource/resource.c index 56c99be..d719a21 100644 --- a/src/plugins/resource-native/libmurphy-resource/resource.c +++ b/src/plugins/resource-native/libmurphy-resource/resource.c @@ -103,13 +103,13 @@ static void resource_event(mrp_msg_t *msg, uint32_t i; mrp_res_resource_set_t *rset; - printf("Resource event (request no %u):\n", seqno); + mrp_res_info("Resource event (request no %u):", seqno); if (!fetch_resource_set_id(msg, pcursor, &rset_id) || !fetch_resource_set_state(msg, pcursor, &state) || !fetch_resource_set_mask(msg, pcursor, 0, &grant) || !fetch_resource_set_mask(msg, pcursor, 1, &advice)) { - mrp_log_error("failed to fetch data from message"); + mrp_res_error("failed to fetch data from message"); goto malformed; } @@ -119,7 +119,7 @@ static void resource_event(mrp_msg_t *msg, rset = mrp_htbl_lookup(cx->priv->rset_mapping, u_to_p(rset_id)); if (!rset) { - mrp_log_error("resource event outside the resource set lifecycle"); + mrp_res_error("resource event outside the resource set lifecycle"); goto malformed; } @@ -140,25 +140,25 @@ static void resource_event(mrp_msg_t *msg, if ((tag != RESPROTO_RESOURCE_ID || type != MRP_MSG_FIELD_UINT32) || !fetch_resource_name(msg, pcursor, &resnam)) { - mrp_log_error("failed to read resource from message"); + mrp_res_error("failed to read resource from message"); goto malformed; } res = get_resource_by_name(rset, resnam); if (!res) { - mrp_log_error("resource doesn't exist in resource set"); + mrp_res_error("resource doesn't exist in resource set"); goto malformed; } resid = value.u32; - mrp_log_info("data for '%s': %d", res->name, resid); + mrp_res_info("data for '%s': %d", res->name, resid); n_attrs = fetch_attribute_array(msg, pcursor, ATTRIBUTE_MAX + 1, attrs); if (n_attrs < 0) { - mrp_log_error("failed to read attributes from message"); + mrp_res_error("failed to read attributes from message"); goto malformed; } @@ -194,7 +194,7 @@ static void resource_event(mrp_msg_t *msg, #endif } - mrp_log_info("advice = 0x%08x, grant = 0x%08x, mandatory = 0x%08x, all = 0x%08x", + mrp_res_info("advice = 0x%08x, grant = 0x%08x, mandatory = 0x%08x, all = 0x%08x", advice, grant, mandatory, all); if (grant) { @@ -225,7 +225,7 @@ static void resource_event(mrp_msg_t *msg, return; malformed: - mrp_log_error("ignoring malformed resource event"); + mrp_res_error("ignoring malformed resource event"); } @@ -247,14 +247,14 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, fetch_request(msg, &cursor, &req) < 0) goto error; - mrp_log_info("received message %d for %p", req, cx); + mrp_res_info("received message %d for %p", req, cx); err = MRP_RES_ERROR_MALFORMED; switch (req) { case RESPROTO_QUERY_RESOURCES: - mrp_log_info("received QUERY_RESOURCES response"); + mrp_res_info("received QUERY_RESOURCES response"); cx->priv->master_resource_set = resource_query_response(msg, &cursor); @@ -263,7 +263,7 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, break; case RESPROTO_QUERY_CLASSES: - mrp_log_info("received QUERY_CLASSES response"); + mrp_res_info("received QUERY_CLASSES response"); cx->priv->master_classes = class_query_response(msg, &cursor); if (!cx->priv->master_classes) @@ -275,7 +275,7 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, mrp_res_resource_set_t *rset = NULL; mrp_list_hook_t *p, *n; - mrp_log_info("received CREATE_RESOURCE_SET response"); + mrp_res_info("received CREATE_RESOURCE_SET response"); /* get the correct resource set from the pending_sets list */ @@ -310,7 +310,7 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, { mrp_res_resource_set_t *rset; - mrp_log_info("received ACQUIRE_RESOURCE_SET response"); + mrp_res_info("received ACQUIRE_RESOURCE_SET response"); rset = acquire_resource_set_response(msg, cx, &cursor); @@ -325,7 +325,7 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, case RESPROTO_RELEASE_RESOURCE_SET: { mrp_res_resource_set_t *rset; - mrp_log_info("received RELEASE_RESOURCE_SET response"); + mrp_res_info("received RELEASE_RESOURCE_SET response"); rset = acquire_resource_set_response(msg, cx, &cursor); @@ -339,12 +339,12 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, break; } case RESPROTO_RESOURCES_EVENT: - mrp_log_info("received RESOURCES_EVENT response"); + mrp_res_info("received RESOURCES_EVENT response"); resource_event(msg, cx, seqno, &cursor); break; case RESPROTO_DESTROY_RESOURCE_SET: - mrp_log_info("received DESTROY_RESOURCE_SET response"); + mrp_res_info("received DESTROY_RESOURCE_SET response"); /* TODO? */ break; default: @@ -361,7 +361,7 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, return; error: - mrp_log_error("error processing a message from the server"); + mrp_res_error("error processing a message from the server"); cx->priv->cb(cx, err, cx->priv->user_data); } @@ -378,7 +378,7 @@ void closed_evt(mrp_transport_t *transp, int error, void *user_data) MRP_UNUSED(transp); MRP_UNUSED(error); - mrp_log_error("connection closed for %p", cx); + mrp_res_error("connection closed for %p", cx); cx->priv->connected = FALSE; if (cx->state == MRP_RES_CONNECTED) { @@ -419,7 +419,7 @@ static void destroy_context(mrp_res_context_t *cx) static void htbl_free_rset_mapping(void *key, void *object) { #if 0 - mrp_log_info("> htbl_free_rset_mapping(%d, %p)", p_to_u(key), object); + mrp_res_info("> htbl_free_rset_mapping(%d, %p)", p_to_u(key), object); #else MRP_UNUSED(key); #endif @@ -515,7 +515,7 @@ mrp_res_context_t *mrp_res_create(mrp_mainloop_t *ml, error: - mrp_log_error("error connecting to server"); + mrp_res_error("error connecting to server"); destroy_context(cx); return NULL; diff --git a/src/plugins/resource-native/libmurphy-resource/rset.c b/src/plugins/resource-native/libmurphy-resource/rset.c index 5e83463..919e423 100644 --- a/src/plugins/resource-native/libmurphy-resource/rset.c +++ b/src/plugins/resource-native/libmurphy-resource/rset.c @@ -57,7 +57,7 @@ static char *state_to_str(mrp_res_resource_state_t st) void print_resource(mrp_res_resource_t *res) { - mrp_log_info(" resource '%s' -> '%s' : %smandatory, %sshared", + mrp_res_info(" resource '%s' -> '%s' : %smandatory, %sshared", res->name, state_to_str(res->state), res->priv->mandatory ? " " : "not ", res->priv->shared ? "" : "not "); @@ -69,7 +69,7 @@ void print_resource_set(mrp_res_resource_set_t *rset) uint32_t i; mrp_res_resource_t *res; - mrp_log_info("Resource set %i/%i (%s) -> '%s':", + mrp_res_info("Resource set %i/%i (%s) -> '%s':", rset->priv->id, rset->priv->internal_id, rset->application_class, state_to_str(rset->state)); @@ -133,7 +133,6 @@ void decrease_ref(mrp_res_context_t *cx, rset->priv->internal_ref_count--; if (rset->priv->internal_ref_count == 0) { - mrp_log_info("delete the server resource set now"); destroy_resource_set_request(cx, rset); @@ -265,7 +264,7 @@ static mrp_res_resource_t *resource_copy(const mrp_res_resource_t *original, return copy; error: - mrp_log_error("failed to copy resource"); + mrp_res_error("failed to copy resource"); if (copy) { mrp_free((void *) copy->name); @@ -506,7 +505,7 @@ mrp_res_resource_t *mrp_res_create_resource(mrp_res_context_t *cx, return res; error: - mrp_log_error("error creating a resource"); + mrp_res_error("mrp_res_create_resource error"); free_resource(res); return NULL; @@ -571,7 +570,8 @@ int mrp_res_release_resource_set(mrp_res_context_t *cx, return release_resource_set_request(cx, internal_set); error: - mrp_log_error("error releasing a resource set"); + mrp_res_error("mrp_release_resources error"); + return -1; } @@ -714,7 +714,7 @@ int mrp_res_acquire_resource_set(mrp_res_context_t *cx, mrp_res_resource_set_t *rset; if (!cx->priv->connected) { - mrp_log_error("not connected to server"); + mrp_res_error("not connected to server"); goto error; } @@ -722,7 +722,7 @@ int mrp_res_acquire_resource_set(mrp_res_context_t *cx, u_to_p(original->priv->internal_id)); if (!rset) { - mrp_log_error("trying to acquire non-existent resource set"); + mrp_res_error("trying to acquire non-existent resource set"); goto error; } @@ -735,7 +735,7 @@ int mrp_res_acquire_resource_set(mrp_res_context_t *cx, if (rset->state == MRP_RES_RESOURCE_ACQUIRED) { /* already requested, updating is not supported yet */ - mrp_log_error("trying to re-acquire already acquired set"); + mrp_res_error("trying to re-acquire already acquired set"); /* TODO: when supported by backend * type = RESPROTO_UPDATE_RESOURCE_SET @@ -770,7 +770,7 @@ int mrp_res_acquire_resource_set(mrp_res_context_t *cx, } if (create_resource_set_request(cx, rset) < 0) { - mrp_log_error("creating resource set failed"); + mrp_res_error("creating resource set failed"); mrp_list_delete(&rset->priv->hook); goto error; } -- 2.7.4