error.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include <libgearman/common.h>
  39. #include <cassert>
  40. #include <cerrno>
  41. #include <cstdarg>
  42. #include <cstdio>
  43. #include <cstring>
  44. gearman_return_t gearman_universal_set_error(gearman_universal_st& universal,
  45. gearman_return_t rc,
  46. const char *function,
  47. const char *position,
  48. const char *format, ...)
  49. {
  50. va_list args;
  51. universal.error.rc= rc;
  52. if (rc != GEARMAN_ERRNO)
  53. {
  54. universal.error.last_errno= 0;
  55. }
  56. char last_error[GEARMAN_MAX_ERROR_SIZE];
  57. va_start(args, format);
  58. int length= vsnprintf(last_error, GEARMAN_MAX_ERROR_SIZE, format, args);
  59. va_end(args);
  60. if (length > int(GEARMAN_MAX_ERROR_SIZE) or length < 0)
  61. {
  62. assert(length > int(GEARMAN_MAX_ERROR_SIZE));
  63. assert(length < 0);
  64. universal.error.last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  65. return GEARMAN_ARGUMENT_TOO_LARGE;
  66. }
  67. length= snprintf(universal.error.last_error, GEARMAN_MAX_ERROR_SIZE, "%s(%s) %s -> %s", function, gearman_strerror(rc), last_error, position);
  68. if (length > int(GEARMAN_MAX_ERROR_SIZE) or length < 0)
  69. {
  70. assert(length > int(GEARMAN_MAX_ERROR_SIZE));
  71. assert(length < 0);
  72. universal.error.last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  73. return GEARMAN_ARGUMENT_TOO_LARGE;
  74. }
  75. if (universal.log_fn)
  76. {
  77. universal.log_fn(universal.error.last_error, GEARMAN_VERBOSE_FATAL,
  78. static_cast<void *>(universal.log_context));
  79. }
  80. return rc;
  81. }
  82. gearman_return_t gearman_universal_set_gerror(gearman_universal_st& universal,
  83. gearman_return_t rc,
  84. const char *func,
  85. const char *position)
  86. {
  87. universal.error.rc= rc;
  88. if (rc != GEARMAN_ERRNO)
  89. {
  90. universal.error.last_errno= 0;
  91. }
  92. int length= snprintf(universal.error.last_error, GEARMAN_MAX_ERROR_SIZE, "%s(%s) -> %s", func, gearman_strerror(rc), position);
  93. if (length > int(GEARMAN_MAX_ERROR_SIZE) or length < 0)
  94. {
  95. assert(length > int(GEARMAN_MAX_ERROR_SIZE));
  96. assert(length < 0);
  97. universal.error.last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  98. return GEARMAN_ARGUMENT_TOO_LARGE;
  99. }
  100. if (universal.log_fn)
  101. {
  102. universal.log_fn(universal.error.last_error, GEARMAN_VERBOSE_FATAL,
  103. static_cast<void *>(universal.log_context));
  104. }
  105. return rc;
  106. }
  107. gearman_return_t gearman_universal_set_perror(gearman_universal_st &universal,
  108. const char *function, const char *position,
  109. const char *message)
  110. {
  111. universal.error.rc= GEARMAN_ERRNO;
  112. universal.error.last_errno= errno;
  113. const char *errmsg_ptr;
  114. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  115. errmsg[0]= 0;
  116. #ifdef STRERROR_R_CHAR_P
  117. errmsg_ptr= strerror_r(universal.error.last_errno, errmsg, sizeof(errmsg));
  118. #else
  119. strerror_r(universal.error.last_errno, errmsg, sizeof(errmsg));
  120. errmsg_ptr= errmsg;
  121. #endif
  122. int length;
  123. if (message)
  124. {
  125. length= snprintf(universal.error.last_error, GEARMAN_MAX_ERROR_SIZE, "%s(%s) %s -> %s", function, errmsg_ptr, message, position);
  126. }
  127. else
  128. {
  129. length= snprintf(universal.error.last_error, GEARMAN_MAX_ERROR_SIZE, "%s(%s) -> %s", function, errmsg_ptr, position);
  130. }
  131. if (length > int(GEARMAN_MAX_ERROR_SIZE) or length < 0)
  132. {
  133. assert(length > int(GEARMAN_MAX_ERROR_SIZE));
  134. assert(length < 0);
  135. universal.error.last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  136. return GEARMAN_ARGUMENT_TOO_LARGE;
  137. }
  138. if (universal.log_fn)
  139. {
  140. universal.log_fn(universal.error.last_error, GEARMAN_VERBOSE_FATAL,
  141. static_cast<void *>(universal.log_context));
  142. }
  143. return GEARMAN_ERRNO;
  144. }