libDXF 0.0.1
A library with DXF related functions written in C.
Defines

dbg.h File Reference

Zed's Awesome Debug Macros. More...

#include <stdio.h>
#include <errno.h>
#include <string.h>
Include dependency graph for dbg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define debug(M,...)   fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define clean_errno()   (errno == 0 ? "None" : strerror(errno))
#define log_err(M,...)   fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_warn(M,...)   fprintf(stderr, "[WARNING] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_info(M,...)   fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define check(A, M,...)   if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
#define sentinel(M,...)   { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
#define check_mem(A)   check((A), "Out of memory.")
#define check_debug(A, M,...)   if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }

Detailed Description

Zed's Awesome Debug Macros.

Author:
Zed A. Shaw <help@learncodethehardway.org>

Exerpt from "Learn C the hard way", Exercise 20: Zed's Awesome Debug Macros by Zed A. Shaw <help@learncodethehardway.org>.

Adaptation for libdxf: Copyright (C) 2016, 2017 by Bert Timmerman <bert.timmerman@xs4all.nl>.


Copyright Notices.


This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to:
Free Software Foundation, Inc.,
59 Temple Place,
Suite 330,
Boston,
MA 02111 USA.


Definition in file dbg.h.


Define Documentation

#define debug (   M,
  ... 
)    fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

Definition at line 54 of file dbg.h.

#define clean_errno ( )    (errno == 0 ? "None" : strerror(errno))

The clean_errno macro that's used in the others to get a safe readable version of errno.

Definition at line 62 of file dbg.h.

#define log_err (   M,
  ... 
)    fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

The log_err macro for logging messages meant for the end user.
Works like debug but can't be compiled out.

Definition at line 69 of file dbg.h.

#define log_warn (   M,
  ... 
)    fprintf(stderr, "[WARNING] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

The log_warn macro for logging messages meant for the end user.
Works like debug but can't be compiled out.

Definition at line 75 of file dbg.h.

#define log_info (   M,
  ... 
)    fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

The log_info macro for logging messages meant for the end user.
Works like debug but can't be compiled out.

Definition at line 81 of file dbg.h.

#define check (   A,
  M,
  ... 
)    if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

The check macro will make sure the condition A is true, and if not logs the error M (with variable arguments for log_err), then jumps to the function's error: label for cleanup.

Definition at line 89 of file dbg.h.

#define sentinel (   M,
  ... 
)    { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

The macro sentinel is placed in any part of a function that shouldn't run, and if it does prints an error message then jumps to the error: label.
You put this in if-statements and switch-statements to catch conditions that shouldn't happen, like the default: .

Definition at line 98 of file dbg.h.

#define check_mem (   A)    check((A), "Out of memory.")

A short-hand macro check_mem that makes sure a pointer is valid, and if it isn't reports it as an error with "Out of memory".

Definition at line 105 of file dbg.h.

#define check_debug (   A,
  M,
  ... 
)    if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }

An alternative macro check_debug that still checks and handles an error, but if the error is common then you don't want to bother reporting it.
In this one it will use debug instead of log_err to report the message, so when you define NDEBUG the check still happens, the error jump goes off, but the message isn't printed.

Definition at line 116 of file dbg.h.