logger.h export function log_it_ex3

use_iouring
YuQing 2025-08-20 17:53:23 +08:00
parent 0afae48142
commit 8e51f4de3e
4 changed files with 54 additions and 39 deletions

View File

@ -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 * getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
* add files: spinlock.[hc] * add files: spinlock.[hc]
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline * shared_func.[hc]: change int2buff, buff2int etc. functions to static inline

View File

@ -112,7 +112,7 @@ int log_init_ex(LogContext *pContext)
} }
pContext->pcurrent_buff = pContext->log_buff; 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; return result;
} }
@ -296,9 +296,9 @@ void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_call
{ {
int64_t current_size; int64_t current_size;
pthread_mutex_lock(&(pContext->log_thread_lock)); pthread_mutex_lock(&(pContext->lock));
current_size = pContext->current_size; current_size = pContext->current_size;
pthread_mutex_unlock(&(pContext->log_thread_lock)); pthread_mutex_unlock(&(pContext->lock));
if (current_size == 0) if (current_size == 0)
{ {
log_print_header(pContext); log_print_header(pContext);
@ -347,7 +347,7 @@ void log_destroy_ex(LogContext *pContext)
close(pContext->log_fd); close(pContext->log_fd);
pContext->log_fd = STDERR_FILENO; pContext->log_fd = STDERR_FILENO;
pthread_mutex_destroy(&pContext->log_thread_lock); pthread_mutex_destroy(&pContext->lock);
} }
if (pContext->log_buff != NULL) if (pContext->log_buff != NULL)
@ -918,19 +918,19 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock)
{ {
if (bNeedLock) if (bNeedLock)
{ {
pthread_mutex_lock(&(pContext->log_thread_lock)); pthread_mutex_lock(&(pContext->lock));
} }
result = log_check_rotate(pContext); result = log_check_rotate(pContext);
if (bNeedLock) if (bNeedLock)
{ {
pthread_mutex_unlock(&(pContext->log_thread_lock)); pthread_mutex_unlock(&(pContext->lock));
} }
return result; return result;
} }
} }
if (bNeedLock && ((lock_res=pthread_mutex_lock( \ if (bNeedLock && ((lock_res=pthread_mutex_lock( \
&(pContext->log_thread_lock))) != 0)) &(pContext->lock))) != 0))
{ {
fprintf(stderr, "file: "__FILE__", line: %d, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
"call pthread_mutex_lock fail, " \ "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( \ if (bNeedLock && ((lock_res=pthread_mutex_unlock( \
&(pContext->log_thread_lock))) != 0)) &(pContext->lock))) != 0))
{ {
fprintf(stderr, "file: "__FILE__", line: %d, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
"call pthread_mutex_unlock fail, " \ "call pthread_mutex_unlock fail, " \
@ -977,8 +977,8 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock)
return result; return result;
} }
static void doLogEx(LogContext *pContext, struct timeval *tv, \ void log_it_ex3(LogContext *pContext, struct timeval *tv,
const char *caption, const char *text, const int text_len, \ const char *caption, const char *text, const int text_len,
const bool bNeedSync, const bool bNeedLock) const bool bNeedSync, const bool bNeedLock)
{ {
struct tm tm; 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, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
"call pthread_mutex_lock fail, " \ "call pthread_mutex_lock fail, " \
@ -1018,12 +1018,12 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
__LINE__, LOG_BUFF_SIZE, text_len + 64); __LINE__, LOG_BUFF_SIZE, text_len + 64);
if (bNeedLock) if (bNeedLock)
{ {
pthread_mutex_unlock(&(pContext->log_thread_lock)); pthread_mutex_unlock(&(pContext->lock));
} }
return; 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_BUFF_SIZE)
{ {
log_fsync(pContext, false); log_fsync(pContext, false);
@ -1067,7 +1067,7 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
log_fsync(pContext, false); 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, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
"call pthread_mutex_unlock fail, " \ "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, \ void log_it_ex2(LogContext *pContext, const char *caption,
const char *text, const int text_len, \ const char *text, const int text_len,
const bool bNeedSync, const bool bNeedLock) const bool bNeedSync, const bool bNeedLock)
{ {
struct timeval tv; struct timeval tv;
@ -1092,10 +1092,10 @@ void log_it_ex2(LogContext *pContext, const char *caption, \
gettimeofday(&tv, NULL); 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) const char *text, const int text_len)
{ {
bool bNeedSync; bool bNeedSync;
@ -1291,7 +1291,7 @@ void logAccess(LogContext *pContext, struct timeval *tvStart, \
{ {
len = sizeof(text) - 1; 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) const char *log_get_level_caption_ex(LogContext *pContext)

View File

@ -62,7 +62,7 @@ typedef struct log_context
char *pcurrent_buff; char *pcurrent_buff;
/* mutext lock */ /* mutext lock */
pthread_mutex_t log_thread_lock; pthread_mutex_t lock;
/* /*
rotate the log when the log file exceeds this parameter 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) * text_len: text string length (bytes)
* return: none * 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); const char *text, const int text_len);
/** log to file /** log to file
@ -355,10 +355,13 @@ void log_it_ex1(LogContext *pContext, const int priority, \
* bNeedSync: if sync to file immediatelly * bNeedSync: if sync to file immediatelly
* return: none * return: none
*/ */
void log_it_ex2(LogContext *pContext, const char *caption, \ void log_it_ex2(LogContext *pContext, const char *caption,
const char *text, const int text_len, \ const char *text, const int text_len,
const bool bNeedSync, const bool bNeedLock); 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 /** sync log buffer to log file
* parameters: * parameters:

View File

@ -29,6 +29,7 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netdb.h> #include <netdb.h>
#include "common_define.h" #include "common_define.h"
#include "shared_func.h"
#define FC_NET_TYPE_NONE 0 #define FC_NET_TYPE_NONE 0
#define FC_NET_TYPE_OUTER 1 //extranet IP #define FC_NET_TYPE_OUTER 1 //extranet IP
@ -53,16 +54,6 @@
#define FAST_WRITE_BUFF_SIZE (256 * 1024) #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 { typedef struct fast_if_config {
char name[IF_NAMESIZE]; //if name char name[IF_NAMESIZE]; //if name
char mac[64]; char mac[64];
@ -719,7 +710,16 @@ static inline const char *format_ip_address(const char *ip, char *buff)
{ {
if (is_ipv6_addr(ip)) 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 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, static inline const char *format_ip_port(const char *ip,
const int port, char *buff) 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; return buff;
} }