tools/toollog: Drop XTL_NEW_LOGGER()
PATCH 1/1 tools/toollog: Drop XTL_NEW_LOGGER()
XTL_NEW_LOGGER() makes a number of unreasonable assumptions about the symbols visible in its scope, and as such is only usable by its sole caller.
Remove it.
---
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
--- tools/libs/toollog/include/xentoollog.h | 21 --------------------- tools/libs/toollog/xtl_logger_stdio.c | 30 ++++++++++++++++++------------ 2 files changed, 18 insertions(+), 33 deletions(-)
tools/libs/toollog/include/xentoollog.h +0 -21
@@ -112,25 +112,4 @@ void xtl_progress(struct xentoollog_logger *logger,const char *xtl_level_to_string (xentoollog_level );/* never fails */ - -#define XTL_NEW_LOGGER (LOGGER ,buffer) ({ \ - xentoollog_logger_##LOGGER *new_consumer; \ - \ - (buffer).vtable.vmessage = LOGGER##_vmessage; \ - (buffer).vtable.progress = LOGGER##_progress; \ - (buffer).vtable.destroy = LOGGER##_destroy; \ - \ - new_consumer = malloc(sizeof(*new_consumer)); \ - if (!new_consumer) { \ - xtl_log((xentoollog_logger*)&buffer, \ - XTL_CRITICAL, errno, "xtl", \ - "failed to allocate memory for new message logger"); \ - } else { \ - *new_consumer = buffer; \ - } \ - \ - new_consumer; \ -}); - -#endif /* XENTOOLLOG_H */
tools/libs/toollog/xtl_logger_stdio.c +18 -12
@@ -165,28 +165,34 @@ void xtl_stdiostream_adjust_flags(xentoollog_logger_stdiostream *lg,xentoollog_logger_stdiostream *xtl_createlogger_stdiostream (FILE *f,xentoollog_level min_level,unsigned flags) { -xentoollog_logger_stdiostream newlogger; - newlogger.f = f; - newlogger.min_level = min_level; - newlogger.flags = flags; +xentoollog_logger_stdiostream *nl = +calloc (sizeof (xentoollog_logger_stdiostream),1 ); + +if (!nl) +return NULL ; + + nl->vtable.vmessage = stdiostream_vmessage; + nl->vtable.progress = stdiostream_progress; + nl->vtable.destroy = stdiostream_destroy; + + nl->f = f; + nl->min_level = min_level; + nl->flags = flags;switch (flags & (XTL_STDIOSTREAM_PROGRESS_USE_CR |XTL_STDIOSTREAM_PROGRESS_NO_CR )) { -case XTL_STDIOSTREAM_PROGRESS_USE_CR : newlogger.progress_use_cr =1 ;break ; -case XTL_STDIOSTREAM_PROGRESS_NO_CR : newlogger.progress_use_cr =0 ;break ; +case XTL_STDIOSTREAM_PROGRESS_USE_CR : nl->progress_use_cr =1 ;break ; +case XTL_STDIOSTREAM_PROGRESS_NO_CR : nl->progress_use_cr =0 ;break ;case 0 : - newlogger.progress_use_cr =isatty (fileno (newlogger.f)) >0 ; + nl->progress_use_cr =isatty (fileno (nl->f)) >0 ;break ;default : errno =EINVAL ;return 0 ; } -if (newlogger.flags &XTL_STDIOSTREAM_SHOW_DATE )tzset (); - - newlogger.progress_erase_len =0 ; - newlogger.progress_last_percent =0 ; +if (nl->flags &XTL_STDIOSTREAM_SHOW_DATE )tzset (); -return XTL_NEW_LOGGER (stdiostream, newlogger); +return nl; }
Signature · 1 line
2.1.4
Quoted 6 lines · Andrew Cooper
XTL_NEW_LOGGER()makes a number of unreasonable assumptions about the symbols visible in its scope, and as such is only usable by its sole caller.Remove it.
Signed-off-by Andrew Cooper <[email protected]>
Quoted 2 lines · Andrew Cooper
XTL_NEW_LOGGER()makes a number of unreasonable assumptions about the symbols visible in its scope,
It assumes that the function names to fill in the vtable and the type name are related, that hardly seems totally "unreasonable". What else does it assume that makes it unreasonable?
I think if you intend to remove something on this basis you need to be specific about what you believe the short comings are.
Quoted 1 line
and as such is only usable by its sole caller.
"not usable by every imaginable caller" is not the same as "usable by one single possible caller", I think you are overstating the case here.
Quoted 111 lines
Remove it.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
---
tools/libs/toollog/include/xentoollog.h | 21 --------------------- tools/libs/toollog/xtl_logger_stdio.c | 30 ++++++++++++++++++---------
---2 files changed, 18 insertions(+), 33 deletions(-)tools/libs/toollog/include/xentoollog.h +0
-0@@ -112,25 +112,4 @@ void xtl_progress(struct xentoollog_logger *logger,
const char*xtl_level_to_string(xentoollog_level); /* never fails */tools/libs/toollog/include/xentoollog.h +0
-2- -#define XTL_NEW_LOGGER (LOGGER ,buffer)({ \
- xentoollog_logger_##LOGGER
*new_consumer; \
-
\
- (buffer).vtable.vmessage =
LOGGER##_vmessage; \
- (buffer).vtable.progress =
LOGGER##_progress; \
- (buffer).vtable.destroy =
LOGGER##_destroy; \
-
\
- new_consumer =
malloc(sizeof(*new_consumer)); \
- if (!new_consumer)
{ \
- xtl_log((xentoollog_logger*)&buffer,
\
- XTL_CRITICAL, errno,
"xtl", \
- "failed to allocate memory for new message logger"); \
- } else
{ \
- *new_consumer =
buffer; \
- }
\
-
\
- new_consumer;
\tools/libs/toollog/include/xentoollog.h +0
-3-}); - -#endif /*
XENTOOLLOG_H*/tools/libs/toollog/xtl_logger_stdio.c +0
-0@@ -165,28 +165,34 @@ void xtl_stdiostream_adjust_flags(xentoollog_logger_stdiostream *lg,
xentoollog_logger_stdiostream*xtl_createlogger_stdiostream(FILE *f,xentoollog_levelmin_level, unsigned flags) { - xentoollog_logger_stdiostream newlogger;
- newlogger.f = f;
- newlogger.min_level =min_level;
- newlogger.flags = flags;
+ xentoollog_logger_stdiostream *nl =
+ calloc(sizeof(xentoollog_logger_stdiostream), 1);tools/libs/toollog/xtl_logger_stdio.c +11
-0+ + if (!nl) + return NULL; + + nl->vtable.vmessage = stdiostream_vmessage; + nl->vtable.progress = stdiostream_progress; + nl->vtable.destroy = stdiostream_destroy; + + nl->f = f; + nl->min_level = min_level; + nl->flags = flags;
switch (flags & (XTL_STDIOSTREAM_PROGRESS_USE_CR|XTL_STDIOSTREAM_PROGRESS_NO_CR)) {
- caseXTL_STDIOSTREAM_PROGRESS_USE_CR: newlogger.progress_use_cr = 1; break;
- case XTL_STDIOSTREAM_PROGRESS_NO_CR: newlogger.progress_use_cr = 0; break;
+ caseXTL_STDIOSTREAM_PROGRESS_USE_CR:nl->progress_use_cr= 1; break;
+ case XTL_STDIOSTREAM_PROGRESS_NO_CR: nl->progress_use_cr = 0; break;
case 0:
- newlogger.progress_use_cr = isatty(fileno(newlogger.f)) > 0; + nl->progress_use_cr = isatty(fileno(nl->f)) > 0; break;
default:
errno = EINVAL;
return 0;
}
- if (newlogger.flags &XTL_STDIOSTREAM_SHOW_DATE)tzset();
Quoted 8 lines
-
- newlogger.progress_erase_len = 0;
- newlogger.progress_last_percent = 0;
+ if (nl->flags&XTL_STDIOSTREAM_SHOW_DATE)tzset();
- returnXTL_NEW_LOGGER(stdiostream, newlogger); + return nl;
}
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 6 lines · Ian Campbell
Quoted 2 lines · Andrew Cooper
XTL_NEW_LOGGER()makes a number of unreasonable assumptions about the symbols visible in its scope,It assumes that the function names to fill in the vtable and the type name are related, that hardly seems totally "unreasonable". What else does it assume that makes it unreasonable?
We have already had this argument once, the result being "patch welcome". Here one is.
Not only does it assume certain names, it is tokenised with a magic 3rd identifier.
It also assumes the presence of a structure member named vtable.
Quoted 3 lines
I think if you intend to remove something on this basis you need to be specific about what you believe the short comings are.
GNUism in header file claiming C99 strictness
If vtable isn't the first element in the structure, it follows a wild pointer on error.
Quoted 4 lines
Quoted 2 lines
and as such is only usable by its sole caller.
"not usable by every imaginable caller" is not the same as "usable by one single possible caller", I think you are overstating the case here.
It is genuinely harder to reverse engineer how to use that macro than to opencode a sane alternative.
I stand firmly by my statement.
~Andrew
Quoted 11 lines · Andrew Cooper
Quoted 9 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper
XTL_NEW_LOGGER()makes a number of unreasonable assumptions about the symbols
visible in its scope,It assumes that the function names to fill in the vtable and the type name
are related, that hardly seems totally "unreasonable". What else does it
assume that makes it unreasonable?
Quoted 7 lines
We have already had this argument once, the result being "patch welcome". Here one is.
Not only does it assume certain names, it is tokenised with a magic 3rd identifier.
It also assumes the presence of a structure member named vtable.
The underlying issue with all of these is the _undocumented_ nature of the assumptions, which is certainly a bug, however those assumptions are not in themselves "unreasonable" as was claimed.
Quoted 7 lines
Quoted 2 lines
I think if you intend to remove something on this basis you need to be specific about what you believe the short comings are.
GNUism in header file claiming C99 strictness
If vtable isn't the first element in the structure, it follows a wild pointer on error.
Thank you. Both of these and the lack of documentation should have been spelled out in the original commit message as reasons for the removal.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
and as such is only usable by its sole caller.
"not usable by every imaginable caller" is not the same as "usable by one
single possible caller", I think you are overstating the case here.It is genuinely harder to reverse engineer how to use that macro than to opencode a sane alternative.
A consequence of the lack of documentation rather than any inherent unreasonableness of the provision of such a helper.
BTW your patch removes the logging on failure to allocate, which should either be fixed or called out in the commit message.
Quoted 1 line
I stand firmly by my statement.
You might reasonably stand by your actual reasons for removing this macro, but the original commit message didn't contain them.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Ian Campbell writes ("Re: [PATCH] tools/toollog: Drop XTL_NEW_LOGGER()"):
Quoted 3 lines
The underlying issue with all of these is the _undocumented_ nature of the assumptions, which is certainly a bug, however those assumptions are not in themselves "unreasonable" as was claimed.
Maybe I should submit a counter-patch providing documentation.
Quoted 2 lines
Quoted 2 lines
If vtable isn't the first element in the structure, it follows a wild pointer on error.
This could be fixed.
Quoted 5 lines
Thank you. Both of these and the lack of documentation should have been spelled out in the original commit message as reasons for the removal.
BTW your patch removes the logging on failure to allocate, which should either be fixed or called out in the commit message.
I don't think this is a good idea.
Ian.
Ian Jackson writes ("Re: [PATCH] tools/toollog: Drop XTL_NEW_LOGGER()"):
Quoted 5 lines
Ian Campbell writes ("Re: [PATCH]
tools/toollog: DropXTL_NEW_LOGGER()"):Quoted 2 lines
Quoted 2 lines
If vtable isn't the first element in the structure, it follows a wild pointer on error.
This could be fixed.
Actually, no it couldn't because the vtable has to come first anyway.
Ian.
Ian Jackson writes ("Re: [PATCH] tools/toollog: Drop XTL_NEW_LOGGER()"):
Quoted 6 lines
Ian Campbell writes ("Re: [PATCH]
tools/toollog: DropXTL_NEW_LOGGER()"):Quoted 3 lines
The underlying issue with all of these is the _undocumented_ nature of the assumptions, which is certainly a bug, however those assumptions are not in themselves "unreasonable" as was claimed.
Maybe I should submit a counter-patch providing documentation.
I think this macro is useful because if you wanted to write (say) xtl_logger_syslog, you would want to use it to help you with some boilerplate.
Ian.
Quoted 1 line
From
f749eea51c35c787b8ca7514a21ac145e2946ff8Mon Sep 17 00:00:00 2001
From: Ian Jackson <[email protected]>
Date: Tue, 19 Jan 2016 17:29:30 +0000
Subject: [PATCH] xentoollog: Document XTL_NEW_LOGGER convenience macro
--- tools/libs/toollog/include/xentoollog.h | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
tools/libs/toollog/include/xentoollog.h +53 -0
@@ -113,6 +113,59 @@ void xtl_progress(struct xentoollog_logger *logger,const char *xtl_level_to_string (xentoollog_level );/* never fails */ +/* +* To use this macro: +* +* Define your own logger struct, containing the vtable. +* +* typedef struct { +* xentoollog_logger vtable; // must come first +* [ state your logger needs ] +* } xentoolog_logger_mine; +* +* Write the logging functions: +* +* static void mine_vmessage([ see above ]); +* static void mine_progress([ see above ]); +* static void mine_destroy(struct xentoollog_logger *logger); +* +* Write a constructor: +* +* mine_xentoollog_logger *tl_createlogger_mine([whatever]) { +* mine_xentoolllog_logger newlogger; +* +* [ fill in fields of newlogger ] +* +* return XTL_NEW_LOGGER(mine, newlogger); +* } +* +* If newlogger contains resources that might need to be released, +* the constructor must check the return value from XTL_NEW_LOGGER: +* if it is NULL, the constructor must release the resources. +* +* +* Formally: +* +* xentoollog_logger_MINE* +* XTL_NEW_LOGGER(MINE, xentoollog_logger_MINE contents); +* +* Fills in contents.vtable. Allocates a new struct. Copies +* contents into it. Finally, returns a pointer to the copy. +* +* If allocation fails, uses contents to report this failure, and +* returns NULL. +* +* Expects that xentoollog_logger_MINE is a struct whose +* first member is +* xentoollog_logger vtable; +* +* Expects that +* MINE_vmessage +* MINE_progress +* MINE_destroy +* are in scope, with types compatible with the vtable members. +* +*/ #define XTL_NEW_LOGGER (LOGGER ,buffer) ({ \ xentoollog_logger_##LOGGER *new_consumer; \ \
Signature · 1 line
1.7.10.4
Quoted 9 lines · Ian Jackson
Ian Jackson writes ("Re: [PATCH]
tools/toollog: DropXTL_NEW_LOGGER()"):Quoted 5 lines
Ian Campbell writes ("Re: [PATCH]
tools/toollog: DropXTL_NEW_LOGGER()"):Quoted 3 lines
The underlying issue with all of these is the _undocumented_ nature of the assumptions, which is certainly a bug, however those assumptions are not in themselves "unreasonable" as was claimed.
Maybe I should submit a counter-patch providing documentation.
I think this macro is useful because if you wanted to write (say)
xtl_logger_syslog, you would want to use it to help you with some boilerplate.
WTF? Even documented, the behaviour of this macro is insane, which is why I am trying to kill it. After this, I will also be fixing the gross pointer abuse which exists in the xentoollog internals, before the ABI becomes fixed in 4.7.
There should be no place for code like this, and certainly not in the clean API/ABI we are trying to create out of the mess which is libxc.
Irrespective of whether you disagree with my opinions here, xentoollog.h is specified to be C99 -strict, meaning no GNUisms.
~Andrew
Andrew Cooper writes ("Re: [PATCH] tools/toollog: Drop XTL_NEW_LOGGER()"):
Quoted 9 lines · Ian Jackson
Quoted 3 lines
I think this macro is useful because if you wanted to write (say)
xtl_logger_syslog, you would want to use it to help you with some boilerplate.WTF? Even documented, the behaviour of this macro is insane, which is why I am trying to kill it. After this, I will also be fixing the gross pointer abuse which exists in the xentoollog internals, before the ABI becomes fixed in 4.7.
I think the behaviour of this macro is perfectly sane.
I think your reference to `gross pointer abuse' is to the casting from the specific to the generic struct. This is a completely standard technique for oopy stuff in C. Here is a whole library (quite a nice neat library, in fact) that uses it:
http://www.lysator.liu.se/liboop/
http://www.lysator.liu.se/liboop/ref.html
Quoted 2 lines
Irrespective of whether you disagree with my opinions here,
xentoollog.his specified to be C99 -strict, meaning no GNUisms.
Specified where ?
Anyway, there is no requirement to use this macro. If someone wants to write a strict C99 xtl logger then they can do it by hand. (I predict that no-one will want to do that.) So there is clearly no actual reason why this macro ought to be pure C99.
Ian.
Quoted 7 lines · Ian Jackson
Andrew Cooper writes ("Re: [PATCH]
tools/toollog: DropXTL_NEW_LOGGER()"):Quoted 5 lines · Ian Jackson
Quoted 3 lines
I think this macro is useful because if you wanted to write (say)
xtl_logger_syslog, you would want to use it to help you with some boilerplate.
Quoted 7 lines
Quoted 5 lines
WTF? Even documented, the behaviour of this macro is insane, which is why I am trying to kill it. After this, I will also be fixing the gross
pointer abuse which exists in the xentoollog internals, before the ABI becomes fixed in 4.7.I think the behaviour of this macro is perfectly sane.
It's clearly not "insane", that's for sure, and statements of such opinions along those lines in the commit message (as oppose to concrete issues) are why I found the original patch unsuitable to apply.
Quoted 7 lines
I think your reference to `gross pointer abuse' is to the casting from the specific to the generic struct. This is a completely standard technique for oopy stuff in C. Here is a whole library (quite a nice neat library, in fact) that uses it:
http://www.lysator.liu.se/liboop/
http://www.lysator.liu.se/liboop/ref.html
Quoted 5 lines
Quoted 3 lines
Irrespective of whether you disagree with my opinions here,
xentoollog.h
is specified to be C99 -strict, meaning no GNUisms.Specified where ?
Sort of in "tools: Arrange to check public headers for ANSI compatiblity" (yet to be applied) where I add checks that the headers are clean per gcc's -ansi flag, which is equivalent to -std=c90.
This was done to support users of the library on platforms with lagging compilers, specifically MS VCC lacks some stuff more modern C stuff (like variable length arrays IIRC, the details were in one of the threads on that patch)
This check doesn't cover #defines though.
Quoted 4 lines
Anyway, there is no requirement to use this macro. If someone wants to write a strict C99 xtl logger then they can do it by hand. (I predict that no-one will want to do that.) So there is clearly no actual reason why this macro ought to be pure C99.
Now that the macro is documented I think the GNUism is the only remaining concern which has been put forward which I think needs addressing any further and I agree with the logic that it is a #define so if you want to use an non-gnu compiler you don't get the handy macro.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel