123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- /* Gearman server and library
- * Copyright (C) 2008-2009 Brian Aker, Eric Day
- * All rights reserved.
- *
- * Use and distribution licensed under the BSD license. See
- * the COPYING file in the parent directory for full text.
- */
- /**
- * @file
- * @brief Gearman State Definitions
- */
- #include <config.h>
- #include <libgearman-server/common.h>
- #include <algorithm>
- #include <cerrno>
- #include <cstring>
- static pthread_key_t logging_key;
- static pthread_once_t intitialize_log_once= PTHREAD_ONCE_INIT;
- static void delete_log(void *ptr)
- {
- if (ptr)
- {
- free(ptr);
- }
- }
- static void create_log(void)
- {
- (void) pthread_key_create(&logging_key, delete_log);
- }
- void gearmand_initialize_thread_logging(const char *identity)
- {
- (void) pthread_once(&intitialize_log_once, create_log);
- void *ptr;
- if ((ptr= pthread_getspecific(logging_key)) == NULL)
- {
- const char *key_to_use= strdup(identity);
- (void) pthread_setspecific(logging_key, key_to_use);
- }
- }
- #ifndef __INTEL_COMPILER
- #pragma GCC diagnostic ignored "-Wold-style-cast"
- #endif
- /**
- * Log a message.
- *
- * @param[in] gearman Structure previously initialized with gearman_create() or
- * gearman_clone().
- * @param[in] verbose Logging level of the message.
- * @param[in] format Format and variable argument list of message.
- * @param[in] args Variable argument list that has been initialized.
- */
- static void gearmand_log(const char *position, const char *func /* func */,
- gearmand_verbose_t verbose,
- const gearmand_error_t error_arg,
- const char *format, va_list args)
- {
- if (Gearmand() == NULL)
- {
- fprintf(stderr, "%s %7s: ", position, gearmand_verbose_name(verbose));
- vprintf(format, args);
- fprintf(stderr, "\n");
- return;
- }
- (void) pthread_once(&intitialize_log_once, create_log);
- const char *identity= (const char *)pthread_getspecific(logging_key);
- if (identity == NULL)
- {
- identity= "[ main ]";
- }
- if (Gearmand() && Gearmand()->log_fn)
- {
- char log_buffer[GEARMAN_MAX_ERROR_SIZE*2];
- char *log_buffer_ptr= log_buffer;
- size_t remaining_size= sizeof(log_buffer);
- {
- int length= snprintf(log_buffer_ptr, sizeof(log_buffer), "%s ", identity);
- // We just return whatever we have if this occurs
- if (length < -1 || (size_t)length >= sizeof(log_buffer))
- {
- remaining_size= 0;
- }
- else
- {
- remaining_size-= size_t(length);
- log_buffer_ptr+= length;
- }
- }
- if (remaining_size)
- {
- int length= vsnprintf(log_buffer_ptr, remaining_size, format, args);
- if (length < -1 or size_t(length) >= remaining_size)
- {
- remaining_size= 0;
- }
- else
- {
- remaining_size-= size_t(length);
- log_buffer_ptr+= length;
- }
- }
- if (remaining_size and error_arg != GEARMAN_SUCCESS)
- {
- int length= snprintf(log_buffer_ptr, remaining_size, " %s(%s)", func, gearmand_strerror(error_arg));
- if (length < -1 or size_t(length) >= remaining_size)
- { }
- else
- {
- remaining_size-= size_t(length);
- log_buffer_ptr+= length;
- }
- }
- if (remaining_size and position and verbose != GEARMAND_VERBOSE_INFO)
- {
- snprintf(log_buffer_ptr, remaining_size, " -> %s", position);
- }
- // Make sure this is null terminated
- log_buffer[sizeof(log_buffer)]= 0;
- Gearmand()->log_fn(log_buffer, verbose, (void *)Gearmand()->log_context);
- }
- else
- {
- fprintf(stderr, "%s %7s: ", identity, gearmand_verbose_name(verbose));
- vprintf(format, args);
- fprintf(stderr, "\n");
- }
- }
- void gearmand_log_fatal(const char *position, const char *func, const char *format, ...)
- {
- va_list args;
- if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
- {
- va_start(args, format);
- gearmand_log(position, func, GEARMAND_VERBOSE_FATAL, GEARMAN_SUCCESS, format, args);
- va_end(args);
- }
- }
- void gearmand_log_fatal_perror(const char *position, const char *function, const char *message)
- {
- if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
- {
- const char *errmsg_ptr;
- char errmsg[GEARMAN_MAX_ERROR_SIZE];
- errmsg[0]= 0;
- #ifdef STRERROR_R_CHAR_P
- errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
- #else
- strerror_r(errno, errmsg, sizeof(errmsg));
- errmsg_ptr= errmsg;
- #endif
- gearmand_log_fatal(position, function, "%s(%s)", message, errmsg_ptr);
- }
- }
- gearmand_error_t gearmand_log_error(const char *position, const char *function, const char *format, ...)
- {
- va_list args;
- if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, GEARMAN_SUCCESS, format, args);
- va_end(args);
- }
- return GEARMAN_UNKNOWN_OPTION;
- }
- void gearmand_log_warning(const char *position, const char *function, const char *format, ...)
- {
- va_list args;
- if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_WARN, GEARMAN_SUCCESS, format, args);
- va_end(args);
- }
- }
- // LOG_NOTICE is only used for reporting job status.
- void gearmand_log_notice(const char *position, const char *function, const char *format, ...)
- {
- va_list args;
- if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_NOTICE)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_NOTICE, GEARMAN_SUCCESS, format, args);
- va_end(args);
- }
- }
- void gearmand_log_info(const char *position, const char *function, const char *format, ...)
- {
- va_list args;
- if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_INFO)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_INFO, GEARMAN_SUCCESS, format, args);
- va_end(args);
- }
- }
- void gearmand_log_debug(const char *position, const char *function, const char *format, ...)
- {
- va_list args;
- if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_DEBUG)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_DEBUG, GEARMAN_SUCCESS, format, args);
- va_end(args);
- }
- }
- gearmand_error_t gearmand_log_perror(const char *position, const char *function, const char *message)
- {
- if (not Gearmand() or (Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR))
- {
- const char *errmsg_ptr;
- char errmsg[GEARMAN_MAX_ERROR_SIZE];
- errmsg[0]= 0;
- #ifdef STRERROR_R_CHAR_P
- errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
- #else
- strerror_r(errno, errmsg, sizeof(errmsg));
- errmsg_ptr= errmsg;
- #endif
- gearmand_log_error(position, function, "%s(%s)", message, errmsg_ptr);
- }
- return GEARMAN_ERRNO;
- }
- gearmand_error_t gearmand_log_gerror(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
- {
- if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
- {
- va_list args;
- if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, rc, format, args);
- va_end(args);
- }
- }
- else if (rc == GEARMAN_IO_WAIT)
- { }
- return rc;
- }
- gearmand_error_t gearmand_log_gerror_warn(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...)
- {
- if (gearmand_failed(rc) and rc != GEARMAN_IO_WAIT)
- {
- va_list args;
- if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_WARN)
- {
- va_start(args, format);
- gearmand_log(position, function, GEARMAND_VERBOSE_WARN, rc, format, args);
- va_end(args);
- }
- }
- else if (rc == GEARMAN_IO_WAIT)
- { }
- return rc;
- }
- gearmand_error_t gearmand_log_gai_error(const char *position, const char *function, const int rc, const char *message)
- {
- if (rc == EAI_SYSTEM)
- {
- return gearmand_log_perror(position, function, message);
- }
- gearmand_log_error(position, function, "%s getaddrinfo(%s)", message, gai_strerror(rc));
- return GEARMAN_GETADDRINFO;
- }
- 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)
- {
- if (count > 1)
- {
- gearmand_log_error(position, function, "%s(%s, count: %lu size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
- }
- else
- {
- gearmand_log_error(position, function, "%s(%s, size: %lu)", allocator, object_type, static_cast<unsigned long>(count), static_cast<unsigned long>(size));
- }
- return GEARMAN_MEMORY_ALLOCATION_FAILURE;
- }
|