diff --git a/libknet/logging.c b/libknet/logging.c index 0e7f4234b..24cd125dd 100644 --- a/libknet/logging.c +++ b/libknet/logging.c @@ -175,24 +175,26 @@ void log_msg(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, int len, err; if ((!knet_h) || - (knet_h->logfd <= 0) || (subsystem > KNET_SUB_LAST) || (msglevel > knet_h->log_levels[subsystem])) return; - memset(&msg, 0, sizeof(struct knet_log_msg)); - msg.subsystem = subsystem; - msg.msglevel = msglevel; - /* * most logging calls will take place with locking in place. * if we get an EINVAL and locking is initialized, then * we are getting a real error and we need to stop */ - err = pthread_rwlock_rdlock(&knet_h->global_rwlock); - if ((err == EINVAL) && (knet_h->lock_init_done)) + err = pthread_rwlock_tryrdlock(&knet_h->global_rwlock); + if ((err == EAGAIN) && (knet_h->lock_init_done)) return; + if (knet_h->logfd <= 0) + goto out_unlock; + + memset(&msg, 0, sizeof(struct knet_log_msg)); + msg.subsystem = subsystem; + msg.msglevel = msglevel; + va_start(ap, fmt); vsnprintf(msg.msg, sizeof(msg.msg) - 2, fmt, ap); va_end(ap); @@ -200,12 +202,6 @@ void log_msg(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, len = strlen(msg.msg); msg.msg[len+1] = '\n'; - /* - * unlock only if we are holding the lock - */ - if (!err) - pthread_rwlock_unlock(&knet_h->global_rwlock); - while (byte_cnt < sizeof(struct knet_log_msg)) { len = write(knet_h->logfd, &msg, sizeof(struct knet_log_msg) - byte_cnt); if (len <= 0) @@ -214,5 +210,12 @@ void log_msg(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, byte_cnt += len; } +out_unlock: + /* + * unlock only if we are holding the lock + */ + if (!err) + pthread_rwlock_unlock(&knet_h->global_rwlock); + return; }