/*****************************************************************************
 * stacktrace.h
 *
 * This file is used to link a normal program with the stacktrace library.
 *
 * ---------------------------------------------------------------------------
 * stacktrace - Stacktrace printer.
 *   (C) 2008-2010 Gerardo García Peña <gerardo@kung-foo.net>
 *
 *   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 the Free Software Foundation, Inc., 51
 *   Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#ifndef __STACKTRACE_H__
#define __STACKTRACE_H__

#include <stdarg.h>
#include <stdio.h>

/* use 'stacktrace_init()' at program startup.                             */
/* From then you can use 'stacktrace()' whenever you want to print nice    */
/* stacktraces of your program.                                            */
void stacktrace_init(int signal_hook);
void stacktrace_init_siglist(int signal_hook, ...);
void stacktrace_version_get(int *maj, int *min, int *rev);
int  stacktrace_version_check(int maj, int min, int rev);
void stacktrace_set_file(FILE *file);
void stacktrace(void);

/* messages (for debugging purposes) */
#ifdef __STACKTRACE_MSG_MACROS__
void stacktrace_msg(char *p, char *f, ...) __attribute__ ((__format__ (__printf__, 2, 3)));

#define ERR(...) stacktrace_msg("stacktrace: error: ",   __VA_ARGS__)
#define WRN(...) stacktrace_msg("stacktrace: warning: ", __VA_ARGS__)
#define MSG(...) stacktrace_msg("stacktrace:",           __VA_ARGS__)

#define ERR_ERRNO(f, ...)     {                                               \
                                char __ed[255];                               \
                                strerror_r(errno,  __ed, sizeof(__ed));       \
                                ERR(f ": %s", ## __VA_ARGS__, __ed);          \
                              }
#define FAT_ERRNO(f, ...)     {                                               \
                                char __ed[255];                               \
                                strerror_r(errno,  __ed, sizeof(__ed));       \
                                FAT(f ": %s", ## __VA_ARGS__, __ed);          \
                              }
#endif

/* Not for normal use - use 'stacktrace_init()' instead (dont worry for    */
/* finalization, it is performed via atexit() at program exit).            */
/*                                                                         */
/* Init and finit internal functions are only used when atexit() mechanism */
/* is not the most suitable method (LD_PRELOAD and so).                    */
void stacktrace_init_internal(int signal_hook, va_list siglist);
void stacktrace_fini_internal(void);

#endif
