Skip to content

Commit

Permalink
mgmt: Add DBG macro
Browse files Browse the repository at this point in the history
This adds mgmt_log wrapper for util_debug and DBG so file and function
names are printed with the logs.
  • Loading branch information
Vudentz committed Mar 23, 2022
1 parent a3c9409 commit f9cb7c8
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/shared/mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "src/shared/mgmt.h"
#include "src/shared/timeout.h"

#define DBG(_mgmt, _format, arg...) \
mgmt_log(_mgmt, "%s:%s() " _format, __FILE__, __func__, ## arg)

struct mgmt {
int ref_count;
int fd;
Expand Down Expand Up @@ -177,6 +180,18 @@ static bool request_timeout(void *data)
return false;
}

static void mgmt_log(struct mgmt *mgmt, const char *format, ...)
{
va_list ap;

if (!mgmt || !format || !mgmt->debug_callback)
return;

va_start(ap, format);
util_debug_va(mgmt->debug_callback, mgmt->debug_data, format, ap);
va_end(ap);
}

static bool send_request(struct mgmt *mgmt, struct mgmt_request *request)
{
struct iovec iov;
Expand All @@ -187,8 +202,8 @@ static bool send_request(struct mgmt *mgmt, struct mgmt_request *request)

ret = io_send(mgmt->io, &iov, 1);
if (ret < 0) {
util_debug(mgmt->debug_callback, mgmt->debug_data,
"write failed: %s", strerror(-ret));
DBG(mgmt, "write failed: %s", strerror(-ret));

if (request->callback)
request->callback(MGMT_STATUS_FAILED, 0, NULL,
request->user_data);
Expand All @@ -202,9 +217,7 @@ static bool send_request(struct mgmt *mgmt, struct mgmt_request *request)
request,
NULL);

util_debug(mgmt->debug_callback, mgmt->debug_data,
"[0x%04x] command 0x%04x",
request->index, request->opcode);
DBG(mgmt, "[0x%04x] command 0x%04x", request->index, request->opcode);

util_hexdump('<', request->buf, ret, mgmt->debug_callback,
mgmt->debug_data);
Expand Down Expand Up @@ -283,9 +296,7 @@ static void request_complete(struct mgmt *mgmt, uint8_t status,
request = queue_remove_if(mgmt->pending_list,
match_request_opcode_index, &match);
if (!request) {
util_debug(mgmt->debug_callback, mgmt->debug_data,
"Unable to find request for opcode 0x%04x",
opcode);
DBG(mgmt, "Unable to find request for opcode 0x%04x", opcode);

/* Attempt to remove with no opcode */
request = queue_remove_if(mgmt->pending_list,
Expand Down Expand Up @@ -383,8 +394,7 @@ static bool can_read_data(struct io *io, void *user_data)
cc = mgmt->buf + MGMT_HDR_SIZE;
opcode = btohs(cc->opcode);

util_debug(mgmt->debug_callback, mgmt->debug_data,
"[0x%04x] command 0x%04x complete: 0x%02x",
DBG(mgmt, "[0x%04x] command 0x%04x complete: 0x%02x",
index, opcode, cc->status);

request_complete(mgmt, cc->status, opcode, index, length - 3,
Expand All @@ -394,15 +404,13 @@ static bool can_read_data(struct io *io, void *user_data)
cs = mgmt->buf + MGMT_HDR_SIZE;
opcode = btohs(cs->opcode);

util_debug(mgmt->debug_callback, mgmt->debug_data,
"[0x%04x] command 0x%02x status: 0x%02x",
DBG(mgmt, "[0x%04x] command 0x%02x status: 0x%02x",
index, opcode, cs->status);

request_complete(mgmt, cs->status, opcode, index, 0, NULL);
break;
default:
util_debug(mgmt->debug_callback, mgmt->debug_data,
"[0x%04x] event 0x%04x", index, event);
DBG(mgmt, "[0x%04x] event 0x%04x", index, event);

process_notify(mgmt, event, index, length,
mgmt->buf + MGMT_HDR_SIZE);
Expand Down

0 comments on commit f9cb7c8

Please sign in to comment.