Commit db28e53f authored by Kirill Smelkov's avatar Kirill Smelkov

lib/bug: Small utilities to say there is a warning, or a todo, or a bug on condition

Modelled by ones used in Linux kernel.
parent 5755a6b3
#ifndef _WENDELIN_BUG_H_
#define _WENDELIN_BUG_H_
/* Wendelin. Utilities for handling assertions and bugs
* Copyright (C) 2014-2015 Nexedi SA and Contributors.
* Kirill Smelkov <kirr@nexedi.com>
*
* This program is free software: you can Use, Study, Modify and Redistribute
* it under the terms of the GNU General Public License version 3, or (at your
* option) any later version, as published by the Free Software Foundation.
*
* You can also Link and Combine this program with other software covered by
* the terms of any of the Open Source Initiative approved licenses and Convey
* the resulting work. Corresponding source of such a combination shall include
* the source code for all other software used.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING file for full licensing terms.
*/
// XXX maybe not needed - just go with std assert()
#define TODO(expr) do { \
if (expr) \
__todo(#expr, __FILE__, __LINE__, __func__); \
} while (0)
#define WARN(msg) do { \
__warn(__FILE__, __LINE__, __func__, msg); \
} while (0)
#define BUG() do { \
__bug(__FILE__, __LINE__, __func__); \
} while (0)
#define BUGe() do { \
__bug_errno(__FILE__, __LINE__, __func__); \
} while (0)
/* like assert(expr) but works indepenently of NDEBUG */
// TODO unlikely
#define ASSERT(expr) do { \
if (!(expr)) \
__bug_fail(#expr, __FILE__, __LINE__, __func__); \
} while (0)
/* =ASSERT(!expr) */
#define BUG_ON(expr) ASSERT(!(expr))
void __todo(const char *, const char *, unsigned, const char *)
__attribute__((noreturn));
void __warn(const char *, unsigned, const char *, const char *);
void __bug(const char *, unsigned, const char *)
__attribute__((noreturn));
void __bug_errno(const char *, unsigned, const char *)
__attribute__((noreturn));
void __bug_fail(const char *, const char *, unsigned, const char *)
__attribute__((noreturn));
#endif
/* Wendelin. Utilities for handling assertions and bugs
* Copyright (C) 2014-2015 Nexedi SA and Contributors.
* Kirill Smelkov <kirr@nexedi.com>
*
* This program is free software: you can Use, Study, Modify and Redistribute
* it under the terms of the GNU General Public License version 3, or (at your
* option) any later version, as published by the Free Software Foundation.
*
* You can also Link and Combine this program with other software covered by
* the terms of any of the Open Source Initiative approved licenses and Convey
* the resulting work. Corresponding source of such a combination shall include
* the source code for all other software used.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING file for full licensing terms.
*/
/* first include with NDEBUG unset to get __assert_* prototypes */
#undef NDEBUG
#include <assert.h>
#include <wendelin/bug.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void __bug_fail(const char *expr, const char *file, unsigned line, const char *func)
{
/* just tail to libc */
__assert_fail(expr, file, line, func);
}
void __bug(const char *file, unsigned line, const char *func)
{
fprintf(stderr, "%s:%u %s\tBUG!\n", file, line, func);
abort();
}
void __bug_errno(const char *file, unsigned line, const char *func)
{
char errno_buf[128];
char *errno_str;
errno_str = strerror_r(errno, errno_buf, sizeof(errno_buf));
fprintf(stderr, "%s:%u %s\tBUG! (%s)\n", file, line, func, errno_str);
abort();
}
void __warn(const char *file, unsigned line, const char *func, const char *msg)
{
fprintf(stderr, "%s:%u %s WARN: %s\n", file, line, func, msg);
}
void __todo(const char *expr, const char *file, unsigned line, const char *func)
{
fprintf(stderr, "%s:%u %s\tTODO %s\n", file, line, func, expr);
abort();
}
......@@ -27,6 +27,7 @@ import os
_bigfile = Extension('wendelin.bigfile._bigfile',
sources = [
'bigfile/_bigfile.c',
'lib/bug.c',
],
include_dirs = [
'./include',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment