log.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 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 <cstddef>
  48. #include <cstdlib>
  49. #include <cstring>
  50. #include <ctime>
  51. #include <pthread.h>
  52. #ifdef _WIN32
  53. # include <malloc.h>
  54. #else
  55. # include <alloca.h>
  56. #endif
  57. #ifndef __INTEL_COMPILER
  58. # pragma GCC diagnostic ignored "-Wold-style-cast"
  59. # pragma GCC diagnostic ignored "-Wformat-nonliteral"
  60. # pragma GCC diagnostic ignored "-Wformat-security"
  61. #endif
  62. static pthread_key_t logging_key;
  63. static pthread_once_t intitialize_log_once= PTHREAD_ONCE_INIT;
  64. static void delete_log(void * ptr)
  65. {
  66. if (ptr)
  67. {
  68. free(ptr);
  69. }
  70. }
  71. static void create_log(void)
  72. {
  73. int pthread_error;
  74. if ((pthread_error= pthread_key_create(&logging_key, delete_log)))
  75. {
  76. gearmand_log_fatal_perror(GEARMAND_AT, "pthread_key_create", pthread_error, "pthread_key_create");
  77. abort();
  78. }
  79. }
  80. gearmand_error_t gearmand_initialize_thread_logging(const char *identity)
  81. {
  82. if (identity)
  83. {
  84. int pthread_error;
  85. if ((pthread_error= pthread_once(&intitialize_log_once, create_log)))
  86. {
  87. return gearmand_log_fatal_perror(GEARMAND_AT, "pthread_once", pthread_error, "identity: %s", identity);
  88. }
  89. if (pthread_getspecific(logging_key) == NULL)
  90. {
  91. const char *key_to_use= strdup(identity);
  92. if ((pthread_error= pthread_setspecific(logging_key, key_to_use)))
  93. {
  94. return gearmand_log_fatal_perror(GEARMAND_AT, "pthread_setspecific", pthread_error, "identity: %s", identity);
  95. }
  96. }
  97. return GEARMAN_SUCCESS;
  98. }
  99. gearmand_fatal("identity was NULL");
  100. return GEARMAN_INVALID_ARGUMENT;
  101. }
  102. /**
  103. * Log a message.
  104. *
  105. * @param[in] gearman Structure previously initialized with gearman_create() or
  106. * gearman_clone().
  107. * @param[in] verbose Logging level of the message.
  108. * @param[in] format Format and variable argument list of message.
  109. * @param[in] args Variable argument list that has been initialized.
  110. */
  111. static void gearmand_log(const char *position, const char *func /* func */,
  112. gearmand_verbose_t verbose,
  113. const gearmand_error_t error_arg,
  114. const char *format, va_list args)
  115. {
  116. struct timeval current_epoch;
  117. if (Gearmand() and Gearmand()->verbose < GEARMAND_VERBOSE_DEBUG)
  118. {
  119. current_epoch= libgearman::server::Epoch::current();
  120. current_epoch.tv_usec= 0;
  121. }
  122. else
  123. {
  124. (void)gettimeofday(&current_epoch, NULL);
  125. }
  126. struct tm current_tm;
  127. if (current_epoch.tv_sec == 0)
  128. {
  129. (void)gettimeofday(&current_epoch, NULL);
  130. }
  131. if ((gmtime_r(&current_epoch.tv_sec, &current_tm) == NULL))
  132. {
  133. memset(&current_epoch, 0, sizeof(current_epoch));
  134. }
  135. (void) pthread_once(&intitialize_log_once, create_log);
  136. const char *identity= (const char *)pthread_getspecific(logging_key);
  137. if (identity == NULL)
  138. {
  139. identity= "[ main ]";
  140. }
  141. char log_buffer[GEARMAN_MAX_ERROR_SIZE*2] = { 0 };
  142. if (Gearmand() && Gearmand()->log_fn)
  143. {
  144. char *log_buffer_ptr= log_buffer;
  145. size_t remaining_size= sizeof(log_buffer);
  146. {
  147. int length= snprintf(log_buffer, sizeof(log_buffer), "%04d-%02d-%02d %02d:%02d:%02d.%06d %s ",
  148. int(1900 +current_tm.tm_year), current_tm.tm_mon +1, current_tm.tm_mday, current_tm.tm_hour,
  149. current_tm.tm_min, current_tm.tm_sec, int(current_epoch.tv_usec),
  150. identity);
  151. // We just return whatever we have if this occurs
  152. if (length <= 0 or (size_t)length >= sizeof(log_buffer))
  153. {
  154. remaining_size= 0;
  155. }
  156. else
  157. {
  158. remaining_size-= size_t(length);
  159. log_buffer_ptr+= length;
  160. }
  161. }
  162. if (remaining_size)
  163. {
  164. int length= vsnprintf(log_buffer_ptr, remaining_size, format, args);
  165. if (length <= 0 or size_t(length) >= remaining_size)
  166. {
  167. remaining_size= 0;
  168. }
  169. else
  170. {
  171. remaining_size-= size_t(length);
  172. log_buffer_ptr+= length;
  173. }
  174. }
  175. if (remaining_size and error_arg != GEARMAN_SUCCESS)
  176. {
  177. int length= snprintf(log_buffer_ptr, remaining_size, " %s(%s)", func, gearmand_strerror(error_arg));
  178. if (length <= 0 or size_t(length) >= remaining_size)
  179. {
  180. remaining_size= 0;
  181. }
  182. else
  183. {
  184. remaining_size-= size_t(length);
  185. log_buffer_ptr+= length;
  186. }
  187. }
  188. if (remaining_size and position and verbose != GEARMAND_VERBOSE_INFO)
  189. {
  190. int length= snprintf(log_buffer_ptr, remaining_size, " -> %s", position);
  191. if (length <= 0 or size_t(length) >= remaining_size)
  192. {
  193. remaining_size= 0;
  194. }
  195. }
  196. // Make sure this is null terminated
  197. log_buffer[sizeof(log_buffer) -1]= 0;
  198. }
  199. if (Gearmand() and Gearmand()->log_fn)
  200. {
  201. Gearmand()->log_fn(log_buffer, verbose, (void *)Gearmand()->log_context);
  202. }
  203. else
  204. {
  205. fprintf(stderr, "%s -> %s",
  206. log_buffer, gearmand_verbose_name(verbose));
  207. vfprintf(stderr, format, args);
  208. fprintf(stderr, "\n");
  209. }
  210. }
  211. gearmand_error_t gearmand_log_fatal(const char *position, const char *func, const char *format, ...)
  212. {
  213. if (Gearmand() and Gearmand()->verbose < GEARMAND_VERBOSE_FATAL)
  214. {
  215. return GEARMAN_ERRNO;
  216. }
  217. {
  218. va_list args;
  219. va_start(args, format);
  220. gearmand_log(position, func, GEARMAND_VERBOSE_FATAL, GEARMAN_SUCCESS, format, args);
  221. va_end(args);
  222. }
  223. return GEARMAN_ERRNO;
  224. }
  225. gearmand_error_t gearmand_log_fatal_perror(const char *position, const char *function, const int local_errno, const char *format, ...)
  226. {
  227. if (Gearmand() and Gearmand()->verbose < GEARMAND_VERBOSE_FATAL)
  228. {
  229. return GEARMAN_ERRNO;
  230. }
  231. char* message_buffer= NULL;
  232. {
  233. va_list args;
  234. va_start(args, format);
  235. size_t ask= snprintf(0, 0, format);
  236. ask++; // for null
  237. message_buffer= (char*)alloca(sizeof(char) * ask);
  238. if (message_buffer)
  239. {
  240. vsnprintf(message_buffer, ask, format, args);
  241. }
  242. va_end(args);
  243. }
  244. {
  245. const char *errmsg_ptr;
  246. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  247. errmsg[0]= 0;
  248. #ifdef STRERROR_R_CHAR_P
  249. errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
  250. #else
  251. strerror_r(local_errno, errmsg, sizeof(errmsg));
  252. errmsg_ptr= errmsg;
  253. #endif
  254. if (message_buffer)
  255. {
  256. gearmand_log_fatal(position, function, "%s(%s)", message_buffer, errmsg_ptr);
  257. }
  258. else
  259. {
  260. gearmand_log_fatal(position, function, "%s", errmsg_ptr);
  261. }
  262. }
  263. switch (local_errno)
  264. {
  265. case ENOMEM:
  266. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  267. case ECONNRESET:
  268. case EHOSTDOWN:
  269. return GEARMAN_LOST_CONNECTION;
  270. default:
  271. break;
  272. }
  273. return GEARMAN_ERRNO;
  274. }
  275. gearmand_error_t gearmand_log_error(const char *position, const char *function, const char *format, ...)
  276. {
  277. if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
  278. {
  279. va_list args;
  280. va_start(args, format);
  281. gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, GEARMAN_SUCCESS, format, args);
  282. va_end(args);
  283. }
  284. return GEARMAN_UNKNOWN_OPTION;
  285. }
  286. void gearmand_log_warning(const char *position, const char *function, const char *format, ...)
  287. {
  288. if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
  289. {
  290. va_list args;
  291. va_start(args, format);
  292. gearmand_log(position, function, GEARMAND_VERBOSE_WARN, GEARMAN_SUCCESS, format, args);
  293. va_end(args);
  294. }
  295. }
  296. // LOG_NOTICE is only used for reporting job status.
  297. void gearmand_log_notice(const char *position, const char *function, const char *format, ...)
  298. {
  299. if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_NOTICE)
  300. {
  301. va_list args;
  302. va_start(args, format);
  303. gearmand_log(position, function, GEARMAND_VERBOSE_NOTICE, GEARMAN_SUCCESS, format, args);
  304. va_end(args);
  305. }
  306. }
  307. void gearmand_log_info(const char *position, const char *function, const char *format, ...)
  308. {
  309. if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_INFO)
  310. {
  311. va_list args;
  312. va_start(args, format);
  313. gearmand_log(position, function, GEARMAND_VERBOSE_INFO, GEARMAN_SUCCESS, format, args);
  314. va_end(args);
  315. }
  316. }
  317. void gearmand_log_debug(const char *position, const char *function, const char *format, ...)
  318. {
  319. va_list args;
  320. if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_DEBUG)
  321. {
  322. va_start(args, format);
  323. gearmand_log(position, function, GEARMAND_VERBOSE_DEBUG, GEARMAN_SUCCESS, format, args);
  324. va_end(args);
  325. }
  326. }
  327. gearmand_error_t gearmand_log_perror(const char *position, const char *function, const int local_errno, const char *format, ...)
  328. {
  329. if (not Gearmand() or (Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR))
  330. {
  331. char* message_buffer= NULL;
  332. {
  333. va_list args;
  334. va_start(args, format);
  335. size_t ask= snprintf(0, 0, format);
  336. ask++; // for null
  337. message_buffer= (char*)alloca(sizeof(char) * ask);
  338. vsnprintf(message_buffer, ask, format, args);
  339. va_end(args);
  340. }
  341. const char *errmsg_ptr;
  342. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  343. errmsg[0]= 0;
  344. #ifdef STRERROR_R_CHAR_P
  345. errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
  346. #else
  347. strerror_r(local_errno, errmsg, sizeof(errmsg));
  348. errmsg_ptr= errmsg;
  349. #endif
  350. if (message_buffer)
  351. {
  352. gearmand_log_error(position, function, "%s(%s)", message_buffer, errmsg_ptr);
  353. }
  354. else
  355. {
  356. gearmand_log_error(position, function, "%s", errmsg_ptr);
  357. }
  358. }
  359. switch (local_errno)
  360. {
  361. case ENOMEM:
  362. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  363. case ECONNRESET:
  364. case EHOSTDOWN:
  365. return GEARMAN_LOST_CONNECTION;
  366. default:
  367. break;
  368. }
  369. return GEARMAN_ERRNO;
  370. }
  371. gearmand_error_t gearmand_log_gerror(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
  372. {
  373. if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
  374. {
  375. va_list args;
  376. if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
  377. {
  378. va_start(args, format);
  379. gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, rc, format, args);
  380. va_end(args);
  381. }
  382. }
  383. else if (rc == GEARMAN_IO_WAIT)
  384. { }
  385. return rc;
  386. }
  387. gearmand_error_t gearmand_log_gerror_warn(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
  388. {
  389. if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
  390. {
  391. va_list args;
  392. if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
  393. {
  394. va_start(args, format);
  395. gearmand_log(position, function, GEARMAND_VERBOSE_WARN, rc, format, args);
  396. va_end(args);
  397. }
  398. }
  399. else if (rc == GEARMAN_IO_WAIT)
  400. { }
  401. return rc;
  402. }
  403. gearmand_error_t gearmand_log_gai_error(const char *position, const char *function, const int rc, const char *message)
  404. {
  405. if (rc == EAI_SYSTEM)
  406. {
  407. return gearmand_log_perror(position, function, errno, message);
  408. }
  409. gearmand_log_error(position, function, "%s getaddrinfo(%s)", message, gai_strerror(rc));
  410. return GEARMAN_GETADDRINFO;
  411. }
  412. 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)
  413. {
  414. if (count > 1)
  415. {
  416. gearmand_log_error(position, function, "%s(%s, count: %lu size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
  417. }
  418. else
  419. {
  420. gearmand_log_error(position, function, "%s(%s, size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
  421. }
  422. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  423. }