Passing string as parameter to function and store it in flash

Hey guys,

after almost two hours of testing different ideas from different search results I hope you can get me a solution....

I want to have a function debug() with which I can print / log some information.
My plan is to pass just a string to that debug-function and either print it via Serial.println() or send it via Bluetooth to my phone or ....

But I don't want to store all the debug-messages in the SRAM because it is getting full quite fast. So I thought of storing the variables in the Flash and get the content from there afterwards.

class helper {
  public:
  static void debug(char txt[]) {

    // 1. Storing txt as variable in flash

    char info[] PROGMEM = {txt};  // here is the error
    
    // 2. Get data of stored string and work with it (Serial, Bluetooth, ...)
   
    Serial.println(txt);

  }
};


void setup() {


  helper::debug("just a senseless long string");

  
}
void loop() {
  
}

The error message is

initializer fails to determine size of 'info'

but even if I try to specify the size of info by
char info[sizeof(txt)] PROGMEM = txt; it doesn't work (neither the enclosing of txt with to curly braces).

Could someone please help me with this?

Thanks and regards
Stefan

with answer, but maybe the answerer didn't understand your question.

there are two ways:

a macro

#define DEBUG(msg) Serial.println(F(msg))

the preprocessor will replace every
DEBUG("some debug message");
with
Serial.println(F("some debug message"));
before compilation, so every debug message will stay in flash and not be loaded to SRAM in runtime.

or the way described in the SO answer. a parameter of type FlashStringHelper and usage
helper::debug(F("some debug message"));

class helper {
  public:
    static void debug(__FlashStringHelper *txt) {
      Serial.println(txt);
    }
};


void setup() {
  Serial.begin(115200);
  delay(1000);
  helper::debug(F("just a senseless long string"));
}

void loop() {
}

Or, even simpler:

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println(F("just a senseless long string"));
}

void loop() {
}

gfvalvo:
helper::debug(F("just a senseless long string"));

That's what I like to avoid: the F() macro within the function debug(). I though something like debug("just a senseless long string"); was possible, where the string is then processed within the function itself and not whily passing the parameter.

alve89:
That's what I like to avoid: the F() macro within the function debug().

Why?

I though something like

debug("just a senseless long string");

was possible, where the string is then processed within the function itself and not whily passing the parameter.

Since the char string must be specified to stay in Flash at compile / link time, how do you expect to possibly do it at run time?

Juraj:
there are two ways:

Okay. If these are the only options, I'll take these and say thank you!

gfvalvo:
Since the char string must be specified to stay in Flash at compile / link time, how do you expect to possibly do it at run time?

Hm, very good point. And quite good (and simple) explanation.

Thanks a lot guys, over and out! :grin: