Using Log to print variables?

I started to use Log::Print for debug. It worked fine for strings I wrote in the code like this: “debug text”

But now I am trying to print numbers from variables, for example in the seed/Blink.cpp example, I would like to print the value of the led (0 or 1), led_state

#include “daisy_seed.h”

// Use the daisy namespace to prevent having to type
// daisy:: before all libdaisy functions
using namespace daisy;

// Declare a DaisySeed object called hardware
DaisySeed hardware;
TimerHandle timer;
int main(void)
{
// Declare a variable to store the state we want to set for the LED.
bool led_state;
led_state = true;

// Configure and Initialize the Daisy Seed
// These are separate to allow reconfiguration of any of the internal
// components before initialization.
hardware.Configure();
hardware.Init();

hardware.StartLog(true); //(true) se queda esperando que la PC escuche en cada PrintLine()

// Loop forever
for( ; ; )
{
// Set the onboard LED
hardware.SetLed(led_state);
hardware.PrintLine(“changing Led”); //HERE I WOULD LIKE TO PRINT led_state
// Toggle the LED state for the next time around.
led_state = !led_state;

// Wait 500ms
System::Delay(500);
}
}

The problem is that Print() only accepts const char*, can you use Print() with a variable?

This should work:

  hw.PrintLine("Led state:%s", led_state? "on" : "off");

Other options:

  int myint = 42;
  float myfloat = 1.234f;
  hw.PrintLine("Print an int value:%d", myint);
  hw.PrintLine("Print a float value:%.3f", myfloat);
  hw.PrintLine("Mix them: %f is ok, but %d is the %s", myfloat, myint, "answer");
2 Likes

Oh I didn’t realize it worked like PrintF() from C

Thanks

resurrecting this thread for s similar question…

i can get it daisy logger’s PrintLine function to print characters but not full strings. i’ve tested the serial monitors in both visual studio and arduino ide. is it possible to print a string?

when i do something like:

std::string test = "test string";
hw.PrintLine( "here's a string: %s",test);

my results look like: here's a string: ��

I’m just guessing, since I’m not near my computer, but maybe %s is expecting a C string ( 0 terminated array of characters) , not a C++ object.

Not really guessing, I’m pretty sure about this.

2 Likes

thank you so much for the explanation.

i was able to print the string by converting it to a C string with c_str().

here’s the full code i used to confirm this, using a vector containing two string objects:

#include "daisysp.h"
#include "daisy_seed.h"
#include <cstring>
using namespace daisy;
using namespace daisysp;

static DaisySeed hw;

int main(void)
{
    hw.Configure();
    hw.Init();

	// Start the log, and wait for connection
    hw.StartLog(true);
 	
	std::vector<std::string> cpp_string = 
	{
		"daisy",
		"seed"
	};

	System::Delay(500);
	char const  * c_str0 = cpp_string[0].c_str();
	char const  * c_str1 = cpp_string[1].c_str();
	hw.PrintLine( "c_str0: %s", c_str0);
	hw.PrintLine( "c_str1: %s", c_str1);
}

I’d just use ‘char *’ for C strings. std::string is needless bloatware on Daisy.

1 Like