There is a debug class that is used to print out debug statements based on a level. The higher the level, the more detailed messages you would get. The problem occurs when I am trying to do print out a uint64_t. On different platforms using printf style it could be either %li or %lli. I want it to become portable. However, based on this, I could include a PRId64 and it would adjust accordingly. The problem is because my messages are customized, it takes in a const char *. I am unable to do with with a message such as "Calling based on pts %"PRId64, mv.pts because it is expecting a char * where there isn't one. What are other approaches in solving this?
Inside the main code
#include "debug.h"
#include <inttypes.h>
#define P_DBG(level, format, ...) DBGPRINT(level, ("%-5d%s::%s() - " format "\n", __LINE__, str().c_str (), __FUNCTION__, __VA_ARGS__))
#define P_ALL(format, ...) P_DBG(P_ALWAYS, format, __VA_ARGS__)
class Something
{
Something::Print(uint64_t a) {
P_ALL("Calling based on %"PRId64 "\n", a)
}
Inside debug.h
class vdebugPrint {
class vdebugPrint(int ID, int level) : v_id(ID), v_level(level) {}
int Print(const char* formatString, ...) __attribute__((__format(printf,2,3)));
}
...
#define VPRINT3(level, msg) VdebugPrint(ID, level).Print msg
#define PRINT(level, msg) VPRINT##level(level,msg)
#define DBGPRINT(level, msg) PRINT(level,msg)
...
Inside debug.cpp
...
VdebugPrint::Print(const char* format, ...)
{
va_list argumentList;
va_start(argumentList, formatString);
vprintf(formatString, argumentList);
va_end(argumentList);
}
The exact error message is
error: expected ‘)’ before ‘PRId64’
AV_TRACE("Calling based on %"PRId64"\n", mv.pts);
^
/.../debug.h:140:94: note: in definition of macro ‘VPRINT3’
#define VPRINT3(level, msg) VdebugPrint(ID, level).DebugPrint msg
/.../debug.h:146:45: note: in expansion of macro ‘DBGPRINT’
#define DBGPRINT(level, msg) PRINT(level, msg)
^
/.../file.h:54:41: note: in expansion of macro ‘DBGPRINT’
#define P_PBD(level, format, args...) DBGPRINT(level, ("%-5d%s::%s() - " format "\n", __LINE__, str().c_str(), __FUNCTION__, ##args))
EDIT:
I've attempted Olaf suggestion to no avail. Here is what I've done. Everything else is the same except for the main code.
...
class Something
{
Something::Print(uint64_t a) {
const char* message = "Calling based on %";
size_t mlen = strlen(message);
char buf[mlen + strlen(PRId64) + 1];
strcpy(buf, message);
strcpy(buf + mlen, PRId64);
P_ALL(buf, a);
}
}
The error that I get is
error: ‘PRId64’ was not declared in this scope
char buf[mlen + strlen(PRId64) + 1];
^
EDIT2: So I fixed the problem with it not finding PRId64. I had to define __STDC_FORMAT_MACROS from here before include inttypes. This now solves the problem and it compiles!
The macros defined in inttypes.h are string literals with the proper printf type specifier. So you have to append this to the format string. Normal usage would be to use string literal concatenation.
If your format string is variable, you have to append the specifier to the string:
char buf[strlen[format] + strlen[PRId64] + 1];
strcpy(buff, format);
strcat(buff, PRId64);
Note this could be optimized using strcpy for both:
size_t flen = strlen(format);
char buf[flen + strlen[PRId64] + 1];
strcpy(buff, format);
strcpy(buff + flen, PRId64);
Caution: The format string has to be NUL-terminated!
Update:
The original answer was for C as that was one of the language-tags and the code looked more like C than C++. However, according to comments, you can use this also for C++11 (check your compiler's support), if you #define __STDC_FORMAT_MACROS before #include <inttypes.h>. (Note the include might appear in other headers, so you should define the macro as early as possibly in your file.)
You need spaces between the adjacent string literals and the macros for the expansion and concatenation to work. For example:
// Note the space on either side of PRId64
AV_TRACE("Calling based on %" PRId64 "\n", mv.pts);
(I don't know if this is required according to the standard, but it is required to make it work for some preprocessors I've used.)
Related
I faced a problem while implementing the logger.
First, I used to __LINE__ and __FILE__ with standard C Macro function like below
// global.h
..
namespace MyLogger {
class Logger
{
..
void _write(int _level, const char* _file, int _line, const char* _fmt, ...);
};
static Logger logger;
}; // namespace MyLogger
..
// avoid conflict..
#undef error
#undef trace
#define error(_MESSAGE_, ...) _write(LOG_LEVEL_ERROR, (const char*)__FILE__, (int)__LINE__, (const char*)_MESSAGE_, ##__VA_ARGS__)
#define info(_MESSAGE_, ...) _write(LOG_LEVEL_INFO, (const char*)__FILE__, (int)__LINE__, (const char*)_MESSAGE_, ##__VA_ARGS__)
#define trace(_MESSAGE_, ...) _write(LOG_LEVEL_TRACE, (const char*)__FILE__, (int)__LINE__, (const char*)_MESSAGE_, ##__VA_ARGS__)
..
// main.cpp
using namespace MyLogger;
// using like this
logger.error("- log error");
logger.info("- log info %s", "test 1");
..
In fact, it seems to work well.
However, the problem occurred while using openv.
error and trace seem to conflicted with error and trace in opencv.
error: expected primary-expression before ‘int’
[build] CV_EXPORTS CV_NORETURN void error(int _code, const String& _err, const char* _func, const char* _file, int _line);
So I'm thinking about other methods using inline functions, not macros.
// global.h
..
//void _write(int _level, const char* _file, int _line, const char* _fmt, ...);
static inline void error(/* Is it possible to toss __LINE__ or __FILE__ ? */);
static inline void info(/* .. */);
static inline void trace(/* .. */);
..
// main.cpp
logger::error("- log error");
logger::info("%d %c %f, 1, 'A', '0.1');
Can I know the line or file of the location where the log was output through a method other than a macro?
No, inline functions/methods won't work the same as macro in this context.
Macros are simply text replacements, that's why the __LINE__ and __FILE__ will give accurate results. Thus macros are your only choice; See if you can name them better to avoid conflicts.
However the inline functions are systematically compiled subunits. So the line number and the file names will always be same that of those functions where it resides.
Refer: Inline functions vs Preprocessor macros
C++20
As suggested in this useful comment, you may also consider using std::source_location which allows you to avoid the macro trickery via the help of standard library & language features.
There are 3 stages in which C++ becomes a running program.
Preprocessor
Compiler
Linker
__LINE__ and __FILE__ are processed by the preprocessor (stage 1). However, it's the compiler that decides about inlining (stage 2).
Other languages like C# have this in the compiler stage: [CallerMemberName], [CallerFilePath] and [CallerLineNumber] [MSDN] but I am don't think this exists in C++ at the moment.
How about this:
#define log_error(_MESSAGE_, ...) \
logger._write( \
LOG_LEVEL_ERROR, \
(const char*)__FILE__, \
(int)__LINE__, \
(const char*)_MESSAGE_, \
##__VA_ARGS__)
There is no much difference (in the sense of typed characters) between logger.error and log_error. The advantages are, that you use a macro (__FILE__, __LINE__ are utilized as desired) and have your logger class used only at one place (via the macro, in case you want to use a different logging mechanism), refactoring should be without any headaches.
I've inherited some code that causes an amazing amount of compiler warnings and am not a strong enough C++ programmer to figure out how to address it properly. To wit:
log.h:
#include <stdarg.h>
#include <libgen.h>
#include <errno.h>
#include <string.h>
void log(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
#define error(fmt, ...) ow_log("ERROR[%s:%s()#%d]: " fmt, basename(__FILE__), __func__, __LINE__, ##__VA_ARGS__)
#define sys_error(fmt, ...) ow_log("ERROR[%s:%s()#%d]: System error: %s:" fmt, basename(__FILE__), __func__, __LINE__, strerror(errno), ##__VA_ARGS__)
log.cpp
#include <stdarg.h>
#include <stdio.h>
#include <pthread.h>
void log(const char* fmt, ...)
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("%04d-%02d-%02d %02d:%02d:%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
printf("\n");
fflush(stdout);
va_end(args);
}
Example use:
int args = 6;
log("Some string not explicitly typed with %i interpolated arguments", args);
error("Something went wrong here");
C++ programmers probably immediately see it, but I can't. What I get whenever this is used is:
log.h:11:79: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
#define error(fmt, ...) log("ERROR[%s:%s()#%d]: " fmt, basename(__FILE__), __func__, __LINE__, ##__VA_ARGS__)
^
It looks like the code has been this way for some time, and I don't see any terribly bad ill effects, but it makes reading the compiler output quite difficult (and seems actually wrong), so I tried to fix it. I tried some different casting strategies but nothing seems to work, I think I'm misunderstanding how macros work and all the builtins that it uses.
According to this page the prototype for basename() is
char *basename(char *path);
and the documentation says:
The basename() function may modify the string pointed to by path, and may return a pointer to static storage that may then be overwritten by a subsequent call to basename().
Therefore you should not pass a string literal to this function.
The function you should be using is
char * basename_r(const char *path, char *bname);
where bname is scratch storage for the function to use.
I have a task at work to make the static library more secure , in order to do so I need to replace the formatted strings of printf for them to appear differently in the compiled static library , for this to happen it must be done in the pre processor stage .
What I did (and it actually works in visual studio) is the following ( and it is just a pseudo example ):
char * my_array[] = {"abcd", "a %d", " b %d %s "};
#define GENERIC_ARRAY(x) my_array[x]
#define VARIADIC_DEBUG_PRINT(...) DebugPrintFunction (__FILE__, __LINE__, __func__, __VA_ARGS__)
#define PRINT_BY_LEVEL(x) VARIADIC_DEBUG_PRINT x
#define REPLACE_STRING(x,...) PRINT_BY_LEVEL((GENERAL_LEVEL,GENERIC_ARRAY(__COUNTER__),__VA_ARGS__))
#define MY_PRINTF(x,...) REPLACE_STRING((void*)0,(void*)0,__VA_ARGS__)
All of this overhead is for me to trick the compiler to accept prints without any arguments except the string
So when testing it in my main.c I tried the following And it worked :
MY_PRINTF("Hello World");
MY_PRINTF("My Val %d", i);
MY_PRINTF("MY VAL %d My String %s", i, s);
But when switching to GCC, he does not like the format of the first print i.e :
MY_PRINTF("Hello World");
And throws me an compilation error :
error: expected expression before ')' token
Any ideas how may I trick the compiler and accept it ? or maybe better ideas how to rename the string safely after compilation ?
You can try something like :
#include <stdio.h>
#define PRINT(x, ...) printf(x, ##__VA_ARGS__)
int main (int argc, char *argv[]) {
PRINT("Hello\n");
PRINT("World %d\n", 42);
return 0;
}
It works with gcc 4.8 (not tried with earlier version, but it should work too)
Using ##__VA_ARGS__, you may try:
#define MY_PRINTF(x, ...) \
VARIADIC_DEBUG_PRINT(GENERAL_LEVEL, GENERIC_ARRAY(__COUNTER__), (void*)0, (void*)0, ##__VA_ARGS__)
Let’s say there is a debugging function, simplified here as:
void DumpString(char* var, char* varname) {
printf("%s : '%s'\n", varname, var);
}
char str[10]="foobar";
DumpString(str, "str");
> str : foobar
Let’s make it easier by removing the unnecessarily extraneous requirement of passing the variable twice, once in quotes:
#define VARASSTR(v) #v
void DumpString(char* var) {
printf("%s : '%s'\n", VARASSTR(var), var);
}
char str[10]="foobar";
DumpString(str);
> var : foobar
Oops! It uses the local variable name instead of the one passed in. Let’s try a different (and less ideal) tack:
#define DumpStr(v) DumpString(v, #v)
void DumpString(char* var, char* varname) {
printf("%s : '%s'\n", varname, var);
}
char str[10]="foobar";
DumpStr(str);
> str : foobar
Great it works. But what if the function was a little more complicated:
void DumpString(char* var, char* varname, int optionalvar=0) {
printf("%s : '%s'\n", varname, var);
printf("blah: %d", optionalvar);
}
It is not possible to overload a macro, so DumpStr won’t work, and we have already ruled out the version with VARASSTR.
How can this be handled (without resorting to multiple similarly, but differently-named functions/macros)?
This is non-standard, but works as an extension in GNU C:
#define DumpStr(v, ...) DumpString(v, #v, ##__VA_ARGS__)
In GNU C, you can pass no arguments to a variadic macro, and the "token pasting operator" ## when applied between a comma and an empty variadic argument list produces nothing (so the trailing comma is suppressed).
In Visual C++, I believe the token pasting operator ## is unnecessary (and will probably break the macro), as Visual C++ automatically suppresses a trailing comma if it appears before an empty variadic argument list.
Note that the only thing that makes this nonstandard is the desire to sometimes pass an empty argument list. Variadic macros are standardized in both C99 and C++11.
Edit: And here's an example that doesn't use non-standard features. You can see why some people really, really wish this sort of thing was addressed in the standard:
#define DUMPSTR_1(v) DumpString(v, #v)
#define DUMPSTR_2(v, opt) DumpString(v, #v, opt)
#define DUMPSTR_NARG(...) DUMPSTR_ARG_N(__VA_ARGS__, 4, 3, 2, 1, 0)
#define DUMPSTR_ARG_N(_1, _2, _3, _4, n, ...) n
#define DUMPSTR_NC(f, ...) f(__VA_ARGS__)
#define DUMPSTR_NB(nargs, ...) DUMPSTR_NC(DUMPSTR_ ## nargs, __VA_ARGS__)
#define DUMPSTR_NA(nargs, ...) DUMPSTR_NB(nargs, __VA_ARGS__)
#define DumpStr(...) DUMPSTR_NA(DUMPSTR_NARG(__VA_ARGS__), __VA_ARGS__)
There's probably a few cleaner ways to do this. But not that many.
Edit 2: And here's yet another example that doesn't use non-standard features, courtesy of R..
#define STRINGIFY_IMPL(s) #s
#define STRINGIFY(s) STRINGIFY_IMPL(s)
#define ARG1_IMPL(a, ...) a
#define ARG1(...) ARG1_IMPL(__VA_ARGS__, 0)
#define DumpStr(...) DumpString(STRINGIFY(ARG1(__VA_ARGS__)), __VA_ARGS__)
Note that this requires the argument order of the DumpString to be changed so that the stringified function name is the first argument.
I am trying to write some code for a macro which returns the length of a string, and am attempting to implement it using BOOST_PP_WHILE. The code stems from the fact that a character at a position specified by position of the string represented by a macro argument foo may be obtained by #foo[position]. Compiling using either MSVC or Intel C++ results in similar syntax errors; if you could point out why the code is generating these syntax errors and how I would rectify code, it would be greatly appreciated. I know that the errors are caused by the code within the PREDICATE macro, but any expression I attempt to use within it barring BOOST_PP_TUPLE_ELEM results in a compile-time error.
Errors:
prog.cpp:47:1: error: pasting "BOOST_PP_BOOL_" and ""\"Hello, World!\""" does not give a valid preprocessing token
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:47: error: ‘BOOST_PP_TUPLE_ELEM_2_1’ was not declared in this scope
As one would expect, the line numbers are not very useful since both point to the line at which the macro MACRO_STRLEN is called.
Code
Below follows the source listing in which I attempt to implement the macro which I describe.
#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <cstdio>
#define TEST_STRING0 "Hello, World!"
#define MACRO_IS_NULL_IMPL(x, position) \
#x[position] == '\0'
#define MACRO_IS_NULL(x, position) \
MACRO_IS_NULL_IMPL(x, position)
#define PREDICATE_D(string, position) \
MACRO_IS_NULL(string, position)
#define PREDICATE(n, state) \
PREDICATE_D( \
BOOST_PP_TUPLE_ELEM(2, 0, state), \
BOOST_PP_TUPLE_ELEM(2, 1, state) \
)
#define OPERATION_D(string, position) \
( \
string, \
BOOST_PP_INC(position) \
)
#define OPERATION(d, state) \
OPERATION_D( \
BOOST_PP_TUPLE_ELEM(2, 0, state), \
BOOST_PP_TUPLE_ELEM(2, 1, state) \
)
#define MACRO_STRLEN_IMPL(string) \
BOOST_PP_TUPLE_ELEM( \
2, 1, BOOST_PP_WHILE(PREDICATE, OPERATION, (string, 0)) \
)
#define MACRO_STRLEN(string) \
MACRO_STRLEN_IMPL(string)
int main(int argc, char ** argv) {
printf("String length: %d.\n", MACRO_STRLEN(TEST_STRING0));
return 0;
}
How about this - http://codepad.org/aT7SK1Lu
Its still a compile-time strlen, and would be likely much faster to compile.
#include <stdio.h>
#include <string.h>
#define TEST_STRING "Hello, World!"
template <int N> struct xtmp2 { typedef char (&t)[N]; };
template< class T, int N > typename xtmp2<N>::t xlen( T (&)[N] );
#define STRLEN(x) (sizeof(xlen(x))-1)
int main( void ) {
printf( "strlen(\"%s\") = %i %i\n", TEST_STRING, STRLEN(TEST_STRING), strlen(TEST_STRING) );
}
As to macro debug, its possible to get a preprocessor output (like gcc -E);
it may be also helpful to undefine most macros, then enable them one by one to
see what happens.
Please forgive me if this is an irrelevant pointing out.
The predicate for BOOST_PP_WHILE is evaluated while preprocess.
However, if I understand correctly, MACRO_IS_NULL_IMPL determines whether
the character is '\0' at compile-time(runtime?).
So, I think it is difficult to accomplish the goal directly with string
literal "Hello, World!".
It won't work, and for a simple reason: the preprocessor is not meant to deal with literals.
The preprocessor only knows about "tokens", it can catenate them, it can transform one into a string literal, and it can operate macros replacements, but that's it.
Here, the condition to stop the loop (use of [] and ==) could be, at best, executed by the compiler (and most likely at runtime), therefore is not suitable for BOOST_PP_WHILE.
You can, actually, use the compiler to get the number of elements of an array (here an array of characters):
For example using sizeof: sizeof(array)/sizeof(array[0]). This can be abstracted in a macro, however it cannot become a "regular" function, since arrays cannot be passed to "regular" functions, only pointers (where you've lost the information size).
You can also use a template function:
template <typename T, size_t N>
size_t size(T (&)[N]) { return N; }
(this actually work on any array with a constant size)
But, for your own issue, you'll be pleased to know that most compilers have a built-in strlen implementation for constants that evaluates at compile-time.
I wonder if it was supposed to be something like this:
#include <stdio.h>
#include <string.h>
#define TEST_STRING "Hello, World!"
#define STRLEN(x) (x[0]==0)?0:TEST_01(x,1)
#define TEST_01(x,y) (x[y]==0)?y:TEST_02(x,y+1)
#define TEST_02(x,y) (x[y]==0)?y:TEST_03(x,y+1)
#define TEST_03(x,y) (x[y]==0)?y:TEST_04(x,y+1)
#define TEST_04(x,y) (x[y]==0)?y:TEST_05(x,y+1)
#define TEST_05(x,y) (x[y]==0)?y:TEST_06(x,y+1)
#define TEST_06(x,y) (x[y]==0)?y:TEST_07(x,y+1)
#define TEST_07(x,y) (x[y]==0)?y:TEST_08(x,y+1)
#define TEST_08(x,y) (x[y]==0)?y:TEST_09(x,y+1)
#define TEST_09(x,y) (x[y]==0)?y:TEST_10(x,y+1)
#define TEST_10(x,y) (x[y]==0)?y:TEST_11(x,y+1)
#define TEST_11(x,y) (x[y]==0)?y:TEST_12(x,y+1)
#define TEST_12(x,y) (x[y]==0)?y:TEST_13(x,y+1)
#define TEST_13(x,y) (x[y]==0)?y:TEST_14(x,y+1)
#define TEST_14(x,y) (x[y]==0)?y:TEST_15(x,y+1)
#define TEST_15(x,y) (x[y]==0)?y:TEST_16(x,y+1)
#define TEST_16(x,y) (x[y]==0)?y:TEST_17(x,y+1)
#define TEST_17(x,y) (x[y]==0)?y:TEST_18(x,y+1)
#define TEST_18(x,y) (x[y]==0)?y:TEST_19(x,y+1)
#define TEST_19(x,y) (x[y]==0)?y:-1
int main( void ) {
printf( "strlen(\"%s\") = %i %i\n", TEST_STRING, STRLEN(TEST_STRING), strlen(TEST_STRING) );
}
But this isn't a compile-time evaluation, even though it would be usually
optimized to a constant.