1  /*
   2   * \brief  Interface of the printf backend
   3   * \author Norman Feske
   4   * \date   2006-04-08
   5   */

   6  
   7  /*
   8   * Copyright (C) 2006-2010 Genode Labs GmbH
   9   *
  10   * This file is part of the Genode OS framework, which is distributed
  11   * under the terms of the GNU General Public License version 2.
  12   */

  13  
  14  #ifndef _INCLUDE__BASE__PRINTF_H_
  15  #define _INCLUDE__BASE__PRINTF_H_
  16  
  17  #include <stdarg.h>
  18  
  19  namespace Genode {
  20  
  21     /**
  22      * For your convenience...
  23      */

  24     void  printf(const char *format, ...);

  25     void vprintf(const char *format, va_list);
  26  
  27     /** Format-string checker */
  28     extern "C" void format_check(const char *format, ...) __attribute__((format(printf, 1, 2)));
  29  }

  30  
  31  #define ESC_DBG  "\033[33m"
  32  #define ESC_WRN  "\033[34m"
  33  #define ESC_ERR  "\033[31m"
  34  #define ESC_END  "\033[0m"
  35  
  36  /*
  37   * We`re using heavy CPP wizardry here to prevent compiler errors after macro
  38   * expansion. Each macro works as follows:
  39   *
  40   * - Support one format string plus zero or more arguments.
  41   * - Put all static strings (including the format string) in the first argument
  42   *   of the call to printf() and let the compiler merge them.
  43   * - Append the function name (magic static string variable) as first argument.
  44   * - (Optionally) append the arguments to the macro with ", ##__VA_ARGS__". CPP
  45   *   only appends the comma and arguments if __VA__ARGS__ is not empty,
  46   *   otherwise nothing (not even the comma) is appended.
  47   */

  48  
  49  #define PDBG(fmt, ...)                                       \
  50     do {                                                     \
  51        if (0) Genode::format_check(fmt, ##__VA_ARGS__);     \
  52        Genode::printf("%s: " ESC_DBG fmt ESC_END "\n",      \
  53                       __PRETTY_FUNCTION__, ##__VA_ARGS__ ); \
  54     } while (0)

  55  
  56  #define PWRN(fmt, ...)                                       \
  57     do {                                                     \
  58        if (0) Genode::format_check(fmt, ##__VA_ARGS__);     \
  59        Genode::printf("%s: " ESC_WRN fmt ESC_END "\n",      \
  60                       __PRETTY_FUNCTION__, ##__VA_ARGS__ ); \
  61     } while (0)

  62  
  63  #define PERR(fmt, ...)                                       \
  64     do {                                                     \
  65        if (0) Genode::format_check(fmt, ##__VA_ARGS__);     \
  66        Genode::printf("%s: " ESC_ERR fmt ESC_END "\n",      \
  67                       __PRETTY_FUNCTION__, ##__VA_ARGS__ ); \
  68     } while (0)

  69  
  70  #endif /* _INCLUDE__BASE__PRINTF_H_ */