diff mbox

[02/14] Introduce monitor-error module

Message ID 1254412245-10452-3-git-send-email-lcapitulino@redhat.com
State Superseded
Headers show

Commit Message

Luiz Capitulino Oct. 1, 2009, 3:50 p.m. UTC
This module provides an API to be used by the Monitor and its
command handlers to store error information.

The stored information will be used by both, the future Monitor
protocol emission code and the user protocol.

The API is composed of a new data type called MonitorError and
related functions.

The MonitorError data type represents an error in the Monitor,
it has to store enough information to satisfy the future Monitor
Protocol needs and at the same time maintain compability with
existing errors.

It is composed of the following members:

- code: error code. Not used by the user protocol, but will be
  used by the Monitor Protocol

- desc: error description. This is the standard error message,
  it describes the error without details

- data: error specific data. This member can hold any kind of
  information about the error, it should be used in conjuction
  with 'desc' to provide accurate error information

For example, a MonitorError for a "unknown command" error would
look like the following:

- code: 20
- desc: "unknown command"
- data: a QString containing the (wrong) command name

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 Makefile        |    2 +-
 monitor-error.c |   92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 monitor-error.h |   62 +++++++++++++++++++++++++++++++++++++
 3 files changed, 155 insertions(+), 1 deletions(-)
 create mode 100644 monitor-error.c
 create mode 100644 monitor-error.h
diff mbox

Patch

diff --git a/Makefile b/Makefile
index d66826c..a85d4f7 100644
--- a/Makefile
+++ b/Makefile
@@ -77,7 +77,7 @@  block-obj-y +=  $(addprefix block/, $(block-nested-y))
 # CPUs and machines.
 
 obj-y = $(block-obj-y)
-obj-y += readline.o console.o
+obj-y += monitor-error.o readline.o console.o
 
 obj-y += tcg-runtime.o host-utils.o
 obj-y += irq.o ptimer.o ioport.o
diff --git a/monitor-error.c b/monitor-error.c
new file mode 100644
index 0000000..35f61b8
--- /dev/null
+++ b/monitor-error.c
@@ -0,0 +1,92 @@ 
+/*
+ * MonitorError: Monitor error information.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <lcapitulino@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+#include "qemu-common.h"
+#include "monitor-error.h"
+
+/**
+ * monitor_error_new(): Allocate a new MonitorError data type
+ */
+MonitorError *monitor_error_new(void)
+{
+    return qemu_mallocz(sizeof(MonitorError));
+}
+
+/**
+ * monitor_error_free(): Destroy a MonitorError data type
+ */
+void monitor_error_free(MonitorError *error)
+{
+    QDECREF(error->code);
+    QDECREF(error->desc);
+    qobject_decref(error->data);
+
+    qemu_free(error);
+}
+
+/**
+ * set_error_desc(): Set the error description.
+ */
+static void set_error_desc(MonitorError *error)
+{
+    const char *str;
+
+    assert(error->desc == NULL);
+    assert(error->code != NULL);
+
+    switch (qint_get_int(error->code)) {
+        case MON_ERR_UNCMD:
+            str = "unknown command";
+            break;
+        case MON_ERR_EXPFILE:
+            str = "filename expected";
+            break;
+        case MON_ERR_EXPBLK:
+            str = "block device name expected";
+            break;
+        case MON_ERR_EXPSTR:
+            str = "string expected";
+            break;
+        case MON_ERR_INVCHAR:
+            str = "invalid char in format";
+            break;
+        case MON_ERR_INVINT:
+            str = "integer is for 32-bit values";
+            break;
+        case MON_ERR_UNSOPT:
+            str = "unsopported option";
+            break;
+        case MON_ERR_UNTYPE:
+            str = "unknown type";
+            break;
+        case MON_ERR_EXTCHAR:
+            str = "extraneous characters at the end of line";
+            break;
+        default:
+            str = "unknown";
+            break;
+    }
+
+    error->desc = qstring_from_str(str);
+}
+
+/**
+ * monitor_error_set(): Setup a MonitorError data type.
+ */
+void monitor_error_set(MonitorError *error, int code, QObject *data,
+                       QString *desc)
+{
+    error->code = qint_from_int(code);
+    error->data = data;
+    error->desc = desc;
+    if (!error->desc)
+        set_error_desc(error);
+}
diff --git a/monitor-error.h b/monitor-error.h
new file mode 100644
index 0000000..cd2f82c
--- /dev/null
+++ b/monitor-error.h
@@ -0,0 +1,62 @@ 
+/*
+ * MonitorError header file.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <lcapitulino@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef MONITOR_ERROR_H
+#define MONITOR_ERROR_H
+
+#include "qobject.h"
+#include "qstring.h"
+#include "qint.h"
+
+typedef enum MonitorErrCodes {
+    MON_ERR_UNCMD = 20,
+    MON_ERR_EXPFILE,
+    MON_ERR_EXPBLK,
+    MON_ERR_EXPSTR,
+    MON_ERR_INVCHAR,
+    MON_ERR_INVINT,
+    MON_ERR_UNSOPT,
+    MON_ERR_UNTYPE,
+    MON_ERR_EXTCHAR,
+} MonitorErrCodes;
+
+typedef struct MonitorError {
+    QInt *code;     /* error code */
+    QString *desc;  /* 'global' error description */
+    QObject *data;  /* error specific data */
+} MonitorError;
+
+MonitorError *monitor_error_new(void);
+void monitor_error_free(MonitorError *error);
+void monitor_error_set(MonitorError *error, int code, QObject *data,
+                       QString *desc);
+
+/**
+ * monitor_has_error(): Check if MonitorError is set.
+ *
+ * Return true if MonitorError holds an error, false otherwise.
+ */
+static inline int monitor_has_error(const MonitorError *error)
+{
+    return error->code != NULL;
+}
+
+/**
+ * monitor_error_set_code(): Set an error code.
+ *
+ * Wrapper for monitor_error_set().
+ */
+static inline void monitor_error_set_code(MonitorError *error, int code)
+{
+    monitor_error_set(error, code, NULL, NULL);
+}
+#endif /* MONITOR_ERROR_H */