diff --git a/HISTORY b/HISTORY index e69a9c2..8ca8d0c 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.78 2025-08-07 +Version 1.78 2025-08-20 * getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6 * add files: spinlock.[hc] * shared_func.[hc]: change int2buff, buff2int etc. functions to static inline diff --git a/src/logger.c b/src/logger.c index 3f13872..130f24d 100644 --- a/src/logger.c +++ b/src/logger.c @@ -112,7 +112,7 @@ int log_init_ex(LogContext *pContext) } pContext->pcurrent_buff = pContext->log_buff; - if ((result=init_pthread_lock(&(pContext->log_thread_lock))) != 0) + if ((result=init_pthread_lock(&(pContext->lock))) != 0) { return result; } @@ -296,9 +296,9 @@ void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_call { int64_t current_size; - pthread_mutex_lock(&(pContext->log_thread_lock)); + pthread_mutex_lock(&(pContext->lock)); current_size = pContext->current_size; - pthread_mutex_unlock(&(pContext->log_thread_lock)); + pthread_mutex_unlock(&(pContext->lock)); if (current_size == 0) { log_print_header(pContext); @@ -347,7 +347,7 @@ void log_destroy_ex(LogContext *pContext) close(pContext->log_fd); pContext->log_fd = STDERR_FILENO; - pthread_mutex_destroy(&pContext->log_thread_lock); + pthread_mutex_destroy(&pContext->lock); } if (pContext->log_buff != NULL) @@ -918,19 +918,19 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock) { if (bNeedLock) { - pthread_mutex_lock(&(pContext->log_thread_lock)); + pthread_mutex_lock(&(pContext->lock)); } result = log_check_rotate(pContext); if (bNeedLock) { - pthread_mutex_unlock(&(pContext->log_thread_lock)); + pthread_mutex_unlock(&(pContext->lock)); } return result; } } if (bNeedLock && ((lock_res=pthread_mutex_lock( \ - &(pContext->log_thread_lock))) != 0)) + &(pContext->lock))) != 0)) { fprintf(stderr, "file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ @@ -966,7 +966,7 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock) } if (bNeedLock && ((lock_res=pthread_mutex_unlock( \ - &(pContext->log_thread_lock))) != 0)) + &(pContext->lock))) != 0)) { fprintf(stderr, "file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ @@ -977,8 +977,8 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock) return result; } -static void doLogEx(LogContext *pContext, struct timeval *tv, \ - const char *caption, const char *text, const int text_len, \ +void log_it_ex3(LogContext *pContext, struct timeval *tv, + const char *caption, const char *text, const int text_len, const bool bNeedSync, const bool bNeedLock) { struct tm tm; @@ -1003,7 +1003,7 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ } } - if (bNeedLock && (result=pthread_mutex_lock(&pContext->log_thread_lock)) != 0) + if (bNeedLock && (result=pthread_mutex_lock(&pContext->lock)) != 0) { fprintf(stderr, "file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ @@ -1018,12 +1018,12 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ __LINE__, LOG_BUFF_SIZE, text_len + 64); if (bNeedLock) { - pthread_mutex_unlock(&(pContext->log_thread_lock)); + pthread_mutex_unlock(&(pContext->lock)); } return; } - if ((pContext->pcurrent_buff - pContext->log_buff) + text_len + 64 \ + if ((pContext->pcurrent_buff - pContext->log_buff) + text_len + 64 > LOG_BUFF_SIZE) { log_fsync(pContext, false); @@ -1067,7 +1067,7 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ log_fsync(pContext, false); } - if (bNeedLock && (result=pthread_mutex_unlock(&(pContext->log_thread_lock))) != 0) + if (bNeedLock && (result=pthread_mutex_unlock(&(pContext->lock))) != 0) { fprintf(stderr, "file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ @@ -1076,8 +1076,8 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ } } -void log_it_ex2(LogContext *pContext, const char *caption, \ - const char *text, const int text_len, \ +void log_it_ex2(LogContext *pContext, const char *caption, + const char *text, const int text_len, const bool bNeedSync, const bool bNeedLock) { struct timeval tv; @@ -1092,10 +1092,10 @@ void log_it_ex2(LogContext *pContext, const char *caption, \ gettimeofday(&tv, NULL); } - doLogEx(pContext, &tv, caption, text, text_len, bNeedSync, bNeedLock); + log_it_ex3(pContext, &tv, caption, text, text_len, bNeedSync, bNeedLock); } -void log_it_ex1(LogContext *pContext, const int priority, \ +void log_it_ex1(LogContext *pContext, const int priority, const char *text, const int text_len) { bool bNeedSync; @@ -1291,7 +1291,7 @@ void logAccess(LogContext *pContext, struct timeval *tvStart, \ { len = sizeof(text) - 1; } - doLogEx(pContext, tvStart, NULL, text, len, false, true); + log_it_ex3(pContext, tvStart, NULL, text, len, false, true); } const char *log_get_level_caption_ex(LogContext *pContext) diff --git a/src/logger.h b/src/logger.h index c276450..bfd0005 100644 --- a/src/logger.h +++ b/src/logger.h @@ -62,7 +62,7 @@ typedef struct log_context char *pcurrent_buff; /* mutext lock */ - pthread_mutex_t log_thread_lock; + pthread_mutex_t lock; /* rotate the log when the log file exceeds this parameter @@ -343,7 +343,7 @@ void log_it_ex(LogContext *pContext, const int priority, \ * text_len: text string length (bytes) * return: none */ -void log_it_ex1(LogContext *pContext, const int priority, \ +void log_it_ex1(LogContext *pContext, const int priority, const char *text, const int text_len); /** log to file @@ -355,10 +355,13 @@ void log_it_ex1(LogContext *pContext, const int priority, \ * bNeedSync: if sync to file immediatelly * return: none */ -void log_it_ex2(LogContext *pContext, const char *caption, \ - const char *text, const int text_len, \ +void log_it_ex2(LogContext *pContext, const char *caption, + const char *text, const int text_len, const bool bNeedSync, const bool bNeedLock); +void log_it_ex3(LogContext *pContext, struct timeval *tv, + const char *caption, const char *text, const int text_len, + const bool bNeedSync, const bool bNeedLock); /** sync log buffer to log file * parameters: diff --git a/src/sockopt.h b/src/sockopt.h index e53ea56..a67c4f6 100644 --- a/src/sockopt.h +++ b/src/sockopt.h @@ -29,6 +29,7 @@ #include #include #include "common_define.h" +#include "shared_func.h" #define FC_NET_TYPE_NONE 0 #define FC_NET_TYPE_OUTER 1 //extranet IP @@ -53,16 +54,6 @@ #define FAST_WRITE_BUFF_SIZE (256 * 1024) -#define FC_FORMAT_IP_ADDRESS(old_ip_addr, new_ip_addr) \ - char new_ip_addr[FORMATTED_IP_SIZE]; \ - do { \ - if (is_ipv6_addr(old_ip_addr)) { \ - sprintf(new_ip_addr, "[%s]", old_ip_addr); \ - } else { \ - strcpy(new_ip_addr, old_ip_addr); \ - } \ - } while (0) - typedef struct fast_if_config { char name[IF_NAMESIZE]; //if name char mac[64]; @@ -719,7 +710,16 @@ static inline const char *format_ip_address(const char *ip, char *buff) { if (is_ipv6_addr(ip)) { - sprintf(buff, "[%s]", ip); + int ip_len; + char *p; + + ip_len = strlen(ip); + p = buff; + *p++ = '['; + memcpy(p, ip, ip_len); + p += ip_len; + *p++ = ']'; + *p = '\0'; } else { @@ -732,14 +732,26 @@ static inline const char *format_ip_address(const char *ip, char *buff) static inline const char *format_ip_port(const char *ip, const int port, char *buff) { - if (is_ipv6_addr(ip)) + int ip_len; + bool is_ipv6; + char *p; + + is_ipv6 = is_ipv6_addr(ip); + ip_len = strlen(ip); + p = buff; + if (is_ipv6) { - sprintf(buff, "[%s]:%u", ip, port); + *p++ = '['; } - else + memcpy(p, ip, ip_len); + p += ip_len; + if (is_ipv6) { - sprintf(buff, "%s:%u", ip, port); + *p++ = ']'; } + *p++ = ':'; + p += fc_itoa(port, p); + *p = '\0'; return buff; }