Skip to content

Commit

Permalink
logging: add min priority and other cleanup
Browse files Browse the repository at this point in the history
The dprintf macro shadows a real libc symbol which is called
differently, so we should change it to something else (in this
case LOGD) and change it to use the libqrtr logging facilities.
This also means we should add a minimum priority to the logging
library so we don't log DEBUG-level messages by default.

Additionally there were some warn/warnx calls left over that
have been changed to PLOGW/LOGW respectively.
  • Loading branch information
ejcaruso authored and andersson committed Dec 11, 2018
1 parent c9c4a3c commit c1bdfb3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
15 changes: 15 additions & 0 deletions lib/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@

static const char default_tag[] = "libqrtr";
static const char *current_tag = default_tag;
static int min_priority = LOG_INFO;

static bool logging_to_syslog = false;

void qlog_setup(const char *tag, bool use_syslog)
{
current_tag = tag;
logging_to_syslog = use_syslog;

openlog(tag, LOG_PID, LOG_USER);
}

void qlog_set_min_priority(int priority)
{
if (priority < LOG_EMERG || priority > LOG_DEBUG)
return;

min_priority = priority;
}

static const char *get_priority_string(int priority)
Expand Down Expand Up @@ -42,6 +53,10 @@ static const char *get_priority_string(int priority)
void qlog(int priority, const char *format, ...)
{
va_list ap;

if (priority > min_priority)
return;

va_start(ap, format);

if (logging_to_syslog) {
Expand Down
3 changes: 3 additions & 0 deletions lib/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#endif

void qlog_setup(const char *tag, bool use_syslog);
void qlog_set_min_priority(int priority);

void qlog(int priority, const char *format, ...) __PRINTF__(2, 3);

#define LOGD(fmt, ...) qlog(LOG_DEBUG, fmt, ##__VA_ARGS__)

#define LOGW(fmt, ...) qlog(LOG_WARNING, fmt, ##__VA_ARGS__)
#define PLOGW(fmt, ...) \
qlog(LOG_WARNING, fmt ": %s", ##__VA_ARGS__, strerror(errno))
Expand Down
37 changes: 21 additions & 16 deletions src/ns.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ static const char *ctrl_pkt_strings[] = {
[QRTR_TYPE_DEL_LOOKUP] = "del-lookup",
};

#define dprintf(...)
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))

struct context {
Expand Down Expand Up @@ -160,7 +159,7 @@ static int service_announce_new(struct context *ctx,
struct qrtr_ctrl_pkt cmsg;
int rc;

dprintf("advertising new server [%d:%x]@[%d:%d]\n",
LOGD("advertising new server [%d:%x]@[%d:%d]\n",
srv->service, srv->instance, srv->node, srv->port);

cmsg.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);
Expand All @@ -172,7 +171,7 @@ static int service_announce_new(struct context *ctx,
rc = sendto(ctx->sock, &cmsg, sizeof(cmsg), 0,
(struct sockaddr *)dest, sizeof(*dest));
if (rc < 0)
warn("sendto()");
PLOGW("sendto()");

return rc;
}
Expand All @@ -184,7 +183,7 @@ static int service_announce_del(struct context *ctx,
struct qrtr_ctrl_pkt cmsg;
int rc;

dprintf("advertising removal of server [%d:%x]@[%d:%d]\n",
LOGD("advertising removal of server [%d:%x]@[%d:%d]\n",
srv->service, srv->instance, srv->node, srv->port);

cmsg.cmd = cpu_to_le32(QRTR_TYPE_DEL_SERVER);
Expand All @@ -196,7 +195,7 @@ static int service_announce_del(struct context *ctx,
rc = sendto(ctx->sock, &cmsg, sizeof(cmsg), 0,
(struct sockaddr *)dest, sizeof(*dest));
if (rc < 0)
warn("sendto()");
PLOGW("sendto()");

return rc;
}
Expand All @@ -218,7 +217,7 @@ static int lookup_notify(struct context *ctx, struct sockaddr_qrtr *to,
rc = sendto(ctx->sock, &pkt, sizeof(pkt), 0,
(struct sockaddr *)to, sizeof(*to));
if (rc < 0)
warn("send lookup result failed");
PLOGW("send lookup result failed");
return rc;
}

Expand Down Expand Up @@ -272,8 +271,8 @@ static struct server *server_add(unsigned int service, unsigned int instance,
if (rc)
goto err;

dprintf("add server [%d:%x]@[%d:%d]\n", srv->service, srv->instance,
srv->node, srv->port);
LOGD("add server [%d:%x]@[%d:%d]\n", srv->service, srv->instance,
srv->node, srv->port);

if (mi) { /* we replaced someone */
struct server *old = container_of(mi, struct server, mi);
Expand Down Expand Up @@ -372,7 +371,7 @@ static int ctrl_cmd_bye(struct context *ctx, struct sockaddr_qrtr *from)
rc = sendto(ctx->sock, &pkt, sizeof(pkt), 0,
(struct sockaddr *)&sq, sizeof(sq));
if (rc < 0)
warn("bye propagation failed");
PLOGW("bye propagation failed");
}

return 0;
Expand Down Expand Up @@ -436,7 +435,7 @@ static int ctrl_cmd_del_client(struct context *ctx, struct sockaddr_qrtr *from,
rc = sendto(ctx->sock, &pkt, sizeof(pkt), 0,
(struct sockaddr *)&sq, sizeof(sq));
if (rc < 0)
warn("del_client propagation failed");
PLOGW("del_client propagation failed");
}

return 0;
Expand Down Expand Up @@ -586,23 +585,23 @@ static void ctrl_port_fn(void *vcontext, struct waiter_ticket *tkt)
sl = sizeof(sq);
len = recvfrom(sock, buf, sizeof(buf), 0, (void *)&sq, &sl);
if (len <= 0) {
warn("recvfrom()");
PLOGW("recvfrom()");
close(sock);
ctx->sock = -1;
goto out;
}
msg = (void *)buf;

if (len < 4) {
warnx("short packet from %d:%d", sq.sq_node, sq.sq_port);
LOGW("short packet from %d:%d", sq.sq_node, sq.sq_port);
goto out;
}

cmd = le32_to_cpu(msg->cmd);
if (cmd < ARRAY_SIZE(ctrl_pkt_strings) && ctrl_pkt_strings[cmd])
dprintf("%s from %d:%d\n", ctrl_pkt_strings[cmd], sq.sq_node, sq.sq_port);
LOGD("%s from %d:%d\n", ctrl_pkt_strings[cmd], sq.sq_node, sq.sq_port);
else
dprintf("UNK (%08x) from %d:%d\n", cmd, sq.sq_node, sq.sq_port);
LOGD("UNK (%08x) from %d:%d\n", cmd, sq.sq_node, sq.sq_port);

rc = 0;
switch (cmd) {
Expand Down Expand Up @@ -648,7 +647,7 @@ static void ctrl_port_fn(void *vcontext, struct waiter_ticket *tkt)
}

if (rc < 0)
warnx("failed while handling packet from %d:%d",
LOGW("failed while handling packet from %d:%d",
sq.sq_node, sq.sq_port);
out:
waiter_ticket_clear(tkt);
Expand Down Expand Up @@ -701,25 +700,31 @@ int main(int argc, char **argv)
socklen_t sl = sizeof(sq);
bool foreground = false;
bool use_syslog = false;
bool verbose_log = false;
char *ep;
int opt;
int rc;
const char *progname = basename(argv[0]);

while ((opt = getopt(argc, argv, "fs")) != -1) {
while ((opt = getopt(argc, argv, "fsv")) != -1) {
switch (opt) {
case 'f':
foreground = true;
break;
case 's':
use_syslog = true;
break;
case 'v':
verbose_log = true;
break;
default:
usage(progname);
}
}

qlog_setup(progname, use_syslog);
if (verbose_log)
qlog_set_min_priority(LOG_DEBUG);

if (optind < argc) {
addr = strtoul(argv[optind], &ep, 10);
Expand Down

0 comments on commit c1bdfb3

Please sign in to comment.