aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2012-08-20 15:24:13 +0200
committerGerd Hoffmann <kraxel@redhat.com>2012-08-20 15:24:13 +0200
commit4bcf40a96e5dd2c0be19ad3878e00b1defcbd671 (patch)
tree122214b1a3c321be966b0a1fa5b2e6e18de51c7a
parent7a7ffc74974ec4c4b2afef762365dfdb4cf59cc2 (diff)
downloadvconsole-4bcf40a96e5dd2c0be19ad3878e00b1defcbd671.tar.gz
implement logging
-rw-r--r--domain.c83
-rw-r--r--vconsole.h5
2 files changed, 86 insertions, 2 deletions
diff --git a/domain.c b/domain.c
index b88c00c..6cf7e77 100644
--- a/domain.c
+++ b/domain.c
@@ -68,9 +68,85 @@ static void domain_configure_vte(struct vconsole_domain *dom)
vte_terminal_set_color_background(vte, &bg);
}
+static void make_dirs(const char *filename)
+{
+ char *dirname = g_strdup(filename);
+ char *slash = strrchr(dirname, '/');
+
+ if (!slash)
+ return;
+ if (slash == dirname)
+ return;
+ *slash = 0;
+ if (mkdir(dirname, 0777) < 0) {
+ if (errno != ENOENT)
+ goto err;
+ make_dirs(dirname);
+ if (mkdir(dirname, 0777) < 0)
+ goto err;
+ }
+ return;
+
+err:
+ fprintf(stderr, "mkdir %s: %s\n", dirname, strerror(errno));
+ g_free(dirname);
+ return;
+}
+
+static void domain_log_open(struct vconsole_domain *dom)
+{
+ virDomainPtr d;
+
+ if (!dom->conn->win->vm_logging)
+ return;
+ if (!dom->stream)
+ return;
+ if (dom->logfp)
+ return;
+
+ d = virDomainLookupByUUIDString(dom->conn->ptr, dom->uuid);
+ dom->logname = g_strdup_printf("%s/vconsole/%s/%s.log",
+ getenv("HOME"),
+ virConnectGetHostname(dom->conn->ptr),
+ virDomainGetName(d));
+ dom->logfp = fopen(dom->logname, "a");
+ if (dom->logfp == NULL) {
+ if (errno != ENOENT)
+ goto err;
+ make_dirs(dom->logname);
+ dom->logfp = fopen(dom->logname, "a");
+ if (dom->logfp == NULL)
+ goto err;
+ }
+ setbuf(dom->logfp, NULL); /* unbuffered please */
+ fprintf(dom->logfp, "*** vconsole: log opened ***\n");
+ return;
+
+err:
+ fprintf(stderr, "open %s: %s\n", dom->logname, strerror(errno));
+ g_free(dom->logname);
+ dom->logname = NULL;
+}
+
+static void domain_log_close(struct vconsole_domain *dom)
+{
+ if (!dom->logfp)
+ return;
+ fprintf(dom->logfp, "\n*** vconsole: closing log ***\n");
+ fclose(dom->logfp);
+ dom->logfp = NULL;
+ g_free(dom->logname);
+ dom->logname = NULL;
+}
+
static void domain_configure_logging(struct vconsole_domain *dom)
{
- /* TODO */
+ gboolean logging = dom->conn->win->vm_logging;
+
+ if (!logging)
+ domain_log_close(dom);
+ else
+ domain_log_open(dom);
}
void domain_configure_all_vtes(struct vconsole_window *win)
@@ -105,6 +181,7 @@ static void domain_disconnect(struct vconsole_domain *dom, virDomainPtr d)
virStreamEventRemoveCallback(dom->stream);
virStreamFree(dom->stream);
dom->stream = NULL;
+ domain_log_close(dom);
domain_update_status(dom);
}
@@ -139,6 +216,8 @@ static void domain_console_event(virStreamPtr stream, int events, void *opaque)
bytes += rc;
if (dom->vte)
vte_terminal_feed(VTE_TERMINAL(dom->vte), buf, rc);
+ if (dom->logfp)
+ fwrite(buf, rc, 1, dom->logfp);
}
if (bytes == 0) {
if (debug)
@@ -190,6 +269,7 @@ static void domain_connect(struct vconsole_domain *dom, virDomainPtr d)
domain_console_event, dom, NULL);
if (debug)
fprintf(stderr, "%s: %s ok\n", __func__, virDomainGetName(d));
+ domain_log_open(dom);
domain_update_status(dom);
}
@@ -391,7 +471,6 @@ void domain_activate(struct vconsole_domain *dom)
gtk_widget_show_all(dom->vbox);
gtk_notebook_set_current_page(GTK_NOTEBOOK(win->notebook), page);
domain_configure_vte(dom);
- domain_configure_logging(dom);
domain_update_status(dom);
}
diff --git a/vconsole.h b/vconsole.h
index 143aa66..803b1c0 100644
--- a/vconsole.h
+++ b/vconsole.h
@@ -6,6 +6,8 @@
#include <fcntl.h>
#include <assert.h>
+#include <sys/stat.h>
+
#include <gtk/gtk.h>
#include <vte/vte.h>
@@ -74,6 +76,9 @@ struct vconsole_domain {
GtkWidget *vbox, *vte, *status;
virStreamPtr stream;
virDomainInfo info;
+
+ FILE *logfp;
+ char *logname;
};
void domain_start(struct vconsole_domain *dom);