X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=include%2Fafb%2Fafb-req-itf.h;h=dcbbb8a768603a0ddf1f1190b3c1a6bea0ad02d5;hb=d3c80685ed6067f4899635c0328bbd8b70a2ed90;hp=34d73dd2a324bdfa3c047213a7040660ab4dd1c5;hpb=6f1126ae2c585afc34d0bb06f3763e3a82ee3d37;p=src%2Fapp-framework-binder.git diff --git a/include/afb/afb-req-itf.h b/include/afb/afb-req-itf.h index 34d73dd2..dcbbb8a7 100644 --- a/include/afb/afb-req-itf.h +++ b/include/afb/afb-req-itf.h @@ -17,13 +17,7 @@ #pragma once -#if !defined(_GNU_SOURCE) -# error "_GNU_SOURCE must be defined for using vasprintf" -#endif - -#include #include -#include #include "afb-event-itf.h" @@ -33,7 +27,8 @@ struct json_object; /* * Describes an argument (or parameter) of a request */ -struct afb_arg { +struct afb_arg +{ const char *name; /* name of the argument or NULL if invalid */ const char *value; /* string representation of the value of the argument */ /* original filename of the argument if path != NULL */ @@ -47,7 +42,8 @@ struct afb_arg { * Don't use this structure directly. * Use the helper functions documented below. */ -struct afb_req_itf { +struct afb_req_itf +{ /* CAUTION: respect the order, add at the end */ struct json_object *(*json)(void *closure); @@ -56,8 +52,8 @@ struct afb_req_itf { void (*success)(void *closure, struct json_object *obj, const char *info); void (*fail)(void *closure, const char *status, const char *info); - /*legacy raw */void (*spare1)(void *closure); - /*legacy send*/void (*spare2)(void *closure); + void (*vsuccess)(void *closure, struct json_object *obj, const char *fmt, va_list args); + void (*vfail)(void *closure, const char *status, const char *fmt, va_list args); void *(*context_get)(void *closure); void (*context_set)(void *closure, void *value, void (*free_value)(void*)); @@ -73,12 +69,15 @@ struct afb_req_itf { void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure); int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result); + + void (*vverbose)(void*closure, int level, const char *file, int line, const char * func, const char *fmt, va_list args); }; /* * Describes the request by bindings from afb-daemon */ -struct afb_req { +struct afb_req +{ const struct afb_req_itf *itf; /* the interface to use */ void *closure; /* the closure argument for functions of 'itf' */ }; @@ -166,16 +165,26 @@ static inline void afb_req_success(struct afb_req req, struct json_object *obj, * Thus, in the case where 'obj' should remain available after * the function returns, the function 'json_object_get' shall be used. */ +static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4))); static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) { - char *message; va_list args; va_start(args, info); - if (info == NULL || vasprintf(&message, info, args) < 0) - message = NULL; + req.itf->vsuccess(req.closure, obj, info, args); va_end(args); - afb_req_success(req, obj, message); - free(message); +} + +/* + * Same as 'afb_req_success_f' but the arguments to the format 'info' + * are given as a variable argument list instance. + * + * For convenience, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args) +{ + req.itf->vsuccess(req.closure, obj, info, args); } /* @@ -186,10 +195,6 @@ static inline void afb_req_success_f(struct afb_req req, struct json_object *obj * Note that calling afb_req_fail("success", info) is equivalent * to call afb_req_success(NULL, info). Thus even if possible it * is strongly recommanded to NEVER use "success" for status. - * - * For convenience, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. */ static inline void afb_req_fail(struct afb_req req, const char *status, const char *info) { @@ -199,21 +204,23 @@ static inline void afb_req_fail(struct afb_req req, const char *status, const ch /* * Same as 'afb_req_fail' but the 'info' is a formatting * string followed by arguments. - * - * For convenience, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. */ +static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4))); static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) { - char *message; va_list args; va_start(args, info); - if (info == NULL || vasprintf(&message, info, args) < 0) - message = NULL; + req.itf->vfail(req.closure, status, info, args); va_end(args); - afb_req_fail(req, status, message); - free(message); +} + +/* + * Same as 'afb_req_fail_f' but the arguments to the format 'info' + * are given as a variable argument list instance. + */ +static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args) +{ + req.itf->vfail(req.closure, status, info, args); } /* @@ -388,3 +395,35 @@ static inline int afb_req_subcall_sync(struct afb_req req, const char *api, cons return req.itf->subcallsync(req.closure, api, verb, args, result); } +/* + * Send associated to 'req' a message described by 'fmt' and following parameters + * to the journal for the verbosity 'level'. + * + * 'file', 'line' and 'func' are indicators of position of the code in source files + * (see macros __FILE__, __LINE__ and __func__). + * + * 'level' is defined by syslog standard: + * EMERGENCY 0 System is unusable + * ALERT 1 Action must be taken immediately + * CRITICAL 2 Critical conditions + * ERROR 3 Error conditions + * WARNING 4 Warning conditions + * NOTICE 5 Normal but significant condition + * INFO 6 Informational + * DEBUG 7 Debug-level messages + */ +static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...) __attribute__((format(printf, 6, 7))); +static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + req.itf->vverbose(req.closure, level, file, line, func, fmt, args); + va_end(args); +} + +/* macro for setting file, line and function automatically */ +# if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS) +#define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,__FILE__,__LINE__,__func__,__VA_ARGS__) +#else +#define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,NULL,0,NULL,__VA_ARGS__) +#endif