/*   ncbierr.h
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name:  ncbierr.c
*
* Author:  Schuler, Sirotkin (UserErr stuff)
*
* Version Creation Date:   9-19-91
*
* $Revision: 2.6 $
*
* File Description:  Error handling functions
*
* Modifications:
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
* ==========================================================================
*/

#ifndef _NCBIERR_
#define _NCBIERR_

#ifdef __cplusplus
extern "C" {
#endif

/*
 * define missing ANSI-isms if necessary
 */
#ifndef __FILE__
#define __FILE__    ""
#endif
#ifndef __LINE__
#define __LINE__    0
#endif
#ifndef __DATE__
#define __DATE__    ""
#endif
#ifndef __TIME__
#define __TIME__    ""
#endif


#ifndef THIS_FILE
#define THIS_FILE	__FILE__
#endif

#define ERRTEXT_MAX  512

/* actopt codes */
#define ERR_CONTINUE  1
#define ERR_IGNORE    2
#define ERR_ADVISE    3
#define ERR_ABORT     4
#define ERR_PROMPT    5
#define ERR_TEE       6

/* logopt codes */
#define ERR_LOG_ON    1
#define ERR_LOG_OFF   2

/* error context codes */
#define CTX_DEBUG      0
#define CTX_UNKNOWN    1
#define CTX_ERRNO      2
#define CTX_NCBICORE   64
#define CTX_NCBIASN1   65
#define CTX_NCBICD	   66
#define CTX_NCBIOBJ    67
#define CTX_NCBILMA    68
#define CTX_NCBIGBPARSE 69
#define CTX_NCBIPIRPARSE 70
#define CTX_NCBI2GB    71
#define CTX_NCBIBB2ASN 72
#define CTX_NCBIMATH   73
#define CTX_NCBIMED2ASN 74
#define CTX_NCBISEQENTRY 75
#define CTX_NCBISEQPORT   200
#define CTX_NCBIIDLOAD 300
#define CTX_NCBIIDPROCESS 301
#define CTX_NCBIIDRETRIEVE 302
#define CTX_NCBIAUTHINPARSE 303
#define CTX_KB2ASN 304

#define CTX_RESERVED   10000
/* context codes > 10000 are available for application use */
#define CTX_USER	   32767    /* default user application context */

/* error codes for CTX_NCBICORE */
#define CORE_UNKNOWN    1
#define CORE_NULLPTR    2    /* NULL pointer passed as an argument */
#define CORE_NULLHND    3    /* NULL handle passed as an argument */
#define CORE_MEMORY     4    /* Memory allocation failure */
#define CORE_BAD_COOKIE 5    /* ErrPost User install misused */
#define CORE_BAD_SGML   6    /* SGML entity or range error */

#define CORE_FILE_ACCESS  10  /* Error accessinf file info (e.g. with fstat()) */
#define CORE_FILE_CREATE  11  /* Error creating file */
#define CORE_FILE_OPEN    12  /* Error opening file */
#define CORE_FILE_READ    13  /* Error reading file */
#define CORE_FILE_WRITE   14  /* Error writing file */
#define CORE_FILE_ERROR   15  /* any other file I/O error */


/* ======== TYPE DEFINITIONS ======== */

typedef struct _ErrDesc {
    int   context;
    int   errcode;
	char  errtext [ERRTEXT_MAX+1];
	char  srcfile [PATH_MAX+1];
    int   srcline;
    char  srcdate [16];
    char  srctime [16];
} ErrDesc, PNTR ErrDescPtr;

typedef void (LIBCALLBACK *ErrHookProc) PROTO((const ErrDesc *err));


/* ======== EXPORTED VARIABLES ======== */
/* WARNING: these definitions will be removed soon.  */
extern ErrDesc __errbuf;
extern ErrDesc __errtmp;

/* ======== Function Prototypes ======== */
void CDECL Nlm_ErrPost VPROTO((int ctx, int code, const char *fmt, ...));
void LIBCALL Nlm_ErrPostStr PROTO((int ctx, int code, const char *str));
void LIBCALL NLm_ErrFileAndLine PROTO((const char *file, int line, int dbflag));
void LIBCALL Nlm_ErrGetOpts PROTO((short * actopt, short * logopt));
void LIBCALL Nlm_ErrSetOpts PROTO((short actopt, short logopt));
Nlm_Boolean LIBCALL Nlm_ErrSetLog PROTO((const char *filename));
Nlm_Boolean LIBCALL Nlm_ErrShow PROTO((void));
Nlm_Boolean LIBCALL Nlm_ErrFetch PROTO((const ErrDescPtr edp));
Nlm_Uint1 LIBCALL Nlm_ErrUserInstall PROTO(( CharPtr msg, Uint1 magic_cookie));
Nlm_Boolean LIBCALL Nlm_ErrUserDelete PROTO(( Uint1 magic_cookie));
Nlm_Boolean LIBCALL Nlm_ErrUserClear PROTO(( void));

void LIBCALL Nlm_AbnormalExit PROTO((int));
void LIBCALL Nlm_AssertionFailed PROTO((const char *,const char *,int));
void LIBCALL Nlm_TraceStr PROTO((const char*));
void CDECL   Nlm_Trace VPROTO((const char *, ...));

ErrHookProc LIBCALL Nlm_ErrSetHandler PROTO((ErrHookProc));

/* ======== Macros ======== */
#define _ErrGetOpts  Nlm_ErrGetOpts
#define ErrGetOpts  Nlm_ErrGetOpts
#define _ErrSetOpts  Nlm_ErrSetOpts
#define ErrSetOpts  Nlm_ErrSetOpts
#define _ErrSetLog   Nlm_ErrSetLog
#define ErrSetLog   Nlm_ErrSetLog
#define _ErrPost     Nlm_ErrPost
#define ErrPost     Nlm_ErrPost
#define _ErrShow     Nlm_ErrShow
#define ErrShow     Nlm_ErrShow
#define _ErrFetch    Nlm_ErrFetch
#define ErrFetch    Nlm_ErrFetch
#define ErrClear     memset(__error, 0, sizeof(ErrDesc))
#define ErrSetHandler	Nlm_ErrSetHandler

#define ERRPOST(x)   strncpy(__errtmp.srcfile,__FILE__,PATH_MAX), \
					__errtmp.srcline = __LINE__, \
                     Nlm_ErrPost x

#define Exit			Nlm_AbnormalExit	/* OBSOLETE */
#define AbnormalExit	Nlm_AbnormalExit

#ifndef TRACE
#ifdef _DEBUG
#define TRACE	Nlm_Trace
#else
#define TRACE	1 ? (void)0 : Nlm_Trace
#endif
#endif

#ifndef ASSERT
#ifdef _DEBUG
#define ASSERT(expr)	((expr) ? (void)0 : \
						Nlm_AssertionFailed(#expr,THIS_FILE,__LINE__))
#define VERIFY(expr)	ASSERT(expr)
#else
#define ASSERT(expr)	((void)0)
#define VERIFY(expr)	((void)(expr))
#endif
#endif

#ifdef __cplusplus
}
#endif

#endif
