Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Inserting your own warnings into code

Inserting your own warnings into code

Scheduled Pinned Locked Moved C / C++ / MFC
performancetutorialcode-review
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nde_plume
    wrote on last edited by
    #1

    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.

    M A 2 Replies Last reply
    0
    • N nde_plume

      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.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • N nde_plume

        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.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        ASSERT(0 && "I have to improve this code");

        1 Reply Last reply
        0
        • M Michael Dunn

          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

          N Offline
          N Offline
          nde_plume
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups