# -*- coding: utf-8 -*-
from __future__ import absolute_import
import time
import traceback
from rpc_thrift.services.ttypes import RpcException

def rpc_wrapper_for_class_method(info_logger):
    """
    计算类成员函数的执行时间&异常处理, 使用方法(其中logger可以为None)
        @calculate_class_fun_execute_time(logger)
        def processs_method(self, param1):
            xxx
    """
    def wrapper(func):
        def _calculate_time(*args, **kwargs):
            try:
                if info_logger:
                    t = time.time()
                    result = func(*args, **kwargs)
                    t = (time.time() - t) * 1000
                    args_str = ", ".join(map(str, args[1:]))
                    info_logger.info('\033[32m%s\033[39m(%s), Elapsed: %.2fms' % (func.__name__, args_str, t))
                else:
                    result = func(*args, **kwargs)
                return result
            except Exception as e:
                args_str = ", ".join(map(str, args[1:]))
                raise RpcException(0, '%s(%s), Exception: %s, Trace: %s' % (func.__name__, args_str, str(e), traceback.format_exc()))

        return _calculate_time
    return wrapper



def rpc_wrapper_for_method(info_logger):
    """
    计算普通函数的执行时间&异常处理, 使用方法(其中logger可以为None)
        @calculate_class_fun_execute_time(logger)
        def processs_method(self, param1):
            xxx
    """
    def wrapper(func):
        def _calculate_time(*args, **kwargs):
            try:
                if info_logger:
                    t = time.time()
                    result = func(*args, **kwargs)
                    t = (time.time() - t) * 1000
                    args_str = ", ".join(map(str, args))
                    info_logger.info('\033[32m%s\033[39m(%s), Elapsed: %.4fms' % (func.__name__, args_str, t))
                else:
                    result = func(*args, **kwargs)
                return result
            except Exception as e:
                args_str = ", ".join(map(str, args))
                raise RpcException(0, '%s(%s), Exception: %s, Trace: %s' % (func.__name__, args_str, str(e), traceback.format_exc()))

        return _calculate_time
    return wrapper

@@ -138,16 +138,102 @@
 eption%0A%0A
+# log_func%0A# def log_func(*args, **kwargs, result = None, ex = None, trace=None):%0A#%0A#%0A
 def rpc_
@@ -260,32 +260,49 @@
 thod(info_logger
+, arg_format=None
 ):%0A    %22%22%22%0A    %E8%AE%A1
@@ -603,32 +603,70 @@
  t = time.time()
+%0A%0A                    # rpc%E8%B0%83%E7%94%A8%E4%B8%AD%E6%B2%A1%E6%9C%89kwargs
 %0A               
@@ -693,32 +693,33 @@
 args, **kwargs)%0A
+%0A
                 
@@ -743,32 +743,154 @@
 e() - t) * 1000%0A
+                    if arg_format:%0A                        args_str = arg_format(*args%5B1:%5D)%0A                    else:%0A    
                 
@@ -926,32 +926,32 @@
 str, args%5B1:%5D))%0A
-
                 
@@ -1175,32 +1175,142 @@
 Exception as e:%0A
+                if arg_format:%0A                    args_str = arg_format(*args%5B1:%5D)%0A                else:%0A    
                 
@@ -1342,32 +1342,32 @@
 str, args%5B1:%5D))%0A
-
                 
@@ -1576,16 +1576,33 @@
 o_logger
+, arg_format=None
 ):%0A    %22
@@ -2011,32 +2011,150 @@
 e() - t) * 1000%0A
+                    if arg_format:%0A                        args_str = arg_format(*args)%0A                    else:%0A    
                 
@@ -2186,32 +2186,32 @@
 map(str, args))%0A
-
                 
@@ -2435,32 +2435,138 @@
 Exception as e:%0A
+                if arg_format:%0A                    args_str = arg_format(*args)%0A                else:%0A    
                 

添加了arg_format参数，可以控制log的格式