/*BHEADER**********************************************************************
 * Copyright (c) 2008,  Lawrence Livermore National Security, LLC.
 * Produced at the Lawrence Livermore National Laboratory.
 * This file is part of HYPRE.  See file COPYRIGHT for details.
 *
 * HYPRE 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) version 2.1 dated February 1999.
 *
 * $Revision: 2.4 $
 ***********************************************************************EHEADER*/


#include "utilities.h"

int hypre__global_error = 0;

/* Process the error with code ierr raised in the given line of the
   given source file. */
void hypre_error_handler(char *filename, int line, int ierr)
{
   hypre_error_flag |= ierr;

#ifdef HYPRE_PRINT_ERRORS
   fprintf(stderr,
           "hypre error in file \"%s\", line %d, error code = %d ",
           filename, line, ierr);
#endif
}

int HYPRE_GetError()
{
   return hypre_error_flag;
}

int HYPRE_CheckError(int ierr, int hypre_error_code)
{
   return ierr & hypre_error_code;
}

void HYPRE_DescribeError(int ierr, char *msg)
{
   if (ierr == 0)
      sprintf(msg,"[No error] ");

   if (ierr & HYPRE_ERROR_GENERIC)
      sprintf(msg,"[Generic error] ");

   if (ierr & HYPRE_ERROR_MEMORY)
      sprintf(msg,"[Memory error] ");

   if (ierr & HYPRE_ERROR_ARG)
      sprintf(msg,"[Error in argument %d] ", HYPRE_GetErrorArg());

   if (ierr & HYPRE_ERROR_CONV)
      sprintf(msg,"[Method did not converge] ");
}

int HYPRE_GetErrorArg()
{
   return (hypre_error_flag>>3 & 31);
}
