bug.c 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* 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
10 11 12 13
 * the terms of any of the Free Software licenses or 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.
14 15 16 17 18
 *
 * 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.
19
 * See https://www.nexedi.com/licensing for rationale and options.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 */

/* 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();
}


48
void __bug_err(const char *file, unsigned line, const char *func, int err)
49
{
50 51
    char err_buf[128];
    char *err_str;
52

53 54
    err_str = strerror_r(err, err_buf, sizeof(err_buf));
    fprintf(stderr, "%s:%u %s\tBUG! (%s)\n", file, line, func, err_str);
55 56 57 58
    abort();
}


59 60 61 62 63 64
void __bug_errno(const char *file, unsigned line, const char *func)
{
    __bug_err(file, line, func, errno);
}


65 66 67 68 69 70 71 72 73 74 75
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();
}