2017-09-19 92 views
0

我創建了一個日誌功能,一方面需要能夠解析任意數量的參數,但另一方面將總是通過__func__和這個(的調用對象)。可變模板和可變參數組合

如果我使用可變參數模板這樣

template<typename... Args> 
void 
log_device_message_template(const class *object, 
       char const *func, 
       char const *const format, 
       Args const &... args)noexcept 
{ 
log(format, to_str(object).c_str(),format, to_str(args).c_str()...); 
} 

我有打電話給log_device_message_template這樣的: log_device_message_template(this,__func__,some format,some parameters)

,所以我已經添加了宏:

#define log_api_call(format, ...)                                       \ 
    log_device_message_template(                         \ 
     this, __func__, "%s::%s(" format ")", ##__VA_ARGS__) 

事情是,我得到一個seg故障,可能是由於格式不正確哪裏。加入__attribiute __(格式)由於使用了可變參數模板的不工作...

這裏是從Python測試測試記錄的錯誤:

lookup in file=***** [0] 
    28371: symbol=_ZN5***6logger27log_device_message_templateIINS_14*****E13****EEEvNS_21logger_component_eENS_17logger_level_eEPKcPKNS_9objectES7_DpRT_; lookup in file=**** [0] 
    28371: ****: error: symbol lookup error: undefined symbol: _ZN5***6logger27log_device_message_templateIINS_14****E13****EEEvNS_21logger_component_eENS_17logger_level_eEPKcPKNS_9objectES7_DpRT_ (fatal) 
+3

也許這是一個錯字,但你弄亂了宏中參數的順序。 – Holt

+0

符號查找錯誤不是段錯誤。看起來你正試圖調用一個尚未編譯的'log_device_message_template'的實例。爲什麼實例化不存在是任何人的猜測。我們可能需要查看包裝這個Python庫的細節。 – cdhowie

回答

0

這裏的另一種方式來整齊地接近它這避免宏。

請注意在這種情況下,我將日誌記錄發送到標準輸出,但可以很容易地進行修改。我也使用std::ostringstream來構建日誌消息。它的表現並不出名,因此是定製點的理想選擇。

然而,這應該讓你開始,如果你喜歡的方式:

#include <iostream> 
#include <sstream> 
#include <tuple> 
#include <utility> 

// define a constant index type 
template<std::size_t N> using index_c = std::integral_constant<std::size_t, N>; 

// emitting an item at index 0 has no prefix  
template<class T> 
void emit_item(std::ostream &os, index_c<0>, T const &t) 
{ 
    os << t; 
} 


// emitting an item at index N (!= 0) has a comma prefix 
template<std::size_t N, class T> 
void emit_item(std::ostream &os, index_c<N>, T const &t) 
{ 
    os << ", " << t; 
} 

// emit args 0 .. N-1 
template<class Tuple, std::size_t...Is> 
void emit_arglist(std::ostream &sink, Tuple &&tuple, std::index_sequence<Is...>) 
{ 
    using expand = int[]; 
    void(expand{0, 
       (emit_item(sink, index_c<Is>(), std::get<Is>(tuple)), 0)... 
    }); 

}; 

// emitting a 'more' at index 0 has a prefix 
template<class T> 
void emit_more(std::ostream &os, index_c<0>, T const &t) 
{ 
    os << " : " << t; 
} 

// emitting a 'more' at index N (!= 0) has a space prefix 
template<std::size_t N, class T> 
void emit_more(std::ostream &os, index_c<N>, T const &t) 
{ 
    os << " " << t; 
} 

template<class Tuple, std::size_t...Is> 
void emit_more(std::ostream &sink, Tuple &&tuple, std::index_sequence<Is...>) 
{ 
    using expand = int[]; 
    void(expand{0, 
       (emit_more(sink, index_c<Is>(), std::get<Is>(tuple)), 0)... 
    }); 

}; 

template<typename... Args, typename ...MoreStuff> 
std::string 
make_log_string(const char *object, 
       char const *func, 
       std::tuple<Args const &...> const& args, 
       MoreStuff&&...morestuff) noexcept 
{ 
    std::ostringstream ss; 
    ss << object << "::" << func << '('; 
    emit_arglist(ss, args, std::make_index_sequence<sizeof...(Args)>()); 
    ss << ')'; 
    emit_more(ss, std::tie(morestuff...), std::make_index_sequence<sizeof...(MoreStuff)>()); 
    return ss.str(); 
} 

// syntactic sugar for indicating arguments  
template<class...Arg> 
decltype(auto) args(Arg const&...args) 
{ 
    return std::tie(args...); 
} 


int main() 
{ 
    int a = 0, b = 1, c = 2; 
    std::string sa = "xxx", sb = "yyy", sc = "zzz"; 

    const char* Class = "foo"; 
    const char* Func = "var"; 

    std::cout << make_log_string(Class, Func, args(a, b, c)) << std::endl; 
    std::cout << make_log_string(Class, Func, args(sa, b, sc)) << std::endl; 
    std::cout << make_log_string(Class, Func, args(sa, b, sc), "more stuff") << std::endl; 
    std::cout << make_log_string(Class, Func, args(), "empty", "argument", "list") << std::endl; 

} 

預期輸出:

foo::var(0, 1, 2) 
foo::var(xxx, 1, zzz) 
foo::var(xxx, 1, zzz) : more stuff 
foo::var() : empty argument list 

並且以位更多的樣板,我們可以這樣寫:

std::cout << make_log_string(method(Class, Func)(a, b, c)) << std::endl; 
std::cout << make_log_string(method(Class, Func)(sa, b, sc)) << std::endl; 
std::cout << make_log_string(method(Class, Func)(sa, b, sc), "more stuff") << std::endl; 
std::cout << make_log_string(method(Class, Func)(), "empty", "argument", "list") << std::endl; 

它在這裏:

#include <iostream> 
#include <sstream> 
#include <tuple> 
#include <utility> 

template<std::size_t N> using index_c = std::integral_constant<std::size_t, N>; 

template<class T> 
void emit_item(std::ostream &os, index_c<0>, T const &t) 
{ 
    os << t; 
} 

template<std::size_t N, class T> 
void emit_item(std::ostream &os, index_c<N>, T const &t) 
{ 
    os << ", " << t; 
} 

template<class Tuple, std::size_t...Is> 
void emit_arglist(std::ostream &sink, Tuple &&tuple, std::index_sequence<Is...>) 
{ 
    using expand = int[]; 
    void(expand{0, 
       (emit_item(sink, index_c<Is>(), std::get<Is>(tuple)), 0)... 
    }); 

}; 

template<class T> 
void emit_more(std::ostream &os, index_c<0>, T const &t) 
{ 
    os << " : " << t; 
} 

template<std::size_t N, class T> 
void emit_more(std::ostream &os, index_c<N>, T const &t) 
{ 
    os << " " << t; 
} 

template<class Tuple, std::size_t...Is> 
void emit_more(std::ostream &sink, Tuple &&tuple, std::index_sequence<Is...>) 
{ 
    using expand = int[]; 
    void(expand{0, 
       (emit_more(sink, index_c<Is>(), std::get<Is>(tuple)), 0)... 
    }); 

}; 

template<class...Args> 
struct method_with_args; 

struct method 
{ 
    constexpr method(const char* c, const char* f) : klass(c), func(f) {} 
    const char* klass; 
    const char* func; 

    template<class...Args> 
    auto operator()(Args const&...args) -> method_with_args<Args...>; 

    friend std::ostream& operator<<(std::ostream& os, const method& m) 
    { 
     return os << m.klass << "::" << m.func; 
    } 
}; 

template<class...Args> 
struct method_with_args 
{ 
    friend std::ostream& operator<<(std::ostream& os, method_with_args const& ma) 
    { 
     os << ma.m << '('; 
     emit_arglist(os, ma.args, std::make_index_sequence<sizeof...(Args)>()); 
     return os << ')'; 
    } 

    method m; 
    std::tuple<Args const&...> args; 
}; 

template<class...Args> 
auto method::operator()(Args const&...args) -> method_with_args<Args...> 
{ 
    return method_with_args<Args...>{*this, std::tie(args...)}; 

} 

struct function 
{ 
    const char* name; 
}; 

template<typename Method, typename ...MoreStuff> 
std::string 
make_log_string(Method m, 
       MoreStuff &&...morestuff) noexcept 
{ 
    std::ostringstream ss; 
    ss << m; 
    emit_more(ss, std::tie(morestuff...), std::make_index_sequence<sizeof...(MoreStuff)>()); 
    return ss.str(); 
} 


int main() 
{ 
    int a = 0, b = 1, c = 2; 
    std::string sa = "xxx", sb = "yyy", sc = "zzz"; 

    const char *Class = "foo"; 
    const char *Func = "var"; 

    std::cout << make_log_string(method(Class, Func)(a, b, c)) << std::endl; 
    std::cout << make_log_string(method(Class, Func)(sa, b, sc)) << std::endl; 
    std::cout << make_log_string(method(Class, Func)(sa, b, sc), "more stuff") << std::endl; 
    std::cout << make_log_string(method(Class, Func)(), "empty", "argument", "list") << std::endl; 
}