Commit 901683ee authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Use loging types for log_proxy messages

Also use colors in log window
parent faefbd74
......@@ -127,6 +127,23 @@ static void timestamp(struct tm *tm, char *time)
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
static char *logtype(enum log_type type)
{
switch(type) {
case LOG_T_ERROR:
return " ERROR: ";
case LOG_T_WARNING:
return "WARNING: ";
case LOG_T_INFO:
return " INFO: ";
case LOG_T_DEBUG:
return " DEBUG: ";
default:
return "UNKNOWN: ";
}
}
static void *log_server(void *arg)
{
int fd_accept;
......@@ -214,23 +231,7 @@ static void *log_server(void *arg)
gmtime_r(&msgs[msgs_rd].t, &gmt);
timestamp(&gmt, time);
switch(msgs[msgs_rd].type) {
case LOG_T_ERROR:
header = " ERROR: ";
break;
case LOG_T_WARNING:
header = "WARNING: ";
break;
case LOG_T_INFO:
header = " INFO: ";
break;
case LOG_T_DEBUG:
header = " DEBUG: ";
break;
default:
header = "UNKNOWN: ";
break;
}
header = logtype(msgs[msgs_rd].type);
if (msgs[msgs_rd].type <= log_level_remote) {
for (i = 0; i < LOG_MAX_CLIENTS; i++) {
......@@ -322,23 +323,7 @@ static void *log_client(void *arg)
gmtime_r(&msgs[msgs_rd].t, &gmt);
timestamp(&gmt, time);
switch(msgs[msgs_rd].type) {
case LOG_T_ERROR:
header = " ERROR: ";
break;
case LOG_T_WARNING:
header = "WARNING: ";
break;
case LOG_T_INFO:
header = " INFO: ";
break;
case LOG_T_DEBUG:
header = " DEBUG: ";
break;
default:
header = "UNKNOWN: ";
break;
}
header = logtype(msgs[msgs_rd].type);
if (fd < 0) {
fd = tcp_connect(server_host, server_port);
......@@ -426,3 +411,27 @@ void log_server_flush(void)
sleep(1);
} while (msgs[msgs_rd].used);
}
void log_string(char *str, size_t len, enum log_type type, char *fmt, ...)
{
va_list ap;
time_t t = time(NULL);
int plen;
struct tm gmt;
va_start(ap, fmt);
gmtime_r(&t, &gmt);
timestamp(&gmt, str);
len -= strlen(str);
str += strlen(str);
plen = snprintf(str, len, logtype(type));
len -= plen;
str += plen;
vsnprintf(str, len, fmt, ap);
va_end(ap);
}
......@@ -36,4 +36,6 @@ void log_server_flush(void);
int log_client_start(char *host, int port, enum log_type console_level,
enum log_type remote_level, char *name);
void log_string(char *str, size_t len, enum log_type type, char *fmt, ...);
#endif /* _INCLUDE_LOG_H_ */
......@@ -35,6 +35,7 @@
#include "dt_port_numbers.h"
#include "dt_host.h"
#include "command_server.h"
#include "log.h"
char *logfile = "controller.log";
......@@ -122,9 +123,12 @@ int main (int argc, char **argv)
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
if (fd_log >= 0) {
rb = sprintf(buffer,
"Established connection to controller\n");
log_string(buffer, 999, LOG_T_INFO, "Established connection to controller");
buffer[strlen(buffer)+1] = 0;
buffer[strlen(buffer)] = '\n';
printf(buffer);
rb = strlen(buffer);
f_log = fdopen(fd_log, "r");
printf("fd_log: %d f_log %p\n", fd_log, f_log);
}
......@@ -144,9 +148,12 @@ int main (int argc, char **argv)
fclose(f_log);
fd_log = -1;
rb = sprintf(buffer,
"Lost connection to controller\n");
log_string(buffer, 999, LOG_T_ERROR, "Lost connection to controller");
buffer[strlen(buffer)+1] = 0;
buffer[strlen(buffer)] = '\n';
printf(buffer);
rb = strlen(buffer);
}
}
command_server_fdset_handle(cmd_srv, &fd_rd);
......@@ -179,11 +186,17 @@ int main (int argc, char **argv)
printf("New client. (now at %d)\n", nr_clients);
if (fd_log >= 0) {
statlen = sprintf(status,
log_string(status, 999, LOG_T_INFO,
"New connection to proxy, controller conection is established.\n");
status[strlen(status)+1] = 0;
status[strlen(status)] = '\n';
statlen = strlen(status);
} else {
statlen = sprintf(status,
log_string(status, 999, LOG_T_INFO,
"New connection to proxy, no connection to controller.\n");
status[strlen(status)+1] = 0;
status[strlen(status)] = '\n';
statlen = strlen(status);
}
write(fdnew, status, statlen);
}
......
......@@ -2155,6 +2155,16 @@ log.innerHTML = "log:";
function log_status_cb(data)
{
if (data.match(/ERROR:/g)) {
data = '<span style="color: Red;">' + data + '</span>';
}
if (data.match(/WARNING:/g)) {
data = '<span style="color: DarkOrange;">' + data + '</span>';
}
if (data.match(/INFO:/g)) {
data = '<span style="color: DarkBlue;">' + data + '</span>';
}
log.innerHTML = log.innerHTML + "\n" + data;
log.scrollTop = log.scrollHeight;
}
......
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