1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* Error functions
*
* Copyright (c) 2008-2014, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* This software is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software 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 Lesser General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <memory.h>
#include <types.h>
#if defined( HAVE_STDARG_H ) || defined( WINAPI )
#include <stdarg.h>
#elif defined( HAVE_VARARGS_H )
#include <varargs.h>
#else
#error Missing headers stdarg.h and varargs.h
#endif
#include "pyewf_error.h"
#include "pyewf_libcerror.h"
#include "pyewf_libcstring.h"
#include "pyewf_python.h"
#if defined( HAVE_STDARG_H ) || defined( WINAPI )
#define VARARGS( function, error, exception_object, type, argument ) \
function( error, exception_object, type argument, ... )
#define VASTART( argument_list, type, name ) \
va_start( argument_list, name )
#define VAEND( argument_list ) \
va_end( argument_list )
#elif defined( HAVE_VARARGS_H )
#define VARARGS( function, error, exception_object, type, argument ) \
function( error, exception_object, va_alist ) va_dcl
#define VASTART( argument_list, type, name ) \
{ type name; va_start( argument_list ); name = va_arg( argument_list, type )
#define VAEND( argument_list ) \
va_end( argument_list ); }
#endif
/* Raises an error
*/
void VARARGS(
pyewf_error_raise,
libcerror_error_t *error,
PyObject *exception_object,
const char *,
format_string )
{
va_list argument_list;
char error_string[ PYEWF_ERROR_STRING_SIZE ];
char exception_string[ PYEWF_ERROR_STRING_SIZE ];
static char *function = "pyewf_error_raise";
size_t error_string_index = 0;
int print_count = 0;
if( format_string == NULL )
{
PyErr_Format(
PyExc_ValueError,
"%s: missing format string.",
function );
return;
}
VASTART(
argument_list,
const char *,
format_string );
print_count = PyOS_vsnprintf(
exception_string,
PYEWF_ERROR_STRING_SIZE,
format_string,
argument_list );
VAEND(
argument_list );
if( print_count < 0 )
{
PyErr_Format(
PyExc_ValueError,
"%s: unable to format exception string.",
function );
return;
}
if( error != NULL )
{
if( libcerror_error_backtrace_sprint(
error,
error_string,
PYEWF_ERROR_STRING_SIZE ) != -1 )
{
while( error_string_index < PYEWF_ERROR_STRING_SIZE )
{
if( error_string[ error_string_index ] == 0 )
{
break;
}
if( error_string[ error_string_index ] == '\n' )
{
error_string[ error_string_index ] = ' ';
}
error_string_index++;
}
if( error_string_index >= PYEWF_ERROR_STRING_SIZE )
{
error_string[ PYEWF_ERROR_STRING_SIZE - 1 ] = 0;
}
PyErr_Format(
exception_object,
"%s %s",
exception_string,
error_string );
return;
}
}
PyErr_Format(
exception_object,
"%s",
exception_string );
return;
}
#undef VARARGS
#undef VASTART
#undef VAEND
|