~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/error.cc

  • Committer: Tim Penhey
  • Date: 2010-01-22 01:44:35 UTC
  • mto: (1271.7.10)
  • mto: This revision was merged to the branch mainline in revision 1293.
  • Revision ID: tim@penhey.net-20100122014435-siqx681pldi6u1vn
Move the bits from my_error.h and my_error.cc into error.h and error.cc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
 
4
 *  Copyright (C) 2000 MySQL AB
4
5
 *  Copyright (C) 2008 Sun Microsystems
5
6
 *
6
7
 *  This program is free software; you can redistribute it and/or modify
23
24
 
24
25
#include "config.h"
25
26
#include "drizzled/internal/my_sys.h"
26
 
#include "drizzled/my_error.h"
27
 
#include <drizzled/definitions.h>
28
 
#include <drizzled/error.h>
29
 
#include <drizzled/gettext.h>
 
27
#include "drizzled/definitions.h"
 
28
#include "drizzled/error.h"
 
29
#include "drizzled/gettext.h"
30
30
 
31
31
static const char *drizzled_error_messages[]=
32
32
{
1486
1486
  return(false);
1487
1487
}
1488
1488
 
 
1489
// -- From here has just been moved from my_error.cc
 
1490
 
 
1491
/* Error message numbers in global map */
 
1492
const char * globerrs[GLOBERRS];
 
1493
 
 
1494
error_handler_func error_handler_hook= NULL;
 
1495
 
 
1496
 
 
1497
void init_glob_errs()
 
1498
{
 
1499
  EE(EE_CANTCREATEFILE) = N_("Can't create/write to file '%s' (Errcode: %d)");
 
1500
  EE(EE_READ)           = N_("Error reading file '%s' (Errcode: %d)");
 
1501
  EE(EE_WRITE)          = N_("Error writing file '%s' (Errcode: %d)");
 
1502
  EE(EE_BADCLOSE)       = N_("Error on close of '%s' (Errcode: %d)");
 
1503
  EE(EE_OUTOFMEMORY)    = N_("Out of memory (Needed %u bytes)");
 
1504
  EE(EE_DELETE)         = N_("Error on delete of '%s' (Errcode: %d)");
 
1505
  EE(EE_LINK)           = N_("Error on rename of '%s' to '%s' (Errcode: %d)");
 
1506
  EE(EE_EOFERR)         = N_("Unexpected eof found when reading file '%s' (Errcode: %d)");
 
1507
  EE(EE_CANTLOCK)       = N_("Can't lock file (Errcode: %d)");
 
1508
  EE(EE_CANTUNLOCK)     = N_("Can't unlock file (Errcode: %d)");
 
1509
  EE(EE_DIR)            = N_("Can't read dir of '%s' (Errcode: %d)");
 
1510
  EE(EE_STAT)           = N_("Can't get stat of '%s' (Errcode: %d)");
 
1511
  EE(EE_CANT_CHSIZE)    = N_("Can't change size of file (Errcode: %d)");
 
1512
  EE(EE_CANT_OPEN_STREAM)= N_("Can't open stream from handle (Errcode: %d)");
 
1513
  EE(EE_GETWD)          = N_("Can't get working dirctory (Errcode: %d)");
 
1514
  EE(EE_SETWD)          = N_("Can't change dir to '%s' (Errcode: %d)");
 
1515
  EE(EE_LINK_WARNING)   = N_("Warning: '%s' had %d links");
 
1516
  EE(EE_OPEN_WARNING)   = N_("Warning: %d files and %d streams is left open\n");
 
1517
  EE(EE_DISK_FULL)      = N_("Disk is full writing '%s'. Waiting for someone to free space...");
 
1518
  EE(EE_CANT_MKDIR)     = N_("Can't create directory '%s' (Errcode: %d)");
 
1519
  EE(EE_UNKNOWN_CHARSET)= N_("Character set '%s' is not a compiled character set and is not specified in the %s file");
 
1520
  EE(EE_OUT_OF_FILERESOURCES)= N_("Out of resources when opening file '%s' (Errcode: %d)");
 
1521
  EE(EE_CANT_READLINK)= N_("Can't read value for symlink '%s' (Error %d)");
 
1522
  EE(EE_CANT_SYMLINK)= N_("Can't create symlink '%s' pointing at '%s' (Error %d)");
 
1523
  EE(EE_REALPATH)= N_("Error on realpath() on '%s' (Error %d)");
 
1524
  EE(EE_SYNC)=   N_("Can't sync file '%s' to disk (Errcode: %d)");
 
1525
  EE(EE_UNKNOWN_COLLATION)= N_("Collation '%s' is not a compiled collation and is not specified in the %s file");
 
1526
  EE(EE_FILENOTFOUND)   = N_("File '%s' not found (Errcode: %d)");
 
1527
  EE(EE_FILE_NOT_CLOSED) = N_("File '%s' (fileno: %d) was not closed");
 
1528
}
 
