[libvirt] [PATCH v2 3/8] util: Generate a common internal only error print

John Ferlan posted 8 patches 7 years, 11 months ago
There is a newer version of this series
[libvirt] [PATCH v2 3/8] util: Generate a common internal only error print
Posted by John Ferlan 7 years, 11 months ago
If virObjectIsClass fails "internally" to virobject.c, create a macro to
generate the VIR_WARN describing what the problem is. Also improve the
checks and message a bit to indicate which was the failure - whether the
obj was NULL or just not the right class

Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/util/virobject.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/util/virobject.c b/src/util/virobject.c
index 34805d3..9f5f187 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -47,6 +47,16 @@ struct _virClass {
     virObjectDisposeCallback dispose;
 };
 
+#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass)                    \
+    do {                                                                    \
+        virObjectPtr obj = anyobj;                                          \
+        if (!obj)                                                           \
+            VIR_WARN("Object %p is not a virObject class instance", anyobj);\
+        else                                                                \
+            VIR_WARN("Object %p (%s) is not a %s instance",                 \
+                      anyobj, obj->klass->name, #objclass);                 \
+    } while (0)
+
 static virClassPtr virObjectClass;
 static virClassPtr virObjectLockableClass;
 
@@ -312,14 +322,10 @@ virObjectRef(void *anyobj)
 static virObjectLockablePtr
 virObjectGetLockableObj(void *anyobj)
 {
-    virObjectPtr obj;
-
     if (virObjectIsClass(anyobj, virObjectLockableClass))
         return anyobj;
 
-    obj = anyobj;
-    VIR_WARN("Object %p (%s) is not a virObjectLockable instance",
-              anyobj, obj ? obj->klass->name : "(unknown)");
+    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass);
 
     return NULL;
 }
-- 
2.9.4

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 3/8] util: Generate a common internal only error print
Posted by Peter Krempa 7 years, 11 months ago
On Fri, Jun 02, 2017 at 06:17:17 -0400, John Ferlan wrote:
> If virObjectIsClass fails "internally" to virobject.c, create a macro to
> generate the VIR_WARN describing what the problem is. Also improve the
> checks and message a bit to indicate which was the failure - whether the
> obj was NULL or just not the right class
> 
> Signed-off-by: John Ferlan <jferlan@redhat.com>
> ---
>  src/util/virobject.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/src/util/virobject.c b/src/util/virobject.c
> index 34805d3..9f5f187 100644
> --- a/src/util/virobject.c
> +++ b/src/util/virobject.c
> @@ -47,6 +47,16 @@ struct _virClass {
>      virObjectDisposeCallback dispose;
>  };
>  
> +#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass)                    \
> +    do {                                                                    \
> +        virObjectPtr obj = anyobj;                                          \
> +        if (!obj)                                                           \
> +            VIR_WARN("Object %p is not a virObject class instance", anyobj);\

Is this really helpful?

Object (nil) is not a virObject class instance.

%p does not make sense since it's always NULL, and also obviously a
null pointer is not a class instance ...
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 3/8] util: Generate a common internal only error print
Posted by John Ferlan 7 years, 11 months ago

On 06/05/2017 03:39 AM, Peter Krempa wrote:
> On Fri, Jun 02, 2017 at 06:17:17 -0400, John Ferlan wrote:
>> If virObjectIsClass fails "internally" to virobject.c, create a macro to
>> generate the VIR_WARN describing what the problem is. Also improve the
>> checks and message a bit to indicate which was the failure - whether the
>> obj was NULL or just not the right class
>>
>> Signed-off-by: John Ferlan <jferlan@redhat.com>
>> ---
>>  src/util/virobject.c | 16 +++++++++++-----
>>  1 file changed, 11 insertions(+), 5 deletions(-)
>>
>> diff --git a/src/util/virobject.c b/src/util/virobject.c
>> index 34805d3..9f5f187 100644
>> --- a/src/util/virobject.c
>> +++ b/src/util/virobject.c
>> @@ -47,6 +47,16 @@ struct _virClass {
>>      virObjectDisposeCallback dispose;
>>  };
>>  
>> +#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass)                    \
>> +    do {                                                                    \
>> +        virObjectPtr obj = anyobj;                                          \
>> +        if (!obj)                                                           \
>> +            VIR_WARN("Object %p is not a virObject class instance", anyobj);\
> 
> Is this really helpful?
> 
> Object (nil) is not a virObject class instance.
> 
> %p does not make sense since it's always NULL, and also obviously a
> null pointer is not a class instance ...
> 

True; however, if you had snipped more context:

-    VIR_WARN("Object %p (%s) is not a virObjectLockable instance",
-              anyobj, obj ? obj->klass->name : "(unknown)");

the old context used the "? ... :" and would also have said "Object
(nil) (unknown) is not... " - I just went more direct and removed the
assumption that it's a virObjectLockable instance.  I can change the
message if that's all you object to though.

The goal here was the second half of the message to be more explicit.

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list