From d20efff495e52e53b5ed5fec894a1f5973450d01 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 4 Feb 2017 19:49:59 -0600 Subject: [PATCH] libpkgconf: overhaul pkgconf_trace() a little --- libpkgconf/client.c | 17 +++++++++++++---- libpkgconf/libpkgconf.h | 12 +++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libpkgconf/client.c b/libpkgconf/client.c index 2472d4e..97af90f 100644 --- a/libpkgconf/client.c +++ b/libpkgconf/client.c @@ -54,7 +54,8 @@ pkgconf_client_init(pkgconf_client_t *client, pkgconf_error_handler_func_t error pkgconf_client_set_error_handler(client, error_handler, error_handler_data); pkgconf_client_set_warn_handler(client, NULL, NULL); - pkgconf_client_set_trace_handler(client, NULL, NULL); + if (client->trace_handler == NULL) + pkgconf_client_set_trace_handler(client, NULL, NULL); pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_LIBRARY_PATH", SYSTEM_LIBDIR, &client->filter_libdirs, false); pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_INCLUDE_PATH", SYSTEM_INCLUDEDIR, &client->filter_includedirs, false); @@ -273,25 +274,33 @@ pkgconf_warn(const pkgconf_client_t *client, const char *format, ...) /* * !doc * - * .. c:function:: bool pkgconf_trace(const pkgconf_client_t *client, const char *format, ...) + * .. c:function:: bool pkgconf_trace(const pkgconf_client_t *client, const char *filename, size_t len, const char *funcname, const char *format, ...) * * Report a message to a client-registered trace handler. * * :param pkgconf_client_t* client: The pkgconf client object to report the trace message to. + * :param char* filename: The file the function is in. + * :param size_t lineno: The line number currently being executed. + * :param char* funcname: The function name to use. * :param char* format: A printf-style format string to use for formatting the trace message. * :return: true if the trace handler processed the message, else false. * :rtype: bool */ bool -pkgconf_trace(const pkgconf_client_t *client, const char *format, ...) +pkgconf_trace(const pkgconf_client_t *client, const char *filename, size_t lineno, const char *funcname, const char *format, ...) { char errbuf[PKGCONF_BUFSIZE]; + size_t len; va_list va; + len = snprintf(errbuf, sizeof errbuf, "%s:%zu [%s]: ", filename, lineno, funcname); + va_start(va, format); - vsnprintf(errbuf, sizeof errbuf, format, va); + vsnprintf(errbuf + len, sizeof(errbuf) - len, format, va); va_end(va); + pkgconf_strlcat(errbuf, "\n", sizeof errbuf); + return client->trace_handler(errbuf, client, client->trace_handler_data); } diff --git a/libpkgconf/libpkgconf.h b/libpkgconf/libpkgconf.h index c034927..8c0d695 100644 --- a/libpkgconf/libpkgconf.h +++ b/libpkgconf/libpkgconf.h @@ -218,9 +218,19 @@ void pkgconf_client_set_trace_handler(pkgconf_client_t *client, pkgconf_error_ha bool pkgconf_error(const pkgconf_client_t *client, const char *format, ...) PRINTFLIKE(2, 3); bool pkgconf_warn(const pkgconf_client_t *client, const char *format, ...) PRINTFLIKE(2, 3); -bool pkgconf_trace(const pkgconf_client_t *client, const char *format, ...) PRINTFLIKE(2, 3); +bool pkgconf_trace(const pkgconf_client_t *client, const char *filename, size_t lineno, const char *funcname, const char *format, ...) PRINTFLIKE(5, 6); bool pkgconf_default_error_handler(const char *msg, const pkgconf_client_t *client, const void *data); +#if defined(__GNUC__) || defined(__INTEL_COMPILER) +#define PKGCONF_TRACE(client, ...) do { \ + pkgconf_trace(client, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ + } while (0); +#else +#define PKGCONF_TRACE(client, ...) do { \ + pkgconf_trace(client, __FILE__, __LINE__, __func__, __VA_ARGS__); \ + } while (0); +#endif + pkgconf_pkg_t *pkgconf_pkg_ref(const pkgconf_client_t *client, pkgconf_pkg_t *pkg); void pkgconf_pkg_unref(pkgconf_client_t *client, pkgconf_pkg_t *pkg); void pkgconf_pkg_free(pkgconf_client_t *client, pkgconf_pkg_t *pkg);