1529
 
 
1530
/*
 
1531
  WARNING!
 
1532
  my_error family functions have to be used according following rules:
 
1533
  - if message have not parameters use my_message(ER_CODE, ER(ER_CODE), MYF(N))
 
1534
  - if message registered use my_error(ER_CODE, MYF(N), ...).
 
1535
  - With some special text of errror message use:
 
1536
  my_printf_error(ER_CODE, format, MYF(N), ...)
 
1537
*/
 
1538
 
 
1539
/*
 
1540
  Message texts are registered into a linked list of 'my_err_head' structs.
 
1541
  Each struct contains (1.) an array of pointers to C character strings with
 
1542
  '\0' termination, (2.) the error number for the first message in the array
 
1543
  (array index 0) and (3.) the error number for the last message in the array
 
1544
  (array index (last - first)).
 
1545
  The array may contain gaps with NULL pointers and pointers to empty strings.
 
1546
  Both kinds of gaps will be translated to "Unknown error %d.", if my_error()
 
1547
  is called with a respective error number.
 
1548
  The list of header structs is sorted in increasing order of error numbers.
 
1549
  Negative error numbers are allowed. Overlap of error numbers is not allowed.
 
1550
  Not registered error numbers will be translated to "Unknown error %d.".
 
1551
*/
 
1552
static struct my_err_head
 
1553
{
 
1554
  struct my_err_head    *meh_next;      /* chain link */
 
1555
  const char            **meh_errmsgs;  /* error messages array */
 
1556
  int                   meh_first;      /* error number matching array slot 0 */
 
1557
  int                   meh_last;       /* error number matching last slot */
 
1558
  bool                  is_globerrs;
 
1559
} my_errmsgs_globerrs = {NULL, globerrs, EE_ERROR_FIRST, EE_ERROR_LAST, true};
 
1560
 
 
1561
static struct my_err_head *my_errmsgs_list= &my_errmsgs_globerrs;
 
1562
 
 
1563
 
 
1564
/*
 
1565
   Error message to user
 
1566
 
 
1567
   SYNOPSIS
 
1568
     my_error()
 
1569
       nr       Errno
 
1570
       MyFlags  Flags
 
1571
       ...      variable list
 
1572
*/
 
1573
 
 
1574
void my_error(int nr, myf MyFlags, ...)
 
1575
{
 
1576
  const char *format;
 
1577
  struct my_err_head *meh_p;
 
1578
  va_list args;
 
1579
  char ebuff[ERRMSGSIZE + 20];
 
1580
 
 
1581
  /* Search for the error messages array, which could contain the message. */
 
1582
  for (meh_p= my_errmsgs_list; meh_p; meh_p= meh_p->meh_next)
 
1583
    if (nr <= meh_p->meh_last)
 
1584
      break;
 
1585
 
 
1586
  /* get the error message string. Default, if NULL or empty string (""). */
 
1587
  if (! (format= (meh_p && (nr >= meh_p->meh_first)) ?
 
1588
         _(meh_p->meh_errmsgs[nr - meh_p->meh_first]) : NULL) || ! *format)
 
1589
    (void) snprintf (ebuff, sizeof(ebuff), _("Unknown error %d"), nr);
 
1590
  else
 
1591
  {
 
1592
    va_start(args,MyFlags);
 
1593
    (void) vsnprintf (ebuff, sizeof(ebuff), format, args);
 
1594
    va_end(args);
 
1595
  }
 
1596
  (*error_handler_hook)(nr, ebuff, MyFlags);
 
1597
  return;
 
1598
}
 
1599
 
 
1600
 
 
1601
/*
 
1602
  Error as printf
 
1603
 
 
1604
  SYNOPSIS
 
1605
    my_printf_error()
 
1606
      error     Errno
 
1607
      format    Format string
 
1608
      MyFlags   Flags
 
1609
      ...       variable list
 
1610
*/
 
1611
 
 
1612
void my_printf_error(uint32_t error, const char *format, myf MyFlags, ...)
 
1613
{
 
1614
  va_list args;
 
1615
  char ebuff[ERRMSGSIZE+20];
 
1616
 
 
1617
  va_start(args,MyFlags);
 
1618
  (void) vsnprintf (ebuff, sizeof(ebuff), format, args);
 
1619
  va_end(args);
 
1620
  (*error_handler_hook)(error, ebuff, MyFlags);
 
1621
  return;
 
1622
}
 
1623
 
 
1624
/*
 
1625
  Give message using error_handler_hook
 
1626
 
 
1627
  SYNOPSIS
 
1628
    my_message()
 
1629
      error     Errno
 
1630
      str       Error message
 
1631
      MyFlags   Flags
 
1632
*/
 
1633
 
 
1634
void my_message(uint32_t error, const char *str, register myf MyFlags)
 
1635
{
 
1636
  (*error_handler_hook)(error, str, MyFlags);
 
1637
}
 
