user57952: I'm using a Teensy 3.5 to control a wall-following robot. The robot sends telemetry to my PC via two independent ports - Serial1 & Serial.  Serial is the normal USB port, used when I direct-connect to the robot for troubleshooting, etc. Serial1 is tied to an HC-05 Bluetooth transceiver and it is used to acquire telemetry remotely.
I have to send telemetry to both ports, because I never know which (or either, or both) port(s) are actually active at any one time, so I wind up with lines like the following all over my code:
Serial.printf("\nChecking for MPU6050 IMU at I2C Addr 0x%x\n", MPU6050_I2C_ADDR);
Serial1.printf("\nChecking for MPU6050 IMU at I2C Addr 0x%x\n", MPU6050_I2C_ADDR);

needless to say this gets old after a while, and is error prone (I might edit one line and forget to copy/paste it to the other one).  What I would really like to do is create a function that would accept a printf-style string and send it to both ports, but I'm having trouble figuring out how to do that.
I have tried playing around with strcpy and strcat without much success:
  char quotestr[] = "\"";
  char hellostr[] = "hello";
  char instr[] = "%s\n, hellostr";
  char deststr[40];
  memset(deststr, 0, sizeof(deststr));
  strcat(deststr, quotestr);
  strcat(deststr, instr);
  strcat(deststr, quotestr);
  Serial.printf("quotestr = %s\n", quotestr);
  Serial.printf("instr = %s\n", instr);
  Serial.printf("deststr = %s\n", deststr);
  Serial.printf(deststr);

But the output is wrong because the '\n' gets interpreted as a CRLF pair even though it is in a string with double-quotes.
quotestr = "
instr = %s
, hellostr
deststr = "%s
, hellostr"
"
, hellostr"

It seems like there ought to be a way to do this easily, but I can't figure it out.  Any help would be appreciated.
TIA,
Frank

user37622: you can write a simple T output using the base class Print. The standard Arduino Print class doesn't have printf but from your code it looks like the Teense core has printf in Print.
class TeePrint: public Print {

  Print &out1;
  Print &out2;

public:

  TeePrint(Print &_out1, Print &_out2) : out1(_out1), out2(_out2) {}

  virtual size_t write(uint8_t b) {
    out1.write(b);
    out2.write(b);
    return 1;
  }

};

TeePrint log(Serial, Serial1);

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);

  log.printf("abcd %d %s", 1234, "xyz");
}

void loop() {

}


57952: <p>I'm using a Teensy 3.5 to control a wall-following robot. The robot sends telemetry to my PC via two independent ports - Serial1 &amp; Serial.  Serial is the normal USB port, used when I direct-connect to the robot for troubleshooting, etc. Serial1 is tied to an HC-05 Bluetooth transceiver and it is used to acquire telemetry remotely.</p>
<p>I have to send telemetry to both ports, because I never know which (or either, or both) port(s) are actually active at any one time, so I wind up with lines like the following all over my code:</p>
<pre><code>Serial.printf(&quot;\nChecking for MPU6050 IMU at I2C Addr 0x%x\n&quot;, MPU6050_I2C_ADDR);
Serial1.printf(&quot;\nChecking for MPU6050 IMU at I2C Addr 0x%x\n&quot;, MPU6050_I2C_ADDR);
</code></pre>
<p>needless to say this gets old after a while, and is error prone (I might edit one line and forget to copy/paste it to the other one).  What I would really like to do is create a function that would accept a printf-style string and send it to both ports, but I'm having trouble figuring out how to do that.</p>
<p>I have tried playing around with strcpy and strcat without much success:</p>
<pre><code>  char quotestr[] = &quot;\&quot;&quot;;
  char hellostr[] = &quot;hello&quot;;
  char instr[] = &quot;%s\n, hellostr&quot;;
  char deststr[40];
  memset(deststr, 0, sizeof(deststr));
  strcat(deststr, quotestr);
  strcat(deststr, instr);
  strcat(deststr, quotestr);
  Serial.printf(&quot;quotestr = %s\n&quot;, quotestr);
  Serial.printf(&quot;instr = %s\n&quot;, instr);
  Serial.printf(&quot;deststr = %s\n&quot;, deststr);
  Serial.printf(deststr);
</code></pre>
<p>But the output is wrong because the '\n' gets interpreted as a CRLF pair even though it is in a string with double-quotes.</p>
<pre><code>quotestr = &quot;
instr = %s
, hellostr
deststr = &quot;%s
, hellostr&quot;
&quot;
, hellostr&quot;
</code></pre>
<p>It seems like there ought to be a way to do this easily, but I can't figure it out.  Any help would be appreciated.</p>
<p>TIA,</p>
<p>Frank</p>
 37622: <p>you can write a simple T output using the base class <code>Print</code>. The standard Arduino <code>Print</code> class doesn't have <code>printf</code> but from your code it looks like the Teense core has <code>printf</code> in <code>Print</code>.</p>
<pre><code>class TeePrint: public Print {

  Print &amp;out1;
  Print &amp;out2;

public:

  TeePrint(Print &amp;_out1, Print &amp;_out2) : out1(_out1), out2(_out2) {}

  virtual size_t write(uint8_t b) {
    out1.write(b);
    out2.write(b);
    return 1;
  }

};

TeePrint log(Serial, Serial1);

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);

  log.printf(&quot;abcd %d %s&quot;, 1234, &quot;xyz&quot;);
}

void loop() {

}
</code></pre>
