Inserting your own warnings into code
-
I want to insert my own warnings into code, to leave a fixed record of things I have delayed doing. For example, I have a function that converts a block of data into ASCII text, for storage. However, the first quick and nasty implementation does not get very good compression performance. So I want to insert a line in the function saying something like: #pragma message(__FILE__ " (" __LINE__ "): warning C1234: " \ "Need to improve the compression here.") However I cann't get it to work right (__LINE__ is a number, and #__LINE__ doesn't seem to work in this context.) I seem to remember something here about this, but I couldn't find it. Any suggestions would be most helpful.
-
I want to insert my own warnings into code, to leave a fixed record of things I have delayed doing. For example, I have a function that converts a block of data into ASCII text, for storage. However, the first quick and nasty implementation does not get very good compression performance. So I want to insert a line in the function saying something like: #pragma message(__FILE__ " (" __LINE__ "): warning C1234: " \ "Need to improve the compression here.") However I cann't get it to work right (__LINE__ is a number, and #__LINE__ doesn't seem to work in this context.) I seem to remember something here about this, but I couldn't find it. Any suggestions would be most helpful.
The
#
operator only works in a macro definition, not a#pragma
, so do something like:#define MAKESTR(x) #x
#pragma message "foo" MAKESTR(__LINE__) "bar"--Mike-- "I'd rather you just give me a fish today, because even if you teach me how to fish, I won't do it. I'm lazy." -- Nish Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm
-
I want to insert my own warnings into code, to leave a fixed record of things I have delayed doing. For example, I have a function that converts a block of data into ASCII text, for storage. However, the first quick and nasty implementation does not get very good compression performance. So I want to insert a line in the function saying something like: #pragma message(__FILE__ " (" __LINE__ "): warning C1234: " \ "Need to improve the compression here.") However I cann't get it to work right (__LINE__ is a number, and #__LINE__ doesn't seem to work in this context.) I seem to remember something here about this, but I couldn't find it. Any suggestions would be most helpful.
-
The
#
operator only works in a macro definition, not a#pragma
, so do something like:#define MAKESTR(x) #x
#pragma message "foo" MAKESTR(__LINE__) "bar"--Mike-- "I'd rather you just give me a fish today, because even if you teach me how to fish, I won't do it. I'm lazy." -- Nish Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm
None of the suggestions offered worked, so I asked the same question on comp.lang.c++.moderated, and they pointed out that you need to double expand the __LINE__ macro for some subtle reasons. Anyways, for future readers interested in how to do it, here is what I now use: #define STRINGIZE(x) #x #define EVAL_STRINGIZE(x) STRINGIZE(x) #define WARN(warning) message(__FILE__ "("EVAL_STRINGIZE(__LINE__)"): warning C1000: " warning) Put these macros in a general project header included everywhere (I guess you could even put them in stdafx.h if you are so inclined, personally, all my projects have a file called Project.h that is the first included in every header.) Then whenever you want to issue a warning during compile time just use this: #pragma WARN("Using bubble sort here, a better algorithm is required") which will appear on your compiler output (and task list if using VC .Net), which you can F4 through. When you fix the error, just remove the pragma.