1638
 
 
1639
 
 
1640
/*
 
1641
  Register error messages for use with my_error().
 
1642
 
 
1643
  SYNOPSIS
 
1644
    my_error_register()
 
1645
    errmsgs                     array of pointers to error messages
 
1646
    first                       error number of first message in the array
 
1647
    last                        error number of last message in the array
 
1648
 
 
1649
  DESCRIPTION
 
1650
    The pointer array is expected to contain addresses to NUL-terminated
 
1651
    C character strings. The array contains (last - first + 1) pointers.
 
1652
    NULL pointers and empty strings ("") are allowed. These will be mapped to
 
1653
    "Unknown error" when my_error() is called with a matching error number.
 
1654
    This function registers the error numbers 'first' to 'last'.
 
1655
    No overlapping with previously registered error numbers is allowed.
 
1656
 
 
1657
  RETURN
 
1658
    0           OK
 
1659
    != 0        Error
 
1660
*/
 
1661
 
 
1662
int my_error_register(const char **errmsgs, int first, int last)
 
1663
{
 
1664
  struct my_err_head *meh_p;
 
1665
  struct my_err_head **search_meh_pp;
 
1666
 
 
1667
  /* Allocate a new header structure. */
 
1668
  if (! (meh_p= (struct my_err_head*) malloc(sizeof(struct my_err_head))))
 
1669
    return 1;
 
1670
  meh_p->meh_errmsgs= errmsgs;
 
1671
  meh_p->meh_first= first;
 
1672
  meh_p->meh_last= last;
 
1673
  meh_p->is_globerrs= false;
 
1674
 
 
1675
  /* Search for the right position in the list. */
 
1676
  for (search_meh_pp= &my_errmsgs_list;
 
1677
       *search_meh_pp;
 
1678
       search_meh_pp= &(*search_meh_pp)->meh_next)
 
1679
  {
 
1680
    if ((*search_meh_pp)->meh_last > first)
 
1681
      break;
 
1682
  }
 
1683
 
 
1684
  /* Error numbers must be unique. No overlapping is allowed. */
 
1685
  if (*search_meh_pp && ((*search_meh_pp)->meh_first <= last))
 
1686
  {
 
1687
    free((unsigned char*)meh_p);
 
1688
    return 1;
 
1689
  }
 
1690
 
 
1691
  /* Insert header into the chain. */
 
1692
  meh_p->meh_next= *search_meh_pp;
 
1693
  *search_meh_pp= meh_p;
 
1694
  return 0;
 
1695
}
 
1696
 
 
1697
 
 
1698
/*
 
1699
  Unregister formerly registered error messages.
 
1700
 
 
1701
  SYNOPSIS
 
1702
    my_error_unregister()
 
1703
    first                       error number of first message
 
1704
    last                        error number of last message
 
1705
 
 
1706
  DESCRIPTION
 
1707
    This function unregisters the error numbers 'first' to 'last'.
 
1708
    These must have been previously registered by my_error_register().
 
1709
    'first' and 'last' must exactly match the registration.
 
1710
    If a matching registration is present, the header is removed from the
 
1711
    list and the pointer to the error messages pointers array is returned.
 
1712
    Otherwise, NULL is returned.
 
1713
 
 
1714
  RETURN
 
1715
    non-NULL    OK, returns address of error messages pointers array.
 
1716
    NULL        Error, no such number range registered.
 
1717
*/
 
1718
 
 
1719
const char **my_error_unregister(int first, int last)
 
1720
{
 
1721
  struct my_err_head    *meh_p;
 
1722
  struct my_err_head    **search_meh_pp;
 
1723
  const char            **errmsgs;
 
1724
 
 
1725
  /* Search for the registration in the list. */
 
1726
  for (search_meh_pp= &my_errmsgs_list;
 
1727
       *search_meh_pp;
 
1728
       search_meh_pp= &(*search_meh_pp)->meh_next)
 
1729
  {
 
1730
    if (((*search_meh_pp)->meh_first == first) &&
 
1731
        ((*search_meh_pp)->meh_last == last))
 
1732
      break;
 
1733
  }
 
1734
  if (! *search_meh_pp)
 
1735
    return NULL;
 
1736
 
 
1737
  /* Remove header from the chain. */
 
1738
  meh_p= *search_meh_pp;
 
1739
  *search_meh_pp= meh_p->meh_next;
 
1740
 
 
1741
  /* Save the return value and free the header. */
 
1742
  errmsgs= meh_p->meh_errmsgs;
 
1743
  bool is_globerrs= meh_p->is_globerrs;
 
1744
 
 
1745
  free((unsigned char*) meh_p);
 
1746
 
 
1747
  if (is_globerrs)
 
1748
    return NULL;
 
1749
 
 
1750
  return errmsgs;
 
1751
}
 
1752
 
 
1753
 
 
1754
void my_error_unregister_all(void)
 
1755
{
 
1756
  struct my_err_head    *list, *next;
 
1757
  for (list= my_errmsgs_globerrs.meh_next; list; list= next)
 
1758
  {
 
1759
    next= list->meh_next;
 
1760
    free((unsigned char*) list);
 
1761
  }
 
1762
  my_errmsgs_list= &my_errmsgs_globerrs;
 
1763
}