I would like to make single string from __FILE__, __FUNCTION__, and __LINE__ predefined macros.
#define LOG_LINE __FILE__ " " __FUNCTION__ " " __LINE__
This probably will don't work because compiler concat don't work with integers which __LINE__ is.
#define LOG_LINE __FILE__ __FUNCTION__
also don't work, because:
error: expected ';' before '__FUNCTION__'
How to achieve that to have
std::cout << LOG_LINE which will expand to e.g. main.cpp main 8?
Solution:
#define LOG_LINE __FILE__ << " " << __FUNCTION__ does not satisfy me because sometimes I would like to use it as string (big macro with sstream also is not valid solution ;).
You may use the following:
#define LOG_LINE (std::string(__FILE__ " ") + __FUNCTION__ + " " + std::to_string(__LINE__))
Live example
Related
I would like to define a macro to concat __func__ (or __FUNCTION__) with __LINE__:
The following works fine:
// macro_test.cc
#include <iostream>
#define STR2(X) #X
#define STR(X) STR2(X)
#define FILE_LOCATION __FILE__ ":" STR(__LINE__) " "
int main() {
std::cout << FILE_LOCATION << "is <file_name>:<line_number>" << std::endl;
return 0;
}
And here is the output
$ ./a.out
macro_test.cc:8 is <file_name>:<line_number>
However the following gives a compilation error (I just replaced __FILE__ with __func__):
// macro_test.cc
#include <iostream>
#define STR2(X) #X
#define STR(X) STR2(X)
#define FUNC_LOCATION __func__ ":" STR(__LINE__) " "
int main() {
std::cout << FUNC_LOCATION << "is <function_name>:<line_number>" << std::endl;
return 0;
}
~$ gcc macro_test.cc
macro_test.cc: In function ‘int main()’:
macro_test.cc:5:32: error: expected ‘;’ before string constant
#define FUNC_LOCATION __func__ ":" STR(__LINE__) " "
^
macro_test.cc:8:16: note: in expansion of macro ‘FUNC_LOCATION’
std::cout << FUNC_LOCATION << "is <function_name>:<line_number>" << std::endl;
Does anyone know the reason for this and how can I achieve this?
I am using gcc 5.4.0 on Linux (Ubuntu 18.04).
gives a compilation error [...] anyone know the reason for this
__func__ is a variable:
static const char __func__[] = "function-name";
It is not to a (string) literal (to which for example __FILE__ "expands".)
(docs are here: https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html)
Instead of trying to stitch together incompatible types into a single string, you could have an immediately invoked function expression (borrowing from JavaScript terminology) as the macro implementation.
Since it is being immediately executed, I pass in the two preprocessor identifiers as parameters.
They shouldn't be baked into the body of the lambda because then the __func__ will reflect the lambda rather than the routine invoking the lambda.
#include <sstream>
#define FUNC_LOCATION \
[](auto fn, auto ln) { \
std::stringstream ss;
ss << fn << ":" << ln << " "; \
return ss.str(); \
}(__func__, __LINE__)
int main() {
std::cout << FILE_LOCATION << "is <file_name>:<line_number>" << std::endl;
return 0;
}
I would like to concatenate the 3 following strings to produce a good debug output, by using std::setw() after.
__ FILENAME__ , ":" and LINE
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define AT __FILENAME__ ":" __LINE__
#ifdef DEBUG
#ifdef VERBOSE
#define printDebug(x) std::cout << AT << x << std::flush
#else
#define printDebug(x) std::cout << x << std::flush
#endif
#else
#define printDebug(x)
#endif
But actually I receive errors saying that a ";" field is missing before ":". Does someone have an idea ?
I actually call the printDebug() function like that :
printDebug("[SUCCESS] Receiving Message");
You can concatenate string literals by putting them alongside each other.
":" is a string literal.
__LINE__ expands to a numeric literal, not string one.
__FILENAME__ doesn't expand to a literal at all. It expands to an expression.
There is a way to get a string literal out of __LINE__, but you can't make __FILENAME__ a string literal.
You don't need to use literal concatenation here at all. You can simply do this:
#ifdef VERBOSE
#define printDebug(x) std::cout << __FILENAME__ << ":" << __LINE__ << x << std::flush
We have some Macros like
#define LOGG(out,line,file,.....)
std::stringstream logprocess; \
logprocess << std::stringstream(out) ; \
functiona(out,....,....);
}
So the thing to be passed to LOGG can be like:
LOGG(message<<"i would like to print"<<interger,......)
So we mainly use "<<" to constructor from whater ever type we want and then send to the MACRO LOGG. It works perfect with gnuc++ 98 but we it is compiled with -std=c++11, it gives error:
error: no match for 'operator<<' (operand types are 'std::stringstream {aka std::basic_stringstream}' and 'std::stringstream {aka std::basic_stringstream}')
Well, as #thundium pointed out, the error message is pretty clear.
An std::stringstream cannot accept an std::stringstream using it's << operator. Maybe you could use this instead:
logprocess << std::stringstream(out).str()
But I won't recommend it.
I can't say for sure how exactly you use this macro in your code, but I do spot a few possible improvements, for example:
Why does your macro receive file and line as parameters? This is a macro and it can use the __FILE__ and __LINE__ inherently
Why create two string streams when one is sufficient?
Why have an additional functional wrapper around the actual logging facility and not just use std::cout or std::ofstream?
Here are two simple and trivial implementations I think you can use in your code with minimum adjustments:
#define LOGG1(...) \
std::cout << __FILE__ << "[" << __LINE__ << "] " << __VA_ARGS__ << "\n"
#define LOGG2(...) \
std::stringstream ss; \
ss << __FILE__ << "[" << __LINE__ << "] " << __VA_ARGS__ << "\n"; \
functional(ss.str());
Here is a full example
How do I get the text of the data given to value, not the value of the executed expression?
#define PRINT_VALUE(value) std::cout << "Value " << __RAWTEXT(value) << " is " << value << "\n";
__RAWTEXT is something I made up. Is there really something out there that does this though?
int testVariable = 5;
PRINT_VALUE(testVariable);
The output of this should be
Value testVariable is 5
Use the "stringize" operator # for this:
#define PRINT_VALUE(value) std::cout << "Value " << #value << " is " << value << "\n";
It's pretty straightforward, #TTT in a macro converts TTT to "TTT", a string literal.
It's worth mentioning that when the parameter is itself a macro, you'll get the name of the macro. However, if the parameter is passed to a subsequent macro, it's "unpacked". So you see these sometimes:
#define STRINGIZE2(X) #X
#define STRINGIZE(X) STRINGIZE2(X)
Here they are in action:
#define TEST Bob
std::cout << #TEST; //results in "TEST"
std::cout << STRINGIZE2(TEST); //results in "TEST"
std::cout << STRINGIZE(TEST); //results in "Bob"
Not relevent to your question but also notable is the "concat" macro operator ## which "glues" two bits of text togeather. std::st ## ing results in std::string. Useful in macros:
#define make_thing(X) \
structX##_class {
static const char* const name=#X;
};
make_thing(Foo);
std::cout << Foo_class::name;
And again, if a parameter is a macro, you get the macro name. So here's the de-macro macros:
#define GLUE2(X,Y) (X##Y)
#define GLUE(X,Y) GLUE2(X,Y)
#define HEY "HELLO"
#define THERE "WORLD"
std::cout << GLUE(HEY,THERE); //"HELLOWORLD"
This is my assert function (it wont compile "error C2110: '+' : cannot add two pointers"):
#define CHAR(x) #x
template<typename T>
inline void ASSERT(T x)
{
if(!x)
{
std::string s("ERROR! Assert " + CHAR(x) + " failed. In file " + __FILE__ +
" at line " + __LINE__ + ".");
std::wstring temp(s.length(), L' ');
std::copy(s.begin(), s.end(), temp.begin());
getLogger().Write(temp);
}
}
Any idea of how to fix it?
String Literals are easily reduced to char pointers, which cannot be added as you try to do with "ERROR! Assert " + CHAR(x) + " failed. In file ".... However, C++ has the handy feature of doing this automatically before compilation! (the preprocessor does this). Even better, it has a handy tool for making wide strings at compile time. So, you want:
#define _T(x) L ## x
#define CHAR(x) #x
#define CHAR2(x) CHAR(x)
#define ASSERT(x) ASSERT2(x, CHAR(x), __FILE__, CHAR2(__LINE__))
#define ASSERT2(x, t, f, l) \
if(!x) \
getLogger().Write(L"ERROR! Assert " _T(t) L" failed. In file " _T(f) L" at line " _T(l) L".");
http://ideone.com/0ibcj
The compiler error is quite clear; you are trying to apply the + operator to string literals. A quick way to fix it is enclosing the first string literal in std::string().
As #James McNellis pointed out, note that FILE and LINE will point to the file and line of the assert function declaration.
You cannot use the + operator to concatenate two char*s; you need printf or some sort of thing for that.
"ERROR! Assert " is a null-terminated, C-style string. You can't execute operator+ on it.
A few issues:
Generally an assert should break into the debugger or dump if one is not attached. This will not.
As already mentioned, your LINE and FILE require use in a macro
You need a couple "helper" macros to get the strings working properly
Try something along these lines:
#define ASSERT_QUOTE_(x) #x
#define ASSERT_QUOTE_(x) ASSERT_QUOTE_(x)
#define MY_ASSERT(cond) \
if(cond) {} else { \
std::stringstream ss; \
ss << "ERROR! Assert " << ASSERT_QUOTE(cond) << " failed. In file " << __FILE__ << " at line " << __LINE__ << "."; \
getLogger().Write(ss.str()); \
}
Be careful trying to use STL here however. I suggest you have your logger's Write() function take variable arguments and process them with printf() or perhaps boost::format