log.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Gearman server and library
  2. * Copyright (C) 2008-2009 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. /**
  9. * @file
  10. * @brief Gearman State Definitions
  11. */
  12. #include <libgearman-server/common.h>
  13. #include <algorithm>
  14. #include <errno.h>
  15. #include <cstring>
  16. static pthread_key_t logging_key;
  17. static pthread_once_t intitialize_log_once = PTHREAD_ONCE_INIT;
  18. static void delete_log(void *ptr)
  19. {
  20. if (ptr)
  21. {
  22. free(ptr);
  23. }
  24. }
  25. static void create_log(void)
  26. {
  27. (void) pthread_key_create(&logging_key, delete_log);
  28. }
  29. void gearmand_initialize_thread_logging(const char *identity)
  30. {
  31. (void) pthread_once(&intitialize_log_once, create_log);
  32. void *ptr;
  33. if ((ptr = pthread_getspecific(logging_key)) == NULL)
  34. {
  35. const char *key_to_use= strdup(identity);
  36. (void) pthread_setspecific(logging_key, key_to_use);
  37. }
  38. }
  39. #ifndef __INTEL_COMPILER
  40. #pragma GCC diagnostic ignored "-Wold-style-cast"
  41. #endif
  42. /**
  43. * Log a message.
  44. *
  45. * @param[in] gearman Structure previously initialized with gearman_create() or
  46. * gearman_clone().
  47. * @param[in] verbose Logging level of the message.
  48. * @param[in] format Format and variable argument list of message.
  49. * @param[in] args Variable argument list that has been initialized.
  50. */
  51. static void gearmand_log(const char *position, const char * /* func */, gearmand_verbose_t verbose, const char *format, va_list args)
  52. {
  53. if (Gearmand() == NULL)
  54. {
  55. fprintf(stderr, "%s %7s: ", position, gearmand_verbose_name(verbose));
  56. vprintf(format, args);
  57. fprintf(stderr, "\n");
  58. return;
  59. }
  60. (void) pthread_once(&intitialize_log_once, create_log);
  61. const char *identity= (const char *)pthread_getspecific(logging_key);
  62. if (identity == NULL)
  63. {
  64. identity= "[ main ]";
  65. }
  66. if (Gearmand() && Gearmand()->log_fn)
  67. {
  68. char log_buffer[GEARMAN_MAX_ERROR_SIZE*2];
  69. int length= snprintf(log_buffer, sizeof(log_buffer), "%s ", identity);
  70. // We just return whatever we have if this occurs
  71. if (length < -1 || (size_t)length >= sizeof(log_buffer))
  72. {
  73. Gearmand()->log_fn(log_buffer, verbose, (void *)Gearmand()->log_context);
  74. return;
  75. }
  76. size_t remaining_size= sizeof(log_buffer) - (size_t)length;
  77. char *ptr= log_buffer;
  78. ptr+= length;
  79. int format_length= vsnprintf(ptr, remaining_size, format, args);
  80. remaining_size-= format_length;
  81. ptr+= format_length;
  82. if (position and verbose != GEARMAND_VERBOSE_INFO)
  83. snprintf(ptr, remaining_size, " -> %s", position);
  84. Gearmand()->log_fn(log_buffer, verbose, (void *)Gearmand()->log_context);
  85. }
  86. else
  87. {
  88. fprintf(stderr, "%s %7s: ", identity, gearmand_verbose_name(verbose));
  89. vprintf(format, args);
  90. fprintf(stderr, "\n");
  91. }
  92. }
  93. void gearmand_log_fatal(const char *position, const char *func, const char *format, ...)
  94. {
  95. va_list args;
  96. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
  97. {
  98. va_start(args, format);
  99. gearmand_log(position, func, GEARMAND_VERBOSE_FATAL, format, args);
  100. va_end(args);
  101. }
  102. }
  103. void gearmand_log_fatal_perror(const char *position, const char *function, const char *message)
  104. {
  105. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
  106. {
  107. const char *errmsg_ptr;
  108. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  109. errmsg[0]= 0;
  110. #ifdef STRERROR_R_CHAR_P
  111. errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
  112. #else
  113. strerror_r(errno, errmsg, sizeof(errmsg));
  114. errmsg_ptr= errmsg;
  115. #endif
  116. gearmand_log_fatal(position, function, "%s(%s)", message, errmsg_ptr);
  117. }
  118. }
  119. gearmand_error_t gearmand_log_error(const char *position, const char *function, const char *format, ...)
  120. {
  121. va_list args;
  122. if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
  123. {
  124. va_start(args, format);
  125. gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, format, args);
  126. va_end(args);
  127. }
  128. return GEARMAN_UNKNOWN_OPTION;
  129. }
  130. void gearmand_log_warning(const char *position, const char *function, const char *format, ...)
  131. {
  132. va_list args;
  133. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
  134. {
  135. va_start(args, format);
  136. gearmand_log(position, function, GEARMAND_VERBOSE_WARN, format, args);
  137. va_end(args);
  138. }
  139. }
  140. void gearmand_log_info(const char *position, const char *function, const char *format, ...)
  141. {
  142. va_list args;
  143. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_INFO)
  144. {
  145. va_start(args, format);
  146. gearmand_log(position, function, GEARMAND_VERBOSE_INFO, format, args);
  147. va_end(args);
  148. }
  149. }
  150. void gearmand_log_debug(const char *position, const char *function, const char *format, ...)
  151. {
  152. va_list args;
  153. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_DEBUG)
  154. {
  155. va_start(args, format);
  156. gearmand_log(position, function, GEARMAND_VERBOSE_DEBUG, format, args);
  157. va_end(args);
  158. }
  159. }
  160. void gearmand_log_crazy(const char *position, const char *function, const char *format, ...)
  161. {
  162. #ifdef DEBUG
  163. va_list args;
  164. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_CRAZY)
  165. {
  166. va_start(args, format);
  167. gearmand_log(position, function, GEARMAND_VERBOSE_CRAZY, format, args);
  168. va_end(args);
  169. }
  170. #else
  171. (void)position;
  172. (void)function;
  173. (void)format;
  174. #endif
  175. }
  176. gearmand_error_t gearmand_log_perror(const char *position, const char *function, const char *message)
  177. {
  178. if (not Gearmand() or (Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR))
  179. {
  180. const char *errmsg_ptr;
  181. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  182. errmsg[0]= 0;
  183. #ifdef STRERROR_R_CHAR_P
  184. errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
  185. #else
  186. strerror_r(errno, errmsg, sizeof(errmsg));
  187. errmsg_ptr= errmsg;
  188. #endif
  189. gearmand_log_error(position, function, "%s(%s)", message, errmsg_ptr);
  190. }
  191. return GEARMAN_ERRNO;
  192. }
  193. gearmand_error_t gearmand_log_gerror(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
  194. {
  195. if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
  196. {
  197. va_list args;
  198. if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
  199. {
  200. va_start(args, format);
  201. gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, format, args);
  202. va_end(args);
  203. }
  204. }
  205. else if (rc == GEARMAN_IO_WAIT)
  206. { }
  207. return rc;
  208. }
  209. gearmand_error_t gearmand_log_gai_error(const char *position, const char *function, const int rc, const char *message)
  210. {
  211. if (rc == EAI_SYSTEM)
  212. {
  213. return gearmand_log_perror(position, function, message);
  214. }
  215. gearmand_log_error(position, function, "%s getaddrinfo(%s)", message, gai_strerror(rc));
  216. return GEARMAN_GETADDRINFO;
  217. }
  218. gearmand_error_t gearmand_log_memory_error(const char *position, const char *function, const char *allocator, const char *object_type, size_t count, size_t size)
  219. {
  220. if (count > 1)
  221. {
  222. gearmand_log_error(position, function, "%s(%s, count: %lu size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
  223. }
  224. else
  225. {
  226. gearmand_log_error(position, function, "%s(%s, size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
  227. }
  228. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  229. }