Commit 156858ae authored by Marc Welz's avatar Marc Welz
Browse files

Logging utility

parent 1484280b
No related merge requests found
Showing with 301 additions and 13 deletions
+301 -13
......@@ -2,7 +2,7 @@ include Makefile.inc
###############################################################################
SUB = katcp cmd kcs examples sq bulkread scripts tmon misc
SUB = katcp cmd kcs examples sq bulkread scripts tmon misc log
###############################################################################
......
......@@ -1196,7 +1196,7 @@ int name_log_level_katcp(struct katcp_dispatch *d, char *name)
return d->d_level;
}
level = log_to_code(name);
level = log_to_code_katcl(name);
if(level < 0){
return -1;
}
......@@ -1235,7 +1235,7 @@ int log_level_cmd_katcp(struct katcp_dispatch *d, int argc)
ok = 1;
d->d_level = KATCP_LEVEL_TRACE;
}
code = log_to_code(requested);
code = log_to_code_katcl(requested);
if(code >= 0){
ok = 1;
d->d_level = code;
......@@ -1246,7 +1246,7 @@ int log_level_cmd_katcp(struct katcp_dispatch *d, int argc)
requested = NULL;
}
got = log_to_string(d->d_level);
got = log_to_string_katcl(d->d_level);
if(got == NULL){
ok = 0;
}
......@@ -1316,7 +1316,7 @@ int log_relay_katcp(struct katcp_dispatch *d, struct katcl_parse *p)
priority = get_string_parse_katcl(p, 1);
if(priority){
code = log_to_code(priority);
code = log_to_code_katcl(priority);
}
if(code < 0){
......@@ -1758,7 +1758,7 @@ int dispatch_cmd_katcp(struct katcp_dispatch *d, int argc)
if(!strcmp(name, "list")){
for(i = 0; i < s->s_used; i++){
dx = s->s_clients[i];
level = log_to_string(dx->d_level);
level = log_to_string_katcl(dx->d_level);
log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "dispatch %s (%s) logging %s with %d notices and %d sensors", dx->d_name, (dx == d) ? "this" : "other", level ? level : "unknown", dx->d_count, dx->d_size);
}
......
......@@ -76,8 +76,8 @@ int problem_katcl(struct katcl_line *l);
void exchange_katcl(struct katcl_line *l, int fd);
int log_to_code(char *name);
char *log_to_string(int code);
int log_to_code_katcl(char *name);
char *log_to_string_katcl(int code);
int log_message_katcl(struct katcl_line *cl, int level, char *name, char *fmt, ...);
int sync_message_katcl(struct katcl_line *cl, int level, char *name, char *fmt, ...);
int vlog_message_katcl(struct katcl_line *cl, int level, char *name, char *fmt, va_list args);
......
......@@ -41,7 +41,7 @@ static char *log_levels_vector[KATCP_MAX_LEVELS] = {
"off"
};
int log_to_code(char *name)
int log_to_code_katcl(char *name)
{
int code;
......@@ -63,7 +63,7 @@ int log_to_code(char *name)
return -1;
}
char *log_to_string(int code)
char *log_to_string_katcl(int code)
{
if((code < 0) || (code >= KATCP_MAX_LEVELS)){
return NULL;
......@@ -128,7 +128,7 @@ int vlog_message_katcl(struct katcl_line *cl, int level, char *name, char *fmt,
}
#endif
logstring = log_to_string(level);
logstring = log_to_string_katcl(level);
if(logstring == NULL){
#ifdef DEBUG
fprintf(stderr, "log: bad log level\n");
......
......@@ -86,10 +86,9 @@ static int client_list_cmd_katcp(struct katcp_dispatch *d, int argc)
if(detail){
append_string_katcp(d, KATCP_FLAG_STRING, dx->d_name);
level = log_to_string(dx->d_level);
level = log_to_string_katcl(dx->d_level);
append_string_katcp(d, KATCP_FLAG_STRING, level ? level : "unknown");
level = log_to_string(dx->d_level);
append_string_katcp(d, KATCP_FLAG_STRING | KATCP_FLAG_LAST, dx->d_pause ? "paused" : "parsing");
} else {
......
include ../Makefile.inc
INC = -I$(KATCP)
LIB = -L$(KATCP) -lkatcp
EXE = kl
SRC = kl.c
OBJ = $(patsubst %.c,%.o,$(SRC))
all: $(EXE)
$(EXE): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LIB)
install: all
$(INSTALL) $(EXE) $(PREFIX)/bin
%.o: %.c $(wildcard *.h)
$(CC) $(CFLAGS) -c $< $(INC)
clean:
$(RM) $(OBJ) core $(EXE)
log/kl.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <sysexits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netc.h>
#include <katcl.h>
#include <katcp.h>
#include <katpriv.h>
#define NAME "kl"
static volatile int log_level = KATCP_LEVEL_INFO;
static volatile int log_changed = 0;
void usage(char *app)
{
printf("usage: %s [-l level] [-o logfile] [-a reconnect-attempts] [-d] [-s server:port]\n", app);
}
static void handle_signal(int signal)
{
switch(signal){
case SIGUSR1 :
if(log_level > KATCP_LEVEL_TRACE){
log_level--;
}
break;
case SIGUSR2 :
if(log_level < KATCP_LEVEL_OFF){
log_level++;
}
break;
case SIGHUP :
log_level = KATCP_LEVEL_INFO;
break;
default :
return;
}
log_changed = 1;
}
int main(int argc, char **argv)
{
char *level, *app, *server, *output;
int run, fd, i, j, c, verbose, attempts, detach, result;
struct katcl_parse *p;
struct katcl_line *ls, *lo;
struct sigaction sa;
i = j = 1;
app = argv[0];
verbose = 0;
attempts = 2;
detach = 0;
server = NULL;
output = NULL;
level = NULL;
while (i < argc) {
if (argv[i][0] == '-') {
c = argv[i][j];
switch (c) {
case 'h' :
usage(app);
return EX_OK;
case 'v' :
verbose++;
j++;
break;
case 'd' :
detach = 1 - detach;
j++;
break;
case 'q' :
verbose = 0;
j++;
break;
case 'l' :
case 'o' :
case 'a' :
case 's' :
j++;
if (argv[i][j] == '\0') {
j = 0;
i++;
}
if (i >= argc) {
fprintf(stderr, "%s: usage: argument needs a parameter\n", app);
return EX_USAGE;
}
switch(c){
case 'l' :
level = argv[i] + j;
break;
case 'o' :
output = argv[i] + j;
break;
case 'a' :
attempts = atoi(argv[i] + j);
break;
case 's' :
server = argv[i] + j;
break;
}
i++;
j = 1;
break;
case '-' :
j++;
break;
case '\0':
j = 1;
i++;
break;
default:
fprintf(stderr, "%s: usage: unknown option -%c\n", app, argv[i][j]);
return EX_USAGE;
}
} else {
if(output){
fprintf(stderr, "%s: usage: unexpected extra argument %s (can only save to one file)\n", app, argv[i]);
return EX_USAGE;
}
output = argv[i];
i++;
}
}
sa.sa_handler = handle_signal;
#if 0
sa.sa_flags = SA_RESTART;
#endif
sa.sa_flags = 0;
sigemptyset(&(sa.sa_mask));
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
if(server == NULL){
server = "localhost:7147";
}
if(level){
log_changed = 1;
log_level = log_to_code_katcl(level);
if(log_level < 0){
fprintf(stderr, "%s: usage: invalid initial log priority %s\n", app, level);
return EX_USAGE;
}
}
if(output == NULL){
if(detach == 1){
fprintf(stderr, "%s: usage: need a filename as target\n", app);
return EX_USAGE;
}
fd = STDOUT_FILENO;
} else {
fd = open(output, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if(fd < 0){
fprintf(stderr, "%s: unable to open file %s: %s\n", app, output, strerror(errno));
return EX_OSERR;
}
}
lo = create_katcl(fd);
if(lo == NULL){
fprintf(stderr, "%s: unable to allocate log state\n", app);
return EX_OSERR;
}
/**********************/
while((attempts-- > 0) && ((fd = net_connect(server, 0, 0)) < 0)){
sleep(1);
}
if(attempts <= 0){
sync_message_katcl(lo, KATCP_LEVEL_FATAL, NAME, "unable to connect to %s", server);
return EX_UNAVAILABLE;
}
ls = create_katcl(fd);
if(ls == NULL){
sync_message_katcl(lo, KATCP_LEVEL_FATAL, NAME, "unable to allocate parser state");
return EX_OSERR;
}
for(run = 1; run > 0;){
/* WARNING: will only run after the next message - may have to interrupt syscall to get past this */
if(log_changed > 0){
level = log_to_string_katcl(log_level);
if(level){
p = create_parse_katcl();
if(p){
add_string_parse_katcl(p, KATCP_FLAG_STRING | KATCP_FLAG_FIRST, "?log-level");
add_string_parse_katcl(p, KATCP_FLAG_STRING | KATCP_FLAG_LAST, level);
append_parse_katcl(lo, p);
/* dodgy refcount dealings: p is created with refcount = 0, so can only do write at end, otherwise may end up being deallocated */
append_parse_katcl(ls, p);
write_katcl(ls);
}
} else {
sync_message_katcl(lo, KATCP_LEVEL_ERROR, NAME, "invalid log priority number %d", level);
}
log_changed = 0;
}
result = read_katcl(ls);
if(result < 0){
sync_message_katcl(lo, KATCP_LEVEL_FATAL, NAME, "read from network failed: %s", strerror(errno));
return EX_OSERR;
}
if(result == 1){
run = 0;
}
while(have_katcl(ls)){
p = ready_katcl(ls);
if(p){
append_parse_katcl(lo, p);
}
}
write_katcl(lo);
}
return EX_OK;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment