]> git.draconx.ca Git - upkg.git/commitdiff
libuobject: Store hierarchical object names for UObjects.
authorNick Bowler <nbowler@draconx.ca>
Tue, 15 May 2012 02:15:01 +0000 (22:15 -0400)
committerNick Bowler <nbowler@draconx.ca>
Tue, 15 May 2012 02:15:01 +0000 (22:15 -0400)
Such names are useful for logging and, in general, displaying object
references in a human-readable manner.  This is only a partial
implementation, as package names are not yet available to the
deserialize method.

src/uobject/uobject.c
src/uobject/uobject.h
src/upkg.c

index fa93ec0f420bb6af18b9845dcd83ae811a21ec2c..47782922c5876ee84f93b1783c62ecaf0c02a9ce 100644 (file)
@@ -311,10 +311,47 @@ static int deserialize(UObject *uo)
        return 0;
 }
 
+/*
+ * Get the full hierarchical object name for an export, used for diagnostics.
+ * Currently, this does not include the package name (which is actually
+ * required for this to be a "full" name) as it's not yet passed to the
+ * deserialize method.
+ *
+ * Returns a buffer allocated by malloc on success, or NULL on failure.
+ */
+static char *get_obj_fullname(const struct upkg_export *e)
+{
+       size_t total_len = 0, len;
+       char *fullname;
+
+       for (const struct upkg_export *c = e; c; c = c->parent) {
+               len = strlen(c->name) + 1;
+               if (total_len > SIZE_MAX - len)
+                       return NULL;
+               total_len += len;
+       }
+
+       fullname = malloc(total_len);
+       if (!fullname)
+               return NULL;
+
+       for (const struct upkg_export *c = e; c; c = c->parent) {
+               len = strlen(c->name);
+               assert(total_len > len);
+
+               total_len -= len + 1;
+               memcpy(fullname + total_len, c->name, len);
+               fullname[total_len + len] = c == e ? '\0' : '.';
+       }
+
+       return fullname;
+}
+
 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
 {
        g_return_val_if_fail(IS_U_OBJECT(obj), -1);
        UObject *uo = U_OBJECT(obj);
+       const struct upkg_export *e;
        struct upkg_file *f;
        int rc;
 
@@ -327,6 +364,13 @@ int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
        uo->pkg      = pkg;
        uo->pkg_file = f;
 
+       e = upkg_get_export(pkg, idx);
+       if (!e)
+               return -1;
+       uo->pkg_name = get_obj_fullname(e);
+       if (!uo->pkg_name)
+               return -1;
+
        rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
        if (rc != 0) {
                upkg_export_close(f);
@@ -371,6 +415,8 @@ static void u_object_finalize(GObject *o)
                upkg_export_close(uo->pkg_file);
        }
 
+       free(uo->pkg_name);
+
        G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
 }
 
@@ -427,12 +473,8 @@ void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
        UObject *uo = U_OBJECT(o);
        char *new_fmt = NULL;
 
-       if (uo->pkg_file) {
-               /* XXX: Currently, there's no way to get the full object name
-                * here because the object path information is discarded
-                * after opening it.  In principle, there's no reason why
-                * we couldn't keep it around to improve log messages. */
-               new_fmt = prepend_fmt(uo->pkg_file->name, fmt);
+       if (uo->pkg_name) {
+               new_fmt = prepend_fmt(uo->pkg_name, fmt);
                if (!new_fmt) {
                        g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
                              uo->pkg_file->name);
index e91bf5bee02b2609962ecd9713c746b2aab871d8..e0b22562beb25b5c2a6e3eb66d07cc72b77db424 100644 (file)
@@ -43,6 +43,7 @@ struct UObject {
 
        struct upkg *pkg;
        struct upkg_file *pkg_file;
+       char *pkg_name;
 };
 
 struct UObjectClass {
index e174c215b459112358c35188342552f21f76016f..5a51c2e2203717e6eaa822a94aeb9c338ca092f9 100644 (file)
@@ -312,8 +312,8 @@ static int object_info(GTypeModule *pkg, unsigned long idx)
                                UObject *obj = g_value_get_object(&val);
 
                                /* TODO: Display full object names here. */
-                               if (obj->pkg_file) {
-                                       printf(" %s", obj->pkg_file->name);
+                               if (obj->pkg_name) {
+                                       printf(" %s", obj->pkg_name);
                                }
                        }