Compare commits

..

1 Commits

Author SHA1 Message Date
Hongcai Deng fc237d1062
Merge a16fde8070 into 0afae48142 2025-08-19 15:40:49 +08:00
7 changed files with 41 additions and 62 deletions

View File

@ -1,7 +1,4 @@
Version 1.79 2025-08-20
* logger.h export function log_it_ex3
Version 1.78 2025-08-07
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
* add files: spinlock.[hc]

View File

@ -3,7 +3,7 @@
%define CommitVersion %(echo $COMMIT_VERSION)
Name: libfastcommon
Version: 1.0.79
Version: 1.0.78
Release: 1%{?dist}
Summary: c common functions library extracted from my open source projects FastDFS
License: LGPL

View File

@ -115,7 +115,7 @@ static inline int fast_buffer_append_char(FastBuffer *buffer, const char ch)
return 0;
}
static inline int fast_buffer_append_int32(FastBuffer *buffer, const int n)
static inline int fast_buffer_append_int(FastBuffer *buffer, const int n)
{
int result;
@ -143,9 +143,6 @@ static inline int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
return 0;
}
#define fast_buffer_append_int(buffer, n) \
fast_buffer_append_int64(buffer, n)
int fast_buffer_append_file(FastBuffer *buffer, const char *filename);
static inline int fast_buffer_append_string(FastBuffer *buffer, const char *str)

View File

@ -112,7 +112,7 @@ int log_init_ex(LogContext *pContext)
}
pContext->pcurrent_buff = pContext->log_buff;
if ((result=init_pthread_lock(&(pContext->lock))) != 0)
if ((result=init_pthread_lock(&(pContext->log_thread_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->lock));
pthread_mutex_lock(&(pContext->log_thread_lock));
current_size = pContext->current_size;
pthread_mutex_unlock(&(pContext->lock));
pthread_mutex_unlock(&(pContext->log_thread_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->lock);
pthread_mutex_destroy(&pContext->log_thread_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->lock));
pthread_mutex_lock(&(pContext->log_thread_lock));
}
result = log_check_rotate(pContext);
if (bNeedLock)
{
pthread_mutex_unlock(&(pContext->lock));
pthread_mutex_unlock(&(pContext->log_thread_lock));
}
return result;
}
}
if (bNeedLock && ((lock_res=pthread_mutex_lock( \
&(pContext->lock))) != 0))
&(pContext->log_thread_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->lock))) != 0))
&(pContext->log_thread_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;
}
void log_it_ex3(LogContext *pContext, struct timeval *tv,
const char *caption, const char *text, const int text_len,
static void doLogEx(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 @@ void log_it_ex3(LogContext *pContext, struct timeval *tv,
}
}
if (bNeedLock && (result=pthread_mutex_lock(&pContext->lock)) != 0)
if (bNeedLock && (result=pthread_mutex_lock(&pContext->log_thread_lock)) != 0)
{
fprintf(stderr, "file: "__FILE__", line: %d, " \
"call pthread_mutex_lock fail, " \
@ -1018,12 +1018,12 @@ void log_it_ex3(LogContext *pContext, struct timeval *tv,
__LINE__, LOG_BUFF_SIZE, text_len + 64);
if (bNeedLock)
{
pthread_mutex_unlock(&(pContext->lock));
pthread_mutex_unlock(&(pContext->log_thread_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 @@ void log_it_ex3(LogContext *pContext, struct timeval *tv,
log_fsync(pContext, false);
}
if (bNeedLock && (result=pthread_mutex_unlock(&(pContext->lock))) != 0)
if (bNeedLock && (result=pthread_mutex_unlock(&(pContext->log_thread_lock))) != 0)
{
fprintf(stderr, "file: "__FILE__", line: %d, " \
"call pthread_mutex_unlock fail, " \
@ -1076,8 +1076,8 @@ void log_it_ex3(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);
}
log_it_ex3(pContext, &tv, caption, text, text_len, bNeedSync, bNeedLock);
doLogEx(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;
}
log_it_ex3(pContext, tvStart, NULL, text, len, false, true);
doLogEx(pContext, tvStart, NULL, text, len, false, true);
}
const char *log_get_level_caption_ex(LogContext *pContext)

View File

@ -62,7 +62,7 @@ typedef struct log_context
char *pcurrent_buff;
/* mutext lock */
pthread_mutex_t lock;
pthread_mutex_t log_thread_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,13 +355,10 @@ 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:

View File

@ -1632,7 +1632,7 @@ static inline int fc_ltostr(int64_t n, char *buff)
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
{
size_t len;
int len;
len = strlen(src);
if (len < size) {

View File

@ -29,7 +29,6 @@
#include <netinet/tcp.h>
#include <netdb.h>
#include "common_define.h"
#include "shared_func.h"
#define FC_NET_TYPE_NONE 0
#define FC_NET_TYPE_OUTER 1 //extranet IP
@ -54,6 +53,16 @@
#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];
@ -710,16 +719,7 @@ static inline const char *format_ip_address(const char *ip, char *buff)
{
if (is_ipv6_addr(ip))
{
int ip_len;
char *p;
ip_len = strlen(ip);
p = buff;
*p++ = '[';
memcpy(p, ip, ip_len);
p += ip_len;
*p++ = ']';
*p = '\0';
sprintf(buff, "[%s]", ip);
}
else
{
@ -732,26 +732,14 @@ 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)
{
int ip_len;
bool is_ipv6;
char *p;
is_ipv6 = is_ipv6_addr(ip);
ip_len = strlen(ip);
p = buff;
if (is_ipv6)
if (is_ipv6_addr(ip))
{
*p++ = '[';
sprintf(buff, "[%s]:%u", ip, port);
}
memcpy(p, ip, ip_len);
p += ip_len;
if (is_ipv6)
else
{
*p++ = ']';
sprintf(buff, "%s:%u", ip, port);
}
*p++ = ':';
p += fc_itoa(port, p);
*p = '\0';
return buff;
}