Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Descriptive errors in C using GCC macros

Function to generate descriptive errors (line number, function name, etc).

   1  
   2  #include <stdlib.h>
   3  #include <stdio.h>
   4  #include <stdarg.h>
   5  #include <string.h>
   6  
   7  #ifdef __OPTIMIZE__
   8  #define __OPT__ 1
   9  #else
  10  #define __OPT__ 0
  11  #endif
  12  
  13  #ifdef __OPTIMIZE_SIZE__
  14  #define __OPT_SIZE__ 1
  15  #else
  16  #define __OPT_SIZE__ 0
  17  #endif
  18  
  19  #define err_print(args...) __err_print(__FILE__, __FUNCTION__, __LINE__, __DATE__ " " __TIME__, __VERSION__, __OPT__, __OPT_SIZE__, ##args)
  20  void __err_print(char *file, char *function, int line, char *date, char *version, int opt, int opt_size, char *txt, ...)
  21  {
  22          va_list argp;
  23  
  24          puts("** ERROR! **");
  25          printf("File:                 \t %s\n",file);
  26          printf("Function:             \t %s\n",function);
  27          printf("Line:                 \t %d\n",line);
  28          printf("Compilation date:     \t %s\n",date);
  29          printf("Compilator version:   \t %s\n",version);
  30          printf("Optimization:         \t %s\n",opt==1 ? "Yes" : "No");
  31          printf("Size optimization:    \t %s\n",opt_size==1 ? "Yes" : "No");
  32  
  33          if (txt == NULL)
  34                  return;
  35          puts("Description:\n>>>");
  36          va_start(argp, txt);
  37          vprintf(txt, argp);
  38          va_end(argp);
  39          puts("\n<<<");
  40  }
  41  
  42  void other_function() {
  43          err_print("other %s (%d+%d=%d)", "description",2,2,5);
  44  }
  45  
  46  int main() {
  47          err_print("test %d", 2);
  48          puts("");
  49          other_function();
  50          return 0;
  51  }

C++ NULL define

// C++ NULL define: better than "#define NULL 0 because" avoids cast warnings

   1  
   2  #define NULL ((void *)0)
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS