log.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 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. /**
  39. * @file
  40. * @brief Gearman State Definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman-server/common.h"
  44. #include <libgearman-server/timer.h>
  45. #include <algorithm>
  46. #include <cerrno>
  47. #include <cstring>
  48. #include <ctime>
  49. static pthread_key_t logging_key;
  50. static pthread_once_t intitialize_log_once= PTHREAD_ONCE_INIT;
  51. static void delete_log(void *ptr)
  52. {
  53. if (ptr)
  54. {
  55. free(ptr);
  56. }
  57. }
  58. static void create_log(void)
  59. {
  60. (void) pthread_key_create(&logging_key, delete_log);
  61. }
  62. void gearmand_initialize_thread_logging(const char *identity)
  63. {
  64. (void) pthread_once(&intitialize_log_once, create_log);
  65. if (pthread_getspecific(logging_key) == NULL)
  66. {
  67. const char *key_to_use= strdup(identity);
  68. (void) pthread_setspecific(logging_key, key_to_use);
  69. }
  70. }
  71. #ifndef __INTEL_COMPILER
  72. #pragma GCC diagnostic ignored "-Wold-style-cast"
  73. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  74. #endif
  75. /**
  76. * Log a message.
  77. *
  78. * @param[in] gearman Structure previously initialized with gearman_create() or
  79. * gearman_clone().
  80. * @param[in] verbose Logging level of the message.
  81. * @param[in] format Format and variable argument list of message.
  82. * @param[in] args Variable argument list that has been initialized.
  83. */
  84. static void gearmand_log(const char *position, const char *func /* func */,
  85. gearmand_verbose_t verbose,
  86. const gearmand_error_t error_arg,
  87. const char *format, va_list args)
  88. {
  89. struct timeval current_epoch;
  90. if (Gearmand() and Gearmand()->verbose < GEARMAND_VERBOSE_DEBUG)
  91. {
  92. current_epoch= libgearman::server::Epoch::current();
  93. current_epoch.tv_usec= 0;
  94. }
  95. else
  96. {
  97. (void)gettimeofday(&current_epoch, NULL);
  98. }
  99. struct tm current_tm;
  100. if (current_epoch.tv_sec == 0)
  101. {
  102. (void)gettimeofday(&current_epoch, NULL);
  103. }
  104. if ((gmtime_r(&current_epoch.tv_sec, &current_tm) == NULL))
  105. {
  106. memset(&current_epoch, 0, sizeof(current_epoch));
  107. }
  108. (void) pthread_once(&intitialize_log_once, create_log);
  109. const char *identity= (const char *)pthread_getspecific(logging_key);
  110. if (identity == NULL)
  111. {
  112. identity= "[ main ]";
  113. }
  114. char log_buffer[GEARMAN_MAX_ERROR_SIZE*2] = { 0 };
  115. if (Gearmand() && Gearmand()->log_fn)
  116. {
  117. char *log_buffer_ptr= log_buffer;
  118. size_t remaining_size= sizeof(log_buffer);
  119. {
  120. int length= snprintf(log_buffer, sizeof(log_buffer), "%04d-%02d-%02d %02d:%02d:%02d.%06d %s ",
  121. int(1900 +current_tm.tm_year), current_tm.tm_mon +1, current_tm.tm_mday, current_tm.tm_hour,
  122. current_tm.tm_min, current_tm.tm_sec, int(current_epoch.tv_usec),
  123. identity);
  124. // We just return whatever we have if this occurs
  125. if (length <= 0 or (size_t)length >= sizeof(log_buffer))
  126. {
  127. remaining_size= 0;
  128. }
  129. else
  130. {
  131. remaining_size-= size_t(length);
  132. log_buffer_ptr+= length;
  133. }
  134. }
  135. if (remaining_size)
  136. {
  137. int length= vsnprintf(log_buffer_ptr, remaining_size, format, args);
  138. if (length <= 0 or size_t(length) >= remaining_size)
  139. {
  140. remaining_size= 0;
  141. }
  142. else
  143. {
  144. remaining_size-= size_t(length);
  145. log_buffer_ptr+= length;
  146. }
  147. }
  148. if (remaining_size and error_arg != GEARMAN_SUCCESS)
  149. {
  150. int length= snprintf(log_buffer_ptr, remaining_size, " %s(%s)", func, gearmand_strerror(error_arg));
  151. if (length <= 0 or size_t(length) >= remaining_size)
  152. {
  153. remaining_size= 0;
  154. }
  155. else
  156. {
  157. remaining_size-= size_t(length);
  158. log_buffer_ptr+= length;
  159. }
  160. }
  161. if (remaining_size and position and verbose != GEARMAND_VERBOSE_INFO)
  162. {
  163. int length= snprintf(log_buffer_ptr, remaining_size, " -> %s", position);
  164. if (length <= 0 or size_t(length) >= remaining_size)
  165. {
  166. remaining_size= 0;
  167. }
  168. }
  169. // Make sure this is null terminated
  170. log_buffer[sizeof(log_buffer) -1]= 0;
  171. }
  172. if (Gearmand() and Gearmand()->log_fn)
  173. {
  174. Gearmand()->log_fn(log_buffer, verbose, (void *)Gearmand()->log_context);
  175. }
  176. else
  177. {
  178. fprintf(stderr, "%s -> %s",
  179. log_buffer, gearmand_verbose_name(verbose));
  180. vfprintf(stderr, format, args);
  181. fprintf(stderr, "\n");
  182. }
  183. }
  184. void gearmand_log_fatal(const char *position, const char *func, const char *format, ...)
  185. {
  186. va_list args;
  187. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
  188. {
  189. va_start(args, format);
  190. gearmand_log(position, func, GEARMAND_VERBOSE_FATAL, GEARMAN_SUCCESS, format, args);
  191. va_end(args);
  192. }
  193. }
  194. void gearmand_log_fatal_perror(const char *position, const char *function, const char *message)
  195. {
  196. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
  197. {
  198. const char *errmsg_ptr;
  199. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  200. errmsg[0]= 0;
  201. #ifdef STRERROR_R_CHAR_P
  202. errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
  203. #else
  204. strerror_r(errno, errmsg, sizeof(errmsg));
  205. errmsg_ptr= errmsg;
  206. #endif
  207. gearmand_log_fatal(position, function, "%s(%s)", message, errmsg_ptr);
  208. }
  209. }
  210. gearmand_error_t gearmand_log_error(const char *position, const char *function, const char *format, ...)
  211. {
  212. va_list args;
  213. if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
  214. {
  215. va_start(args, format);
  216. gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, GEARMAN_SUCCESS, format, args);
  217. va_end(args);
  218. }
  219. return GEARMAN_UNKNOWN_OPTION;
  220. }
  221. void gearmand_log_warning(const char *position, const char *function, const char *format, ...)
  222. {
  223. va_list args;
  224. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
  225. {
  226. va_start(args, format);
  227. gearmand_log(position, function, GEARMAND_VERBOSE_WARN, GEARMAN_SUCCESS, format, args);
  228. va_end(args);
  229. }
  230. }
  231. // LOG_NOTICE is only used for reporting job status.
  232. void gearmand_log_notice(const char *position, const char *function, const char *format, ...)
  233. {
  234. va_list args;
  235. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_NOTICE)
  236. {
  237. va_start(args, format);
  238. gearmand_log(position, function, GEARMAND_VERBOSE_NOTICE, GEARMAN_SUCCESS, format, args);
  239. va_end(args);
  240. }
  241. }
  242. void gearmand_log_info(const char *position, const char *function, const char *format, ...)
  243. {
  244. va_list args;
  245. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_INFO)
  246. {
  247. va_start(args, format);
  248. gearmand_log(position, function, GEARMAND_VERBOSE_INFO, GEARMAN_SUCCESS, format, args);
  249. va_end(args);
  250. }
  251. }
  252. void gearmand_log_debug(const char *position, const char *function, const char *format, ...)
  253. {
  254. va_list args;
  255. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_DEBUG)
  256. {
  257. va_start(args, format);
  258. gearmand_log(position, function, GEARMAND_VERBOSE_DEBUG, GEARMAN_SUCCESS, format, args);
  259. va_end(args);
  260. }
  261. }
  262. gearmand_error_t gearmand_log_perror(const char *position, const char *function, const char *message)
  263. {
  264. if (not Gearmand() or (Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR))
  265. {
  266. const char *errmsg_ptr;
  267. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  268. errmsg[0]= 0;
  269. #ifdef STRERROR_R_CHAR_P
  270. errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
  271. #else
  272. strerror_r(errno, errmsg, sizeof(errmsg));
  273. errmsg_ptr= errmsg;
  274. #endif
  275. gearmand_log_error(position, function, "%s(%s)", message, errmsg_ptr);
  276. }
  277. return GEARMAN_ERRNO;
  278. }
  279. gearmand_error_t gearmand_log_gerror(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
  280. {
  281. if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
  282. {
  283. va_list args;
  284. if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
  285. {
  286. va_start(args, format);
  287. gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, rc, format, args);
  288. va_end(args);
  289. }
  290. }
  291. else if (rc == GEARMAN_IO_WAIT)
  292. { }
  293. return rc;
  294. }
  295. gearmand_error_t gearmand_log_gerror_warn(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
  296. {
  297. if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
  298. {
  299. va_list args;
  300. if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
  301. {
  302. va_start(args, format);
  303. gearmand_log(position, function, GEARMAND_VERBOSE_WARN, rc, format, args);
  304. va_end(args);
  305. }
  306. }
  307. else if (rc == GEARMAN_IO_WAIT)
  308. { }
  309. return rc;
  310. }
  311. gearmand_error_t gearmand_log_gai_error(const char *position, const char *function, const int rc, const char *message)
  312. {
  313. if (rc == EAI_SYSTEM)
  314. {
  315. return gearmand_log_perror(position, function, message);
  316. }
  317. gearmand_log_error(position, function, "%s getaddrinfo(%s)", message, gai_strerror(rc));
  318. return GEARMAN_GETADDRINFO;
  319. }
  320. 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)
  321. {
  322. if (count > 1)
  323. {
  324. gearmand_log_error(position, function, "%s(%s, count: %lu size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
  325. }
  326. else
  327. {
  328. gearmand_log_error(position, function, "%s(%s, size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
  329. }
  330. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  331. }