wuffs icon indicating copy to clipboard operation
wuffs copied to clipboard

Debug prints in wuffs code?

Open mvdan opened this issue 6 years ago • 2 comments

I'm trying to play with wuffs, and something I miss from coding in Go or other languages is the ability to add debug prints. Something akin to Go's println would be great, for example.

I realise that I can do this via the generated C code, but that's not as straightforward. And, once wuffs adds support for other languages, debugging will be different depending on what language you're generating.

Any ideas or suggestions?

Also, as a drop-by question - how is one supposed to use the wuffs tool for little wuffs packages not part of wuffs/std? I haven't figured how to get wuffs gen to work with that, so I'm just using wuffs-c. I presume that's the best I can do at the moment.

mvdan avatar Mar 24 '18 17:03 mvdan

I can see the appeal of having a built-in println function or statement, but that's tricky. For example, Wuffs-the-language has not needed a string type so far (other than for error messages), or varargs, whether variant in number or in type, especially as every call argument is currently explicitly named: it's foo(a:x, b:y) not just foo(x, y).

Instead, I've been adding printfs to the generated C code. It's crude, but it suffices. That's why, in gen/c/std/*.c, there's

// Uncomment this #include for printf-debugging.
// #include <stdio.h>

If you're using the wuffs command line tool, such as wuffs test, be aware of flags like -ccompilers, -focus and -skipgen. Run wuffs test -? for some brief documentation.


As for packages outside of std, I definitely want to support that one day, but it hasn't been the highest priority thing to work on yet.

You can run wuffs-c directly, or you can fork wuffs and add your own code under std. Maybe a symlink would work, if you're just playing around and don't care yet about version control, I haven't tried. Yeah, the workflow isn't ideal. Sorry.

nigeltao avatar Mar 31 '18 02:03 nigeltao

For example, Wuffs-the-language has not needed a string type so far (other than for error messages), or varargs

That's a good point. I'm afraid I don't have a good, specific proposal of what I would add to the language, then. Varargs wouldn't be necessary, but "empty interface" and string types would still be necessary for a println func to be useful.

Is there any way that we could allow printfs in wuffs, as opposed to a print/println? For example, forgetting the syntax for a moment, suppose I could do:

//wuffs:c printf("length = %d", length);

That is, telling the wuffs-c generator to include a line in the C output at this position. Then, if I were generating/testing Go, I'd instead write something like //wuffs:go fmt.Println("length", length).

I realise including this in the language as a formal feature can get ugly; it doesn't need to be guaranteed to work forever, much like Go's print/println. And I also imagine that the include dance with printf in C might get tricky. But it seems to me like a possible way to introduce debug prints without having to worry about Wuff's syntax and language features, nor with modifying generated code by hand.

A different version of this idea is a println; //wuffs:println("length =", length), which would get translated to printf, fmt.Println, or whatever the language equivalent is. Narrower and perhaps simpler to implement reliably, but also not as powerful as "insert any C statement here".

If you're using the wuffs command line tool, such as wuffs test

If only I could without hacks :)

Yeah, the workflow isn't ideal. Sorry.

Oh, that is fine - wuffs-c is not hard to use. I think a "hello world" example using it, complete with how to build a working binary, would be very useful though. It took me a good half hour to figure that out, when the solution wasn't really complex. I just had to dig through the codebase, which is a bit unnecessary. I'll open an issue about it.

mvdan avatar Mar 31 '18 11:03 mvdan