Browse Source

ckpmsg - add an optional displayed line counter and allow setting the log level

master
kanoi 9 years ago
parent
commit
b87dc9d1e3
  1. 58
      src/ckpmsg.c

58
src/ckpmsg.c

@ -1,5 +1,6 @@
/* /*
* Copyright 2014-2016 Con Kolivas * Copyright 2014-2016 Con Kolivas
* Copyright 2014-2016 Andrew Smith
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free * under the terms of the GNU General Public License as published by the Free
@ -15,6 +16,23 @@
#include "libckpool.h" #include "libckpool.h"
static int msg_loglevel = LOG_DEBUG;
void logmsg(int loglevel, const char *fmt, ...)
{
va_list ap;
char *buf;
if (loglevel <= msg_loglevel) {
va_start(ap, fmt);
VASPRINTF(&buf, fmt, ap);
va_end(ap);
printf("%s\n", buf);
free(buf);
}
}
void mkstamp(char *stamp, size_t siz) void mkstamp(char *stamp, size_t siz)
{ {
long minoff, hroff; long minoff, hroff;
@ -57,12 +75,28 @@ int main(int argc, char **argv)
char *name = NULL, *socket_dir = NULL, *buf = NULL, *sockname = "listener"; char *name = NULL, *socket_dir = NULL, *buf = NULL, *sockname = "listener";
int tmo1 = RECV_UNIX_TIMEOUT1; int tmo1 = RECV_UNIX_TIMEOUT1;
int tmo2 = RECV_UNIX_TIMEOUT2; int tmo2 = RECV_UNIX_TIMEOUT2;
bool proxy = false; bool proxy = false, counter = false;
char stamp[128]; char stamp[128];
int c; int c, count;
while ((c = getopt(argc, argv, "N:n:s:pt:T:")) != -1) { while ((c = getopt(argc, argv, "cl:N:n:ps:t:T:")) != -1) {
switch(c) { switch(c) {
/* You'd normally disable most logmsg with -l 3 to
* only see the counter */
case 'c':
counter = true;
break;
case 'l':
msg_loglevel = atoi(optarg);
if (msg_loglevel < LOG_EMERG ||
msg_loglevel > LOG_DEBUG) {
quit(1, "Invalid loglevel: %d (range %d"
" - %d)",
msg_loglevel,
LOG_EMERG,
LOG_DEBUG);
}
break;
/* Allows us to specify which process or socket to /* Allows us to specify which process or socket to
* talk to. */ * talk to. */
case 'N': case 'N':
@ -71,12 +105,12 @@ int main(int argc, char **argv)
case 'n': case 'n':
name = strdup(optarg); name = strdup(optarg);
break; break;
case 's':
socket_dir = strdup(optarg);
break;
case 'p': case 'p':
proxy = true; proxy = true;
break; break;
case 's':
socket_dir = strdup(optarg);
break;
case 't': case 't':
tmo1 = atoi(optarg); tmo1 = atoi(optarg);
break; break;
@ -99,6 +133,7 @@ int main(int argc, char **argv)
trail_slash(&socket_dir); trail_slash(&socket_dir);
realloc_strcat(&socket_dir, sockname); realloc_strcat(&socket_dir, sockname);
count = 0;
while (42) { while (42) {
int sockd, len; int sockd, len;
size_t n; size_t n;
@ -106,7 +141,7 @@ int main(int argc, char **argv)
dealloc(buf); dealloc(buf);
len = getline(&buf, &n, stdin); len = getline(&buf, &n, stdin);
if (len == -1) { if (len == -1) {
LOGERR("Failed to get a valid line"); LOGNOTICE("Failed to get a valid line");
break; break;
} }
mkstamp(stamp, sizeof(stamp)); mkstamp(stamp, sizeof(stamp));
@ -135,11 +170,18 @@ int main(int argc, char **argv)
buf = recv_unix_msg_tmo2(sockd, tmo1, tmo2); buf = recv_unix_msg_tmo2(sockd, tmo1, tmo2);
close(sockd); close(sockd);
if (!buf) { if (!buf) {
LOGERR("Received empty message"); LOGERR("Received empty reply");
continue; continue;
} }
mkstamp(stamp, sizeof(stamp)); mkstamp(stamp, sizeof(stamp));
LOGMSGSIZ(65536, LOG_NOTICE, "%s Received response: %s", stamp, buf); LOGMSGSIZ(65536, LOG_NOTICE, "%s Received response: %s", stamp, buf);
if (counter) {
if ((++count % 100) == 0) {
printf("%8d\r", count);
fflush(stdout);
}
}
} }
dealloc(buf); dealloc(buf);

Loading…
Cancel
Save