Fu Juntang
2021-10-08 5add39f46c8323875fb56bc764a8ff627ad82f18
src/bus_error.cpp
@@ -11,12 +11,21 @@
static pthread_key_t strerrorKey;
static char *_bus_errlist[_bus_nerr] = {
static const char *_bus_errlist[_bus_nerr] = {
  "\0",
  "Timeout",
  "Timed out",
  "The other end is not inline",
  "Key already in use",
  "Network fault"
  "Network fault",
  "Send to self error",
  "Receive from wrong end",
  "Service stoped",
  "Exceed resource limit",
  "Service not supported",
  "Resource busy",
  "Resource not provide",
  "Invalid parameters",
  "No enough memory"
};
@@ -40,11 +49,10 @@
}
char *
bus_strerror(int err)
bus_strerror(int err, int flag)
{
  int s, eindex;
  int s;
  char *buf;
  eindex = err - EBUS_BASE;
  /* Make first caller allocate key for thread-specific data */
  s = pthread_once(&once, createKey);
@@ -56,24 +64,79 @@
  {
    /* If first call from this thread, allocate
                                   buffer for thread, and save its location */
    buf = (char *)malloc(MAX_ERROR_LEN);
    buf = (char *)malloc(MAX_ERROR_LEN + sizeof(int));
    if (buf == NULL)
      err_exit(errno, "malloc");
    memset(buf, 0x00, MAX_ERROR_LEN + sizeof(int));
    s = pthread_setspecific(strerrorKey, buf);
    if (s != 0)
      err_exit(s, "pthread_setspecific");
  }
  if (flag != 0) {
    err = *(int *)(buf + MAX_ERROR_LEN);
  }
  if (err == 0) {
    err = EBUS_BASE;
  }
  if(err < EBUS_BASE) {
    // libc错误
    if (err < 0 || err >= _sys_nerr || _sys_errlist[err] == NULL)
    {
      snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err);
    }
    else
    {
      strncpy(buf, _sys_errlist[err], MAX_ERROR_LEN - 1);
      buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
    }
  } else {
    //自定义错误
    err -= EBUS_BASE;
    if (err < 0 || err >= _bus_nerr || _bus_errlist[err] == NULL)
    {
      snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err);
    }
    else
    {
      strncpy(buf, _bus_errlist[err], MAX_ERROR_LEN - 1);
      buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
    }
  }
  return buf;
}
void bus_errorset(int err)
{
  int s;
  char *buf;
  /* Make first caller allocate key for thread-specific data */
  s = pthread_once(&once, createKey);
  if (s != 0)
    err_exit(s, "pthread_once");
  buf = (char *)pthread_getspecific(strerrorKey);
  if (buf == NULL)
  {
    /* If first call from this thread, allocate
                                   buffer for thread, and save its location */
    buf = (char *)malloc(MAX_ERROR_LEN + sizeof(int));
    if (buf == NULL)
      err_exit(errno, "malloc");
    s = pthread_setspecific(strerrorKey, buf);
    if (s != 0)
      err_exit(s, "pthread_setspecific");
  }
  }
  *(int *)(buf + MAX_ERROR_LEN) = err;
  if (eindex < 0 || eindex >= _bus_nerr || _bus_errlist[eindex] == NULL)
  {
    snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", eindex);
  }
  else
  {
    strncpy(buf, _bus_errlist[eindex], MAX_ERROR_LEN - 1);
    buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
  }
  return buf;
}