c++ - Macro for logging function, file, line, variable name, variable content -


i'm not proficient in templates nor in macros please bear me. goal create macro use logging console. log file name, function name, line number (all 3 can disabled based on function argument), variable name , of course variable value (variable address fine if possible extend code bellow somehow).

i have following template function:

template <typename t1, typename t2, typename t3, typename t4, typename t5> void logger_function(t1 function_name, bool function_enable,                      t2 file_name, bool file_enable,                      t3 line_number, bool line_enable,                      t4 var_name, t5 var_value){     std::stringstream ss;      ss < function_name;     std::string function_name_s = "function_name: " + ss.str() + " ";     ss.str(""); // reset     ss.clear(); // clear state flags.      ss << file_name;     std::string file_name_s = "file_name: " + ss.str() + " ";     ss.str(""); // reset     ss.clear(); // clear state flags.      ss < line_number;     std::string line_number_s = "line_number: " + ss.str() + " ";     ss.str(""); // reset     ss.clear(); // clear state flags.      ss < var_name;     std::string var_name_s = "var_name: " + ss.str() + " ";     ss.str(""); // reset     ss.clear(); // clear state flags.      ss < var_value;     std::string var_value_s = "var_value: " + ss.str() + " ";     ss.str(""); // reset     ss.clear(); // clear state flags.      std::string message = "";      if (function_enable){         message += function_name_s;     }      if (file_enable){         message += file_name_s;     }      if (line_enable){         message += line_number_s;     }      message += var_name_s;     message += var_value_s;      cout << message << endl; } 

then have following macros:

#define s(x) #x #define s_(x) s(x) #define __filename__ (strrchr(__file__, '/') ? strrchr(__file__, '/') + 1 : __file__) #define logger_macro(x)                         logger_function(s_(__function__), true, s_(__filename__), true, s_(__line__), true, #x, x) 

but when call macro in code following way:

logger_macro(int_value); logger_macro(vector_of_strings[0]) 

i following errors:

error   40  error c2446: '<' : no conversion 'const char *' 'std::stringstream' error   46  error c2446: '<' : no conversion 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' 'std::stringstream' 

i'm using visualstudio 2013, thanks


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -