Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print::vprintf (and syntactic glue for flash) (IDFGH-8582) #7392

Closed
dirkx opened this issue Oct 23, 2022 · 4 comments · Fixed by #8677
Closed

Print::vprintf (and syntactic glue for flash) (IDFGH-8582) #7392

dirkx opened this issue Oct 23, 2022 · 4 comments · Fixed by #8677
Assignees
Labels
Status: In Progress Issue is in progress Type: Feature request Feature request for Arduino ESP32
Milestone

Comments

@dirkx
Copy link
Contributor

dirkx commented Oct 23, 2022

Is your feature request related to a problem?

Although (AFAIK) the ESP32 does not need the F() or PROGMEM macro to force a const char * into flash -- a lot of code in the Arduino system contains this.

Secondly - the Print class for the ESP32 currently lacks a vprintf -- which is useful if you override the printf (e.g. for a writev() like approach).

Describe the solution you'd like.

May be useful to kill these two birds with one stone; by splitting the printf and adding the syntactic glue.

size_t Print::printf(const __FlashStringHelper *ifsh, ...) {
   va_list arg;
   va_start(arg, ifsh);
   const char * format = (reinterpret_cast<const char *>(ifsh));
   size_t ret = vprintf(format, arg);
   va_end(arg);
   return ret;
}

size_t Print::printf(const char *format, ...)
{
    va_list arg;
    va_start(arg, format);
    size_t ret = vprintf(format, arg);
    va_end(arg);
    return ret;
}

size_t Print::vprinttf(const char *format, va_list arg) {
    char loc_buf[64];
    char * temp = loc_buf;
    va_list copy;
    va_copy(copy, arg);
    int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
    va_end(copy);
    if(len < 0) {
        va_end(arg);
        return 0;
    };
    if(len >= sizeof(loc_buf)){
        temp = (char*) malloc(len+1);
        if(temp == NULL) {
            va_end(arg);
            return 0;
        }
        len = vsnprintf(temp, len+1, format, arg);
    }
    va_end(arg);
    len = write((uint8_t*)temp, len);
    if(temp != loc_buf){
        free(temp);
    }
    return len;
}

Describe alternatives you've considered.

Adding a vprintf and associated in a class above Print -and inject this.

Additional context.

No response

@dirkx dirkx added the Type: Feature request Feature request for Arduino ESP32 label Oct 23, 2022
@github-actions github-actions bot changed the title Print::vprintf (and syntactic glue for flash) Print::vprintf (and syntactic glue for flash) (IDFGH-8582) Oct 23, 2022
@atanisoft
Copy link
Collaborator

This should be transferred to https://github.com/espressif/arduino-esp32.

@Alvin1Zhang Alvin1Zhang transferred this issue from espressif/esp-idf Oct 24, 2022
@dirkx
Copy link
Contributor Author

dirkx commented Oct 24, 2022

@atanisoft @Alvin1Zhang my apologies for misfiling. Thanks for the correction.

@VojtechBartoska
Copy link
Contributor

We'll triage this issue for 3.0.0 milestone and let you know if this request will be implemented.

@lucasssvaz lucasssvaz added Status: In Progress Issue is in progress and removed Status: Awaiting triage Issue is waiting for triage labels Sep 26, 2023
@lucasssvaz
Copy link
Collaborator

vprintf can easily be added and used to implement printf. My main concern is that by adding size_t Print::printf(const __FlashStringHelper *ifsh, ...) we would lose the compile-time checks when using __FlashStringHelper as we cannot use __attribute__ ((format (printf, 2, 3))) like we do with the regular printf. Idk if that's really safe. Wouldn't it be possible to use the regular Serial.print(const __FlashStringHelper *ifsh) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: In Progress Issue is in progress Type: Feature request Feature request for Arduino ESP32
Projects
Development

Successfully merging a pull request may close this issue.

4 participants