mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 10:57:34 +08:00
move some API from fiber's internal to outer for using easily
This commit is contained in:
parent
67394c9c63
commit
bb49d5c617
@ -551,6 +551,55 @@ ssize_t fiber_sendmsg(int sockfd, const struct msghdr* msg, int flags);
|
||||
|
||||
/****************************************************************************/
|
||||
|
||||
/**
|
||||
* 在将写日志至日志文件前回调用户自定义的函数,且将日志信息传递给该函数,
|
||||
* 只有当用户通过 msg_pre_write 进行设置后才生效
|
||||
* @param ctx {void*} 用户的自定义参数
|
||||
* @param fmt {const char*} 格式参数
|
||||
* @param ap {va_list} 格式参数列表
|
||||
*/
|
||||
typedef void (*MSG_PRE_WRITE_FN)(void *ctx, const char *fmt, va_list ap);
|
||||
|
||||
/**
|
||||
* 应用通过此函数类型可以自定义日志记录函数,当应用在打开日志前调用
|
||||
* msg_register 注册了自定义记录函数,则当应用写日志时便用此自定义
|
||||
* 函数记录日志,否则用缺省的日志记录函数
|
||||
* @param ctx {void*} 应用传递进去的参数
|
||||
* @param fmt {const char*} 格式参数
|
||||
* @param ap {va_list} 参数列表
|
||||
*/
|
||||
typedef void (*MSG_WRITE_FN) (void *ctx, const char *fmt, va_list ap);
|
||||
|
||||
/**
|
||||
* 在打开日志前调用此函数注册应用自己的日志记录函数
|
||||
* @param write_fn {MSG_WRITE_FN} 自定义日志记录函数
|
||||
* @param ctx {void*} 自定义参数
|
||||
*/
|
||||
void msg_register(MSG_WRITE_FN write_fn, void *ctx);
|
||||
|
||||
/**
|
||||
* 将 msg_register 注册自定义函数清除,采用缺省的日志函数集
|
||||
*/
|
||||
void msg_unregister(void);
|
||||
|
||||
/**
|
||||
* 在打开日志前调用此函数注册应用的私有函数,在记录日志前会先记录信息通过
|
||||
* 此注册的函数传递给应用
|
||||
* @param pre_write {MSG_PRE_WRITE_FN} 日志记录前调用的函数
|
||||
* @param ctx {void*} 自定义参数
|
||||
*/
|
||||
void msg_pre_write(MSG_PRE_WRITE_FN pre_write, void *ctx);
|
||||
|
||||
/**
|
||||
* 当未调用 msg_open 方式打开日志时,调用了 msg_info/error/fatal/warn
|
||||
* 的操作,是否允许信息输出至标准输出屏幕上,通过此函数来设置该开关,该开关
|
||||
* 仅影响是否需要将信息输出至终端屏幕而不影响是否输出至文件中
|
||||
* @param onoff {int} 非 0 表示允许输出至屏幕
|
||||
*/
|
||||
void msg_stdout_enable(int onoff);
|
||||
|
||||
/****************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -9,40 +9,34 @@
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "fiber/lib_fiber.h"
|
||||
#include "init.h"
|
||||
#include "msg.h"
|
||||
|
||||
|
||||
#ifndef USE_PRINTF_MACRO
|
||||
|
||||
static int __log_open_flag = 0;
|
||||
static int __stdout_enable = 0;
|
||||
|
||||
static MSG_OPEN_FN __open_fn = NULL;
|
||||
static MSG_CLOSE_FN __close_fn = NULL;
|
||||
static MSG_WRITE_FN __write_fn = NULL;
|
||||
static MSG_PRE_WRITE_FN __pre_write_fn = NULL;
|
||||
static void *__pre_write_ctx = NULL;
|
||||
static void *__msg_ctx = NULL;
|
||||
|
||||
void msg_register(MSG_OPEN_FN open_fn, MSG_CLOSE_FN close_fn,
|
||||
MSG_WRITE_FN write_fn, void *ctx)
|
||||
void msg_register(MSG_WRITE_FN write_fn, void *ctx)
|
||||
{
|
||||
if (open_fn == NULL || write_fn == NULL)
|
||||
return;
|
||||
|
||||
__open_fn = open_fn;
|
||||
__close_fn = close_fn,
|
||||
__write_fn = write_fn;
|
||||
__msg_ctx = ctx;
|
||||
if (write_fn != NULL) {
|
||||
__write_fn = write_fn;
|
||||
__msg_ctx = ctx;
|
||||
}
|
||||
}
|
||||
|
||||
void msg_unregister(void)
|
||||
{
|
||||
__open_fn = NULL;
|
||||
__close_fn = NULL;
|
||||
__write_fn = NULL;
|
||||
__msg_ctx = NULL;
|
||||
__write_fn = NULL;
|
||||
__msg_ctx = NULL;
|
||||
__pre_write_fn = NULL;
|
||||
__pre_write_ctx = NULL;
|
||||
}
|
||||
|
||||
void msg_pre_write(MSG_PRE_WRITE_FN pre_write, void *ctx)
|
||||
@ -66,10 +60,8 @@ void msg_info(const char *fmt,...)
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
@ -81,29 +73,6 @@ void msg_info(const char *fmt,...)
|
||||
va_end (ap);
|
||||
}
|
||||
|
||||
void msg_info2(const char *fmt, va_list ap)
|
||||
{
|
||||
if (__pre_write_fn) {
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
#ifdef LINUX
|
||||
printf("msg_info->pid(%d), ", getpid());
|
||||
#elif defined(SOLARIS)
|
||||
printf("msg_info->pid(%ld), ", getpid());
|
||||
#endif
|
||||
vprintf(fmt, ap);
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void msg_warn(const char *fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
@ -114,10 +83,8 @@ void msg_warn(const char *fmt,...)
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
@ -129,25 +96,6 @@ void msg_warn(const char *fmt,...)
|
||||
va_end (ap);
|
||||
}
|
||||
|
||||
void msg_warn2(const char *fmt, va_list ap)
|
||||
{
|
||||
if (__pre_write_fn) {
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
printf("msg_warn->pid(%d), ", getpid());
|
||||
vprintf(fmt, ap);
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void msg_error(const char *fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
@ -158,10 +106,8 @@ void msg_error(const char *fmt,...)
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
@ -173,25 +119,6 @@ void msg_error(const char *fmt,...)
|
||||
va_end (ap);
|
||||
}
|
||||
|
||||
void msg_error2(const char *fmt, va_list ap)
|
||||
{
|
||||
if (__pre_write_fn) {
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
printf("msg_error->pid(%d), ", getpid());
|
||||
vprintf(fmt, ap);
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void msg_fatal(const char *fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
@ -202,10 +129,8 @@ void msg_fatal(const char *fmt,...)
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
@ -219,27 +144,6 @@ void msg_fatal(const char *fmt,...)
|
||||
assert(0);
|
||||
}
|
||||
|
||||
void msg_fatal2(const char *fmt, va_list ap)
|
||||
{
|
||||
if (__pre_write_fn) {
|
||||
__pre_write_fn(__pre_write_ctx, fmt, ap);
|
||||
}
|
||||
|
||||
if (__log_open_flag) {
|
||||
if (__write_fn != NULL) {
|
||||
__write_fn(__msg_ctx, fmt, ap);
|
||||
}
|
||||
}
|
||||
|
||||
if (__stdout_enable) {
|
||||
printf("msg_fatal->pid(%d), ", getpid());
|
||||
vprintf(fmt, ap);
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
assert(0);
|
||||
}
|
||||
|
||||
#endif /* USE_PRINTF_MACRO */
|
||||
|
||||
const char *msg_strerror(int errnum, char *buffer, size_t size)
|
||||
|
@ -11,74 +11,6 @@ extern "C" {
|
||||
|
||||
#undef USE_PRINTF_MACRO
|
||||
|
||||
/**
|
||||
* 在将写日志至日志文件前回调用户自定义的函数,且将日志信息传递给该函数,
|
||||
* 只有当用户通过 msg_pre_write 进行设置后才生效
|
||||
* @param ctx {void*} 用户的自定义参数
|
||||
* @param fmt {const char*} 格式参数
|
||||
* @param ap {va_list} 格式参数列表
|
||||
*/
|
||||
typedef void (*MSG_PRE_WRITE_FN)(void *ctx, const char *fmt, va_list ap);
|
||||
|
||||
/**
|
||||
* 应用通过此函数类型可以自定义日志打开函数,当应用在打开日志前调用
|
||||
* msg_register 注册了自定义打开函数,则当应用调用 msg_open
|
||||
* 时会调用此定义打开日志函数打开日志,否则则用缺省的方法打开日志文件
|
||||
* @param file_name {const char*} 回传给自定义日志打开函数的参数,即
|
||||
* 将日志文件回传
|
||||
* @param ctx {void*} 应用传递进去的参数
|
||||
* @return {int} 如果自定义打开日志函数返回 -1 则调用缺省的日志打开函数
|
||||
*/
|
||||
typedef int (*MSG_OPEN_FN) (const char *file_name, void *ctx);
|
||||
|
||||
/**
|
||||
* 应用通过此函数类型可以自定义日志关闭函数,当应用在打开日志前调用
|
||||
* msg_register 注册了自定义打开函数
|
||||
* @param ctx {void*} 应用传递进去的参数
|
||||
*/
|
||||
typedef void (*MSG_CLOSE_FN) (void *ctx);
|
||||
|
||||
/**
|
||||
* 应用通过此函数类型可以自定义日志记录函数,当应用在打开日志前调用
|
||||
* msg_register 注册了自定义记录函数,则当应用写日志时便用此自定义
|
||||
* 函数记录日志,否则用缺省的日志记录函数
|
||||
* @param ctx {void*} 应用传递进去的参数
|
||||
* @param fmt {const char*} 格式参数
|
||||
* @param ap {va_list} 参数列表
|
||||
*/
|
||||
typedef void (*MSG_WRITE_FN) (void *ctx, const char *fmt, va_list ap);
|
||||
|
||||
/**
|
||||
* 在打开日志前调用此函数注册应用自己的日志打开函数、日志关闭函数、日志记录函数
|
||||
* @param open_fn {MSG_OPEN_FN} 自定义日志打开函数
|
||||
* @param close_fn {MSG_CLOSE_FN} 自定义日志关闭函数
|
||||
* @param write_fn {MSG_WRITE_FN} 自定义日志记录函数
|
||||
* @param ctx {void*} 自定义参数
|
||||
*/
|
||||
void msg_register(MSG_OPEN_FN open_fn, MSG_CLOSE_FN close_fn,
|
||||
MSG_WRITE_FN write_fn, void *ctx);
|
||||
|
||||
/**
|
||||
* 将 msg_register 注册自定义函数清除,采用缺省的日志函数集
|
||||
*/
|
||||
void msg_unregister(void);
|
||||
|
||||
/**
|
||||
* 在打开日志前调用此函数注册应用的私有函数,在记录日志前会先记录信息通过
|
||||
* 此注册的函数传递给应用
|
||||
* @param pre_write {MSG_PRE_WRITE_FN} 日志记录前调用的函数
|
||||
* @param ctx {void*} 自定义参数
|
||||
*/
|
||||
void msg_pre_write(MSG_PRE_WRITE_FN pre_write, void *ctx);
|
||||
|
||||
/**
|
||||
* 当未调用 msg_open 方式打开日志时,调用了 msg_info/error/fatal/warn
|
||||
* 的操作,是否允许信息输出至标准输出屏幕上,通过此函数来设置该开关,该开关
|
||||
* 仅影响是否需要将信息输出至终端屏幕而不影响是否输出至文件中
|
||||
* @param onoff {int} 非 0 表示允许输出至屏幕
|
||||
*/
|
||||
void msg_stdout_enable(int onoff);
|
||||
|
||||
/**
|
||||
* 当记录日志信息至日志文件时,需要调用如下的日志记录函数
|
||||
*/
|
||||
@ -113,35 +45,6 @@ void PRINTF(1, 2) msg_error(const char *fmt,...);
|
||||
*/
|
||||
void PRINTF(1, 2) msg_fatal(const char *fmt,...);
|
||||
|
||||
/**
|
||||
* 一般级别日志信息记录函数
|
||||
* @param fmt {const char*} 参数格式
|
||||
* @param ap {va_list} 变参列表
|
||||
*/
|
||||
void msg_info2(const char *fmt, va_list ap);
|
||||
|
||||
/**
|
||||
* 警告级别日志信息记录函数
|
||||
* @param fmt {const char*} 参数格式
|
||||
* @param ap {va_list} 变参列表
|
||||
*/
|
||||
void msg_warn2(const char *fmt, va_list ap);
|
||||
|
||||
/**
|
||||
* 错误级别日志信息记录函数
|
||||
* @param fmt {const char*} 参数格式
|
||||
* @param ap {va_list} 变参列表
|
||||
*/
|
||||
void msg_error2(const char *fmt, va_list ap);
|
||||
|
||||
|
||||
/**
|
||||
* 致命级别日志信息记录函数
|
||||
* @param fmt {const char*} 参数格式
|
||||
* @param ap {va_list} 变参列表
|
||||
*/
|
||||
void msg_fatal2(const char *fmt, va_list ap);
|
||||
|
||||
#else
|
||||
|
||||
/**
|
||||
@ -154,13 +57,11 @@ void msg_fatal2(const char *fmt, va_list ap);
|
||||
#undef msg_warn
|
||||
#undef msg_error
|
||||
#undef msg_fatal
|
||||
#undef msg_panic
|
||||
|
||||
#define msg_info msg_printf
|
||||
#define msg_warn msg_printf
|
||||
#define msg_error msg_printf
|
||||
#define msg_fatal msg_printf
|
||||
#define msg_panic msg_printf
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -118,14 +118,12 @@ endif
|
||||
|
||||
OBJ_PATH_DST = ./debug
|
||||
LIB_PATH_DST = ../lib
|
||||
DEF_PATH_DST = ./debug
|
||||
|
||||
CFLAGS += -I./src -I../c/include -I./include -I../../lib_acl/include -I../../lib_acl_cpp/include
|
||||
|
||||
#Project's objs
|
||||
|
||||
OBJS_DST = $(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/*.cpp)))
|
||||
OBJS_DEF = $(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/*.cpp)))
|
||||
|
||||
###########################################################
|
||||
.PHONY = static shared clean
|
||||
@ -136,16 +134,10 @@ STATIC_LIBNAME = libfiber_cpp.a
|
||||
SHARED_LIBNAME = libfiber_cpp.so
|
||||
STATIC_OLD_LIBNAME = lib_fiber_cpp.a
|
||||
SHARED_OLD_LIBNAME = lib_fiber_cpp.so
|
||||
PCH = ./acl_stdafx.hpp.gch
|
||||
|
||||
lib: static
|
||||
all: static shared sample
|
||||
|
||||
$(shell mkdir -p $(DEF_PATH_DST))
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(OBJS_DEF)
|
||||
endif
|
||||
|
||||
COMPILE = $(CC) $(CFLAGS)
|
||||
COMPILE_OBJ = @(echo 'building $<'; $(COMPILE) $< -o $@)
|
||||
CREATE_DEF = @(echo 'creating $@'; rm -f $@; \
|
||||
@ -158,7 +150,7 @@ CREATE_DEF = @(echo 'creating $@'; rm -f $@; \
|
||||
sample: static
|
||||
@(cd samples; make)
|
||||
|
||||
static: depends $(OBJS_DST)
|
||||
static: $(OBJS_DST)
|
||||
@echo 'creating $(LIB_PATH_DST)/$(STATIC_LIBNAME)'
|
||||
@$(AR) $(ARFL) $(LIB_PATH_DST)/$(STATIC_LIBNAME) $(OBJS_DST)
|
||||
@$(RANLIB) $(LIB_PATH_DST)/$(STATIC_LIBNAME)
|
||||
@ -166,7 +158,7 @@ static: depends $(OBJS_DST)
|
||||
ln -s ${STATIC_LIBNAME} ${STATIC_OLD_LIBNAME})
|
||||
@echo 'create $(LIB_PATH_DST)/$(STATIC_LIBNAME) ok!'
|
||||
|
||||
shared: depends pch $(OBJS_DST)
|
||||
shared: $(OBJS_DST)
|
||||
@echo 'creating $(SHARED_LIBNAME)'
|
||||
@if test -n "$(rpath)" && test "$(UNIXTYPE)" = "LINUX"; then \
|
||||
echo "building for linux"; \
|
||||
@ -192,28 +184,15 @@ shared: depends pch $(OBJS_DST)
|
||||
echo 'skip build $(SHARED_LIBNAME); usage: make shared rpath=xxx'; \
|
||||
fi
|
||||
|
||||
depends: $(OBJS_DEF)
|
||||
pch: ./acl_stdafx.hpp
|
||||
rm -f $(PCH)
|
||||
@if test "$(UNIXTYPE)" = "LINUX" || test "$(UNIXTYPE)" = "FREEBSD" \
|
||||
|| test "$(UNIXTYPE)" = "MACOSX"; then \
|
||||
$(CC) -o $(PCH) -x c++-header $(CFLAGS) src/acl_stdafx.hpp; \
|
||||
fi
|
||||
|
||||
# src
|
||||
$(OBJ_PATH_DST)/%.o: ./src/%.cpp
|
||||
$(COMPILE_OBJ)
|
||||
|
||||
$(DEF_PATH_DST)/%.inc: ./src/%.cpp
|
||||
$(CREATE_DEF)
|
||||
|
||||
clean cl:
|
||||
rm -f $(LIB_PATH_DST)/${STATIC_LIBNAME}
|
||||
rm -f $(LIB_PATH_DST)/${SHARED_LIBNAME}
|
||||
rm -f $(LIB_PATH_DST)/${STATIC_OLD_LIBNAME}
|
||||
rm -f $(LIB_PATH_DST)/${SHARED_OLD_LIBNAME}
|
||||
rm -f $(OBJS_DST)
|
||||
rm -f $(OBJS_DEF)
|
||||
rm -f $(PCH)
|
||||
|
||||
rebuild rb: clean static
|
||||
|
Loading…
Reference in New Issue
Block a user