move some API from fiber's internal to outer for using easily

This commit is contained in:
zsx 2017-12-24 21:53:11 +08:00
parent 67394c9c63
commit bb49d5c617
4 changed files with 69 additions and 236 deletions

View File

@ -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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -9,40 +9,34 @@
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include "fiber/lib_fiber.h"
#include "init.h" #include "init.h"
#include "msg.h" #include "msg.h"
#ifndef USE_PRINTF_MACRO #ifndef USE_PRINTF_MACRO
static int __log_open_flag = 0;
static int __stdout_enable = 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_WRITE_FN __write_fn = NULL;
static MSG_PRE_WRITE_FN __pre_write_fn = NULL; static MSG_PRE_WRITE_FN __pre_write_fn = NULL;
static void *__pre_write_ctx = NULL; static void *__pre_write_ctx = NULL;
static void *__msg_ctx = NULL; static void *__msg_ctx = NULL;
void msg_register(MSG_OPEN_FN open_fn, MSG_CLOSE_FN close_fn, void msg_register(MSG_WRITE_FN write_fn, void *ctx)
MSG_WRITE_FN write_fn, void *ctx)
{ {
if (open_fn == NULL || write_fn == NULL) if (write_fn != NULL) {
return; __write_fn = write_fn;
__msg_ctx = ctx;
__open_fn = open_fn; }
__close_fn = close_fn,
__write_fn = write_fn;
__msg_ctx = ctx;
} }
void msg_unregister(void) void msg_unregister(void)
{ {
__open_fn = NULL; __write_fn = NULL;
__close_fn = NULL; __msg_ctx = NULL;
__write_fn = NULL; __pre_write_fn = NULL;
__msg_ctx = NULL; __pre_write_ctx = NULL;
} }
void msg_pre_write(MSG_PRE_WRITE_FN pre_write, void *ctx) 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); __pre_write_fn(__pre_write_ctx, fmt, ap);
} }
if (__log_open_flag) { if (__write_fn != NULL) {
if (__write_fn != NULL) { __write_fn(__msg_ctx, fmt, ap);
__write_fn(__msg_ctx, fmt, ap);
}
} }
if (__stdout_enable) { if (__stdout_enable) {
@ -81,29 +73,6 @@ void msg_info(const char *fmt,...)
va_end (ap); 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,...) void msg_warn(const char *fmt,...)
{ {
va_list ap; va_list ap;
@ -114,10 +83,8 @@ void msg_warn(const char *fmt,...)
__pre_write_fn(__pre_write_ctx, fmt, ap); __pre_write_fn(__pre_write_ctx, fmt, ap);
} }
if (__log_open_flag) { if (__write_fn != NULL) {
if (__write_fn != NULL) { __write_fn(__msg_ctx, fmt, ap);
__write_fn(__msg_ctx, fmt, ap);
}
} }
if (__stdout_enable) { if (__stdout_enable) {
@ -129,25 +96,6 @@ void msg_warn(const char *fmt,...)
va_end (ap); 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,...) void msg_error(const char *fmt,...)
{ {
va_list ap; va_list ap;
@ -158,10 +106,8 @@ void msg_error(const char *fmt,...)
__pre_write_fn(__pre_write_ctx, fmt, ap); __pre_write_fn(__pre_write_ctx, fmt, ap);
} }
if (__log_open_flag) { if (__write_fn != NULL) {
if (__write_fn != NULL) { __write_fn(__msg_ctx, fmt, ap);
__write_fn(__msg_ctx, fmt, ap);
}
} }
if (__stdout_enable) { if (__stdout_enable) {
@ -173,25 +119,6 @@ void msg_error(const char *fmt,...)
va_end (ap); 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,...) void msg_fatal(const char *fmt,...)
{ {
va_list ap; va_list ap;
@ -202,10 +129,8 @@ void msg_fatal(const char *fmt,...)
__pre_write_fn(__pre_write_ctx, fmt, ap); __pre_write_fn(__pre_write_ctx, fmt, ap);
} }
if (__log_open_flag) { if (__write_fn != NULL) {
if (__write_fn != NULL) { __write_fn(__msg_ctx, fmt, ap);
__write_fn(__msg_ctx, fmt, ap);
}
} }
if (__stdout_enable) { if (__stdout_enable) {
@ -219,27 +144,6 @@ void msg_fatal(const char *fmt,...)
assert(0); 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 */ #endif /* USE_PRINTF_MACRO */
const char *msg_strerror(int errnum, char *buffer, size_t size) const char *msg_strerror(int errnum, char *buffer, size_t size)

View File

@ -11,74 +11,6 @@ extern "C" {
#undef USE_PRINTF_MACRO #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,...); 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 #else
/** /**
@ -154,13 +57,11 @@ void msg_fatal2(const char *fmt, va_list ap);
#undef msg_warn #undef msg_warn
#undef msg_error #undef msg_error
#undef msg_fatal #undef msg_fatal
#undef msg_panic
#define msg_info msg_printf #define msg_info msg_printf
#define msg_warn msg_printf #define msg_warn msg_printf
#define msg_error msg_printf #define msg_error msg_printf
#define msg_fatal msg_printf #define msg_fatal msg_printf
#define msg_panic msg_printf
#endif #endif

View File

@ -118,14 +118,12 @@ endif
OBJ_PATH_DST = ./debug OBJ_PATH_DST = ./debug
LIB_PATH_DST = ../lib 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 CFLAGS += -I./src -I../c/include -I./include -I../../lib_acl/include -I../../lib_acl_cpp/include
#Project's objs #Project's objs
OBJS_DST = $(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/*.cpp))) 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 .PHONY = static shared clean
@ -136,16 +134,10 @@ STATIC_LIBNAME = libfiber_cpp.a
SHARED_LIBNAME = libfiber_cpp.so SHARED_LIBNAME = libfiber_cpp.so
STATIC_OLD_LIBNAME = lib_fiber_cpp.a STATIC_OLD_LIBNAME = lib_fiber_cpp.a
SHARED_OLD_LIBNAME = lib_fiber_cpp.so SHARED_OLD_LIBNAME = lib_fiber_cpp.so
PCH = ./acl_stdafx.hpp.gch
lib: static lib: static
all: static shared sample all: static shared sample
$(shell mkdir -p $(DEF_PATH_DST))
ifneq ($(MAKECMDGOALS),clean)
-include $(OBJS_DEF)
endif
COMPILE = $(CC) $(CFLAGS) COMPILE = $(CC) $(CFLAGS)
COMPILE_OBJ = @(echo 'building $<'; $(COMPILE) $< -o $@) COMPILE_OBJ = @(echo 'building $<'; $(COMPILE) $< -o $@)
CREATE_DEF = @(echo 'creating $@'; rm -f $@; \ CREATE_DEF = @(echo 'creating $@'; rm -f $@; \
@ -158,7 +150,7 @@ CREATE_DEF = @(echo 'creating $@'; rm -f $@; \
sample: static sample: static
@(cd samples; make) @(cd samples; make)
static: depends $(OBJS_DST) static: $(OBJS_DST)
@echo 'creating $(LIB_PATH_DST)/$(STATIC_LIBNAME)' @echo 'creating $(LIB_PATH_DST)/$(STATIC_LIBNAME)'
@$(AR) $(ARFL) $(LIB_PATH_DST)/$(STATIC_LIBNAME) $(OBJS_DST) @$(AR) $(ARFL) $(LIB_PATH_DST)/$(STATIC_LIBNAME) $(OBJS_DST)
@$(RANLIB) $(LIB_PATH_DST)/$(STATIC_LIBNAME) @$(RANLIB) $(LIB_PATH_DST)/$(STATIC_LIBNAME)
@ -166,7 +158,7 @@ static: depends $(OBJS_DST)
ln -s ${STATIC_LIBNAME} ${STATIC_OLD_LIBNAME}) ln -s ${STATIC_LIBNAME} ${STATIC_OLD_LIBNAME})
@echo 'create $(LIB_PATH_DST)/$(STATIC_LIBNAME) ok!' @echo 'create $(LIB_PATH_DST)/$(STATIC_LIBNAME) ok!'
shared: depends pch $(OBJS_DST) shared: $(OBJS_DST)
@echo 'creating $(SHARED_LIBNAME)' @echo 'creating $(SHARED_LIBNAME)'
@if test -n "$(rpath)" && test "$(UNIXTYPE)" = "LINUX"; then \ @if test -n "$(rpath)" && test "$(UNIXTYPE)" = "LINUX"; then \
echo "building for linux"; \ echo "building for linux"; \
@ -192,28 +184,15 @@ shared: depends pch $(OBJS_DST)
echo 'skip build $(SHARED_LIBNAME); usage: make shared rpath=xxx'; \ echo 'skip build $(SHARED_LIBNAME); usage: make shared rpath=xxx'; \
fi 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 # src
$(OBJ_PATH_DST)/%.o: ./src/%.cpp $(OBJ_PATH_DST)/%.o: ./src/%.cpp
$(COMPILE_OBJ) $(COMPILE_OBJ)
$(DEF_PATH_DST)/%.inc: ./src/%.cpp
$(CREATE_DEF)
clean cl: clean cl:
rm -f $(LIB_PATH_DST)/${STATIC_LIBNAME} rm -f $(LIB_PATH_DST)/${STATIC_LIBNAME}
rm -f $(LIB_PATH_DST)/${SHARED_LIBNAME} rm -f $(LIB_PATH_DST)/${SHARED_LIBNAME}
rm -f $(LIB_PATH_DST)/${STATIC_OLD_LIBNAME} rm -f $(LIB_PATH_DST)/${STATIC_OLD_LIBNAME}
rm -f $(LIB_PATH_DST)/${SHARED_OLD_LIBNAME} rm -f $(LIB_PATH_DST)/${SHARED_OLD_LIBNAME}
rm -f $(OBJS_DST) rm -f $(OBJS_DST)
rm -f $(OBJS_DEF)
rm -f $(PCH)
rebuild rb: clean static rebuild rb: clean static