/*
 * rpc_util.h
 *
 *  Created on: 2011-3-26
 *      Author: yanghu
 */

#ifndef RPC_UTIL_H_
#define RPC_UTIL_H_

#include "rpc_types.h"
#include <stdarg.h>

#define DEBUG_ERROR 1
#define DEBUG_WARN  1
#define DEBUG_INFO  1
#define DEBUG_DBG   0

/* log */
#define rpc_log_error(fmt, ...)                                     \
	do {                                                            \
		if (DEBUG_ERROR)                                            \
			printf("ERROR! %s():%d: " fmt,                          \
				   __func__, __LINE__, ##__VA_ARGS__);              \
	} while (0)
	
#define rpc_log_warn(fmt, ...)                                      \
    do {                                                            \
        if (DEBUG_WARN)                                             \
            printf("WARN! %s():%d: " fmt,                           \
                   __func__, __LINE__, ##__VA_ARGS__);              \
    } while (0) 
    
#define rpc_log_info(fmt, ...)                                      \
    do {                                                            \
        if (DEBUG_INFO)                                             \
            printf("INFO! %s():%d: " fmt,                           \
                   __func__, __LINE__, ##__VA_ARGS__);              \
    } while (0)  

#define rpc_log_dbg(fmt, ...)                                      \
    do {                                                            \
        if (DEBUG_DBG)                                             \
            printf("DBG! %s():%d: " fmt,                           \
                   __func__, __LINE__, ##__VA_ARGS__);              \
    } while (0)  


#define ASSERT_RETCONN_VOID(conn,ret) \
do { \
    if (ret != RET_OK) \
    {\
        rpc_log_error("ret error: %s\n", rpc_code_format(ret));\
        rpc_return_error(conn, ret, (char *)rpc_code_format(ret));\
        return;\
    }\
} while (0)  

#define ASSERT_RETCONN(conn,ret) \
do { \
    if (ret != RET_OK) \
    {\
        rpc_log_error("ret error: %s\n", rpc_code_format(ret));\
        rpc_return_error(conn, ret, (char *)rpc_code_format(ret));\
        return ret;\
    }\
} while (0)  

#define ASSERT_RET(ret) \
do { \
    if (ret != RET_OK) \
    {\
        rpc_log_error("ret error: %s\n", rpc_code_format(ret));\
        return ret;\
    }\
} while (0)

#define ASSERT_RET_VOID(ret) \
do { \
    if (ret != RET_OK) \
    {\
        rpc_log_error("ret error: %s\n", rpc_code_format(ret));\
        return;\
    }\
} while (0)

#define ASSERT_RET_NO(ret) \
do { \
    if (ret != RET_OK) \
    {\
        rpc_log_error("ret error: %s\n", rpc_code_format(ret));\
    }\
} while (0)

#define RET_ERR_FORMART(ret, errno, buff, buff_size) \
do { \
    if(ret == RET_SYSERR)\
    {\
        sprintf(buff, "%s\n", strerror(errno));\
        (buff_size) = strlen(buff);\
        (buff_size)++;\
    }\
    else if(ret != RET_OK)\
    {\
        sprintf(buff, "%s\n", rpc_code_format(ret));\
        (buff_size) = strlen(buff);\
        (buff_size)++;\
    }\
} while (0)

char* rpc_vsprintf(char *format, ...);

int rpc_vslen(char const *format, va_list args);

int rpc_vasprintf(char **string, char const *format, va_list args);

void rpc_sleep(int ms);

void rpc_set_non_block(int fd);

long rpc_time_msec(void);

#endif /* RPC_UTIL_H_ */