Display command on serial and telnt (optimisation)

Hi in my program i have some debug that i want to display for debug. The user can be connected by serial or by telnet.

For the moment i have the same write 2 time for example for "Debug xxxxx" (it's bigger in the program)

Serial.print(F("Debug xxxxx"));
Telnet.print(F("Debug xxxxx"));

My first idea is to create a function

String displayDebug() {
answer+="Debug xxxxx";
return answer;

But i thing that the string answer can be long an take many memory

Do you have an idea ?

Not sure I follow what you want. This is what I use for debug printing. You can add to the Macros as needed.

EDIT: corrected a typo in the sketch

 Turns on an LED on for one second, then off for one second, repeatedly.

 This example code is in the public domain.

#define DEBUG       //If you comment this line the DPRINT & DPRINTLN
//lines are defined as blank.

#ifdef DEBUG
#define DPRINT(...)    Serial.print(__VA_ARGS__)
#define DPRINTLN(...)  Serial.println(__VA_ARGS__)
#define DRINTF(...)    Serial.print(F(__VA_ARGS__))
#define DPRINTLNF(...) Serial.println(F(__VA_ARGS__))

#define DPRINT(...)    //blank line
#define DPRINTLN(...)  //blank line
#define DPRINTF(...)    //blank line
#define DPRINTLNF(...)  //blank line


// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

int z = 123;

void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     


void loop() {
  digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

  DPRINTLNF("This is in flash");
  DPRINTLN( z );

Thanks LarryD,

But on my side i want to send on different place the same information for example date and time the function is

String printDateTime(){

    answer ="";
    answer +=(RTC.year);
    answer +=("/");
    answer +=(RTC.month);
    answer +=("/");
    answer +=(RTC.day);

    answer +=(" - ");

    answer +=(RTC.hour);
    answer +=(":");
    answer +=(RTC.minute);
    answer +=(":");
    answer +=(RTC.second);
    return answer;

And i use like


Make two functions with the serial output you want displayed. CALL the functions as needed.

Most here would suggest you never use String as it can lead to memory problems.