#include <libubox/usock.h>
#include <libubox/uloop.h>
#include "libubus.h"
+#include "syslog.h"
enum {
LOG_STDOUT,
static struct ubus_subscriber log_event;
static struct uloop_timeout retry;
static struct uloop_fd sender;
-static const char *log_file, *log_ip, *log_port, *pid_file;
+static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
static int log_type = LOG_STDOUT;
static int log_size, log_udp;
+static const char* getcodetext(int value, CODE *codetable) {
+ CODE *i;
+
+ if (value >= 0)
+ for (i = codetable; i->c_val != -1; i++)
+ if (i->c_val == value)
+ return (i->c_name);
+ return "<unknown>";
+};
+
static void log_handle_reconnect(struct uloop_timeout *timeout)
{
sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
{
struct blob_attr *tb[__LOG_MAX];
struct stat s;
- char buf[256];
+ char buf[512];
uint32_t p;
char *str;
time_t t;
if (log_type == LOG_NET) {
int err;
- snprintf(buf, sizeof(buf), "%s%s\n",
- (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : ("kernel: "),
- method);
+ *buf = '\0';
+ if (hostname)
+ snprintf(buf, sizeof(buf), "%s ", hostname);
+ if (log_prefix) {
+ strncat(buf, log_prefix, sizeof(buf));
+ strncat(buf, ": ", sizeof(buf));
+ }
+ if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
+ strncat(buf, "kernel: ", sizeof(buf));
+ strncat(buf, method, sizeof(buf));
if (log_udp)
err = write(sender.fd, buf, strlen(buf));
else
}
} else {
snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
- c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
+ c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
(blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
method);
write(sender.fd, buf, strlen(buf));
__READ_MAX
};
+
+
static const struct blobmsg_policy read_policy[] = {
[READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY },
};
c[strlen(c) - 1] = '\0';
printf("%s %s.%s%s %s\n",
- c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
+ c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
(blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
blobmsg_get_string(tb[LOG_MSG]));
}
" -F <file> Log file\n"
" -S <bytes> Log size\n"
" -p <file> PID file\n"
+ " -h <hostname> Add hostname to the message\n"
+ " -P <prefix> Prefix custom text to streamed messages\n"
" -f Follow log messages\n"
" -u Use UDP as the protocol\n"
"\n", prog);
int ch, ret, subscribe = 0, lines = 0;
static struct blob_buf b;
- while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:")) != -1) {
+ while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:P:h:")) != -1) {
switch (ch) {
case 'u':
log_udp = 1;
case 'p':
pid_file = optarg;
break;
+ case 'P':
+ log_prefix = optarg;
+ break;
case 'f':
subscribe = 1;
break;
log_size = 1;
log_size *= 1024;
break;
+ case 'h':
+ hostname = optarg;
+ break;
default:
return usage(*argv);
}