Skip to content

printf with FlashHelperString #4224

Closed
Closed
@Ariel-International

Description

@Ariel-International

Hello,
I upgraded to 2.4.0 and I had again to restore my hack to be able to printf with FlashStringHelper string.
I saw the new printf_P feature, but this doesn't solve my issue, because I want to use both F() Strings and normal in debug, without thinking about it.
The new implementation breaks my debug system.

Anyhow: this is the complete hack, which is very simple:

in Print.h

/**************************************
 *
 * printf with PROGMEM flashHelperString
 * by ariel@ariel.international
 **************************************/ 
		#include <stdarg.h>
		
		#ifdef F // check to see if F() macro is available
		size_t printf(const __FlashStringHelper *format, ...);
		#endif

I put this near the end, just before

};
#endif

In Print.cpp:

size_t Print::printf(const __FlashStringHelper *format, ...)
	{
    va_list arg;
    va_start(arg, format);
    char temp[128];
    char* buffer = temp;
    size_t len = vsnprintf_P(temp, sizeof(temp), (const char *)format, arg);  
    va_end(arg);
    if (len > sizeof(temp) - 1) 
		{
        buffer = new char[len + 1];
        if (!buffer) return 0;
		
        va_start(arg, format);
        vsnprintf_P(buffer, len + 1, (const char *)format, arg);
        va_end(arg);
		}
    len = write((const uint8_t*) buffer, len);
    if (buffer != temp) delete[] buffer;
    return len;
	}
	

I usually add this after the original printf(const char *format, ...) function and it adds the requested feature.

I know this is probably more of a pull request than an issue, but I don't know how to make one, so I figured someone will do that if they find this interesting and useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions