diff mbox

[avr] Tidy up avr-log.c

Message ID 54EC7802.9030701@gjlay.de
State New
Headers show

Commit Message

Georg-Johann Lay Feb. 24, 2015, 1:09 p.m. UTC
avr-log.c and respective macros in avr-protos.h still assume that the 
implementation language is C90, i.e. no variadic macros are available.

This patch cleans up the code from the cumbersome old approach and uses 
variadic macros for avr_dump, avr_edump and avr_fdump.

Ok for trunk?

Johann


	Use variadic macros with avr-log.c.

	* config/avr/avr-protos.h (avr_vdump): New prototype.
	(avr_log_set_caller_e, avr_log_set_caller_f): Remove protos.
	(avr_edump, avr_fdump, avr_dump): (Re)define to use avr_vdump.
	* config/avr/avr-log.c: Adjust comments.
	(avr_vdump): New function.
	(avr_vadump): Pass caller as 2nd argument instead of format string.
	(avr_log_caller, avr_log_fdump_e, avr_log_fdump_f)
	(avr_log_set_caller_e, avr_log_set_caller_f): Remove.

Comments

Denis Chertykov Feb. 25, 2015, 8:50 a.m. UTC | #1
2015-02-24 16:09 GMT+03:00 Georg-Johann Lay <avr@gjlay.de>:
> avr-log.c and respective macros in avr-protos.h still assume that the
> implementation language is C90, i.e. no variadic macros are available.
>
> This patch cleans up the code from the cumbersome old approach and uses
> variadic macros for avr_dump, avr_edump and avr_fdump.
>
> Ok for trunk?
>
> Johann
>
>
>         Use variadic macros with avr-log.c.
>
>         * config/avr/avr-protos.h (avr_vdump): New prototype.
>         (avr_log_set_caller_e, avr_log_set_caller_f): Remove protos.
>         (avr_edump, avr_fdump, avr_dump): (Re)define to use avr_vdump.
>         * config/avr/avr-log.c: Adjust comments.
>         (avr_vdump): New function.
>         (avr_vadump): Pass caller as 2nd argument instead of format string.
>         (avr_log_caller, avr_log_fdump_e, avr_log_fdump_f)
>         (avr_log_set_caller_e, avr_log_set_caller_f): Remove.

Ok.

Denis.
diff mbox

Patch

Index: config/avr/avr-log.c
===================================================================
--- config/avr/avr-log.c	(revision 220854)
+++ config/avr/avr-log.c	(working copy)
@@ -43,13 +43,11 @@ 
 
 /* This file supplies some functions for AVR back-end developers
    with a printf-like interface.  The functions are called through
-   macros avr_edump or avr_fdump from avr-protos.h:
-
-      avr_edump (const char *fmt, ...);
-
-      avr_fdump (FILE *stream, const char *fmt, ...);
+   macros `avr_dump', `avr_edump' or `avr_fdump' from avr-protos.h:
 
+   avr_fdump (FILE *stream, const char *fmt, ...);
    avr_edump (fmt, ...) is a shortcut for avr_fdump (stderr, fmt, ...)
+   avr_dump (fmt, ...)  is a shortcut for avr_fdump (dump_file, fmt, ...)
 
   == known %-codes ==
 
@@ -85,76 +83,41 @@  macros avr_edump or avr_fdump from avr-p
 /* Set according to -mlog= option.  */
 avr_log_t avr_log;
 
-/* The caller as of __FUNCTION__ */
-static const char *avr_log_caller = "?";
-
 /* The worker function implementing the %-codes */
 static void avr_log_vadump (FILE*, const char*, va_list);
 
-/* As we have no variadic macros, avr_edump maps to a call to
-   avr_log_set_caller_e which saves __FUNCTION__ to avr_log_caller and
-   returns a function pointer to avr_log_fdump_e.  avr_log_fdump_e
-   gets the printf-like arguments and calls avr_log_vadump, the
-   worker function.  avr_fdump works the same way.  */
-
-/* Provide avr_log_fdump_e/f so that avr_log_set_caller_e/_f can return
-   their address.  */
-
-static int
-avr_log_fdump_e (const char *fmt, ...)
-{
-  va_list ap;
-
-  va_start (ap, fmt);
-  avr_log_vadump (stderr, fmt, ap);
-  va_end (ap);
-
-  return 1;
-}
+/* Wrapper for avr_log_vadump.  If STREAM is NULL we are called by avr_dump,
+   i.e. output to dump_file if available.  The 2nd argument is __FUNCTION__.
+   The 3rd argument is the format string. */
 
-static int
-avr_log_fdump_f (FILE *stream, const char *fmt, ...)
+int
+avr_vdump (FILE *stream, const char *caller, ...)
 {
   va_list ap;
+        
+  if (NULL == stream && dump_file)
+    stream = dump_file;
 
-  va_start (ap, fmt);
+  va_start (ap, caller);
   if (stream)
-    avr_log_vadump (stream, fmt, ap);
+    avr_log_vadump (stream, caller, ap);
   va_end (ap);
 
   return 1;
 }
 
-/* Macros avr_edump/avr_fdump map to calls of the following two functions,
-   respectively.  You don't need to call them directly.  */
-
-int (*
-avr_log_set_caller_e (const char *caller)
-     )(const char*, ...)
-{
-  avr_log_caller = caller;
-
-  return avr_log_fdump_e;
-}
-
-int (*
-avr_log_set_caller_f (const char *caller)
-     )(FILE*, const char*, ...)
-{
-  avr_log_caller = caller;
-
-  return avr_log_fdump_f;
-}
-
 
 /* Worker function implementing the %-codes and forwarding to
    respective print/dump function.  */
 
 static void
-avr_log_vadump (FILE *file, const char *fmt, va_list ap)
+avr_log_vadump (FILE *file, const char *caller, va_list ap)
 {
   char bs[3] = {'\\', '?', '\0'};
 
+  /* 3rd proper argument is always the format string.  */
+  const char *fmt = va_arg (ap, const char*);
+
   while (*fmt)
     {
       switch (*fmt++)
@@ -256,7 +219,7 @@  avr_log_vadump (FILE *file, const char *
               break;
 
             case 'F':
-              fputs (avr_log_caller, file);
+              fputs (caller, file);
               break;
 
             case 'H':
@@ -280,7 +243,7 @@  avr_log_vadump (FILE *file, const char *
               /* FALLTHRU */
 
             case '?':
-              avr_log_fdump_f (file, "%F[%f:%P]");
+              avr_vdump (file, caller, "%F[%f:%P]");
               break;
 
             case 'P':
Index: config/avr/avr-protos.h
===================================================================
--- config/avr/avr-protos.h	(revision 220854)
+++ config/avr/avr-protos.h	(working copy)
@@ -155,12 +155,11 @@  extern bool avr_have_dimode;
 
 /* From avr-log.c */
 
-#define avr_edump (avr_log_set_caller_e (__FUNCTION__))
-#define avr_fdump (avr_log_set_caller_f (__FUNCTION__))
-
-extern int (*avr_log_set_caller_e (const char*))(const char*, ...);
-extern int (*avr_log_set_caller_f (const char*))(FILE*, const char*, ...);
+#define avr_dump(...) avr_vdump (NULL, __FUNCTION__, __VA_ARGS__)
+#define avr_edump(...) avr_vdump (stderr, __FUNCTION__, __VA_ARGS__)
+#define avr_fdump(FIL, ...) avr_vdump (FIL, __FUNCTION__, __VA_ARGS__)
 
+extern int avr_vdump (FILE*, const char*, ...);
 extern void avr_log_set_avr_log (void);
 
 typedef struct