Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:20:45+00:00 2026-05-30T04:20:45+00:00

EDIT: The code marked as not working was actually working. It was because of

  • 0

EDIT: The code marked as “not working” was actually working. It was because of a syntax problems in my tests, not detected by the compiler. So the question is already solved, thank you.

C++ is not a language I use everyday, so it is possible that the solution is trivial.

About the context first. I use C++ to develop on a microcontroller (Arduino-based, AVR microcontroller), so I do not use the STL, printf-like functions, new/malloc should be avoided and C++ <string> too.

I have an object called Serial similar to the C++ cout iostream, to communicate with the microcontroller with a serial interface. I have overloaded the “<<” operator of the class from which Serial is an instance so I can do something like that:

Serial << "debug " << "value is " << 3 << endl;

// Whithout the << operator it would be:
Serial.print("debug ");
Serial.print("value is ");
Serial.println(3);

I would like to create a function (or a macro) that enables this kind of line only if debugging is enabled, and which automatically add the “debug” string and append the “endl” value at the end.

So something like that (warning, code does not work because “data” cannot expand as a whole C++ instruction):

#ifdef DEBUG
    #define PRINT_DEBUG(data) do {Serial << "debug " << data << endl;} while(0)
#else
    #define PRINT_DEBUG(data) do {} while(0)
#endif

// This code works
PRINT_DEBUG("hello world");

// This code does not work
int value1 = 3;
char * value2 = "this is a string";
PRINT_DEBUG("sensor1 value:" << value1 << " other sensor value " << value2);

This kind of function/macro would allow me to easily output strings on my serial interface with a specific “string protocol” without having to repeat the “debug” string at the start. It would also allow me to easily disable the print of debug message by not setting the DEBUG macro. I also have only one “#ifdef DEBUG” instead of several ones in my code.

I managed to do something like that with variadic arguments, but I hate this solution because it is dangerous to use (I do not want to specify the number of arguments), and I cannot mix different type of data:

void __rawSend(char * args, ...) {
    Serial.print(args);
    va_list paramList;
    va_start (paramList, args);
    while(true) {
        char * next = va_arg(paramList, char*);
        if (next == NULL) {
            break;
        }
        Serial.print(" ");
        Serial.print(next);
    }
    Serial.println();
    va_end(paramList);
}

#ifdef DEBUG
    #define printDebug(...) do {__rawSend(OUTPUT_DEBUG, __VA_ARGS__, NULL);} while(0)
#else
    #define printDebug(...) do {} while(0)
#endif

int intValue = 1;
char * stringValue = "data";

// This works
printDebug("hello",stringValue);
// This does not works
printDebug("data is", intValue);

How can I do that? Is it possible with macros (while avoiding variadic arguments and mixing different kind of types)? Is there a better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:20:46+00:00Added an answer on May 30, 2026 at 4:20 am

    Sorry all, the code marked as “not working” does actually work. It was because of a syntax problems in my tests, not detected by the compiler.

    Anyway, my solution can benefit other people working with Arduino, as I have seen solutions using printf or trying to recreate printf.

    I used the “<<” operator coming from http://arduiniana.org/libraries/streaming/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code: String my = c.getString(c.getColumnIndexOrThrow(ringtype)); Log.e(my, my); if(my==default) { Log.e(tablename, button marked not visable1);
Edit: The code here still has some bugs in it, and it could do
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
EDIT - The code looks strange here, so I suggest viewing the files directly
I am supposed edit some code for an assignment, and he gave us the
I am using the following code --- EDIT @annakata : Adding the complete code
I have a piece of code: EDIT: The _penParams are initialized as the added
I am getting a StackOverflow Error in this code: EDIT [XmlAttribute(ID)] public string ID
Debugging a Firefox addon is a slow process: (1) edit source code in a
EDIT: The entire code and database creation script can be found from http://gitorious.org/scheator .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.