[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Request to abstract write operations to stdout and stderr in jitter
From: |
Mohammad-Reza Nabipoor |
Subject: |
Re: Request to abstract write operations to stdout and stderr in jitter |
Date: |
Thu, 12 Nov 2020 11:07:50 +0330 |
Hi, Luca.
On Wed, Nov 11, 2020 at 11:42:07PM +0100, Luca Saiu wrote:
> On 2020-11-08 at 19:02 +0100, Luca Saiu wrote:
>
> > Following the IRC discussions with you and with José, I will implement a
> > tentative API very soon; I hope tomorrow. You are right, this is a
> > problem worth solving.
> >
> > If I do not answer within a few days feel free to write me.
>
> I pushed a definition of the new data structures, plus two predefined
> print context kinds, to the new "print-context" branch:
>
> http://git.ageinghacker.net/jitter/log/?h=print-context
>
Thanks.
> The new functionality is not yet used from the rest of the code base,
> but it will soon. I have tested it with dirty code which is not worth
> committing to git. Example:
>
> jitter_print_context cx = jitter_print_context_make_file_star (stdout);
> jitter_print_char_star (cx, "===========\n");
> jitter_print_pointer (cx, main);
> jitter_print_char_star (cx, "\n===========\n");
> jitter_print_int (cx, 10, 0); jitter_print_char_star (cx, "\n");
> jitter_print_begin_class (cx, "interesting");
> jitter_print_int (cx, 2, 14); jitter_print_char_star (cx, "\n");
> jitter_print_end_class (cx);
> jitter_print_int (cx, 10, 1234567); jitter_print_char_star (cx, "\n");
> jitter_print_ulong_long (cx, 10, 1234567); jitter_print_char_star (cx,
> "\n");
> jitter_print_long_long (cx, 10, -123456789012345LL); jitter_print_char_star
> (cx, "\n");
> jitter_print_destroy_context (cx);
>
> I welcome comments.
>
What about a callback printer:
```c
jitter_print_context ctx = jitter_print_context_make_callback (clbk, clbkdata);
```
Every time jitter wants to print something, the callback `clbk` gets called.
For example, after calling `jitter_print_int (ctx, 10, 0)`, the `clbk` will
be called like this:
```c
clbk(JITTER_PRINT_TOKEN_INT, "10", clbkdata);
```
One possible implementation of `clbk` (defined by user of jitter):
```c
void
clbk(enum jitter_print_token tok, const char* str, void* clbkdata)
{
struct buf *buf = (struct buffer *)clbkdata;
switch (tok)
{
case JITTER_PRINT_TOKEN_INT:
bufappend (buf, "(integer %s) ", str);
break;
case JITTER_PRINT_TOKEN_INDENT: /* whitespace prefix used for indentation */
bufappend (buf, "(indentation-whitespace %s) ", str);
break;
default:
bufappend (buf, "(misc %s) ", str);
}
}
```
Tokens can be defined like this in jitter:
```c
enum jitter_printer_token
{
JITTER_PRINTER_TOKEN_,INDENT, /* indentation whitespace */
JITTER_PRINTER_TOKEN_INT,
JITTER_PRINTER_TOKEN_STR,
JITTER_PRINTER_TOKEN_INST, /* instruction */
JITTER_PRINTER_TOKEN_ULONG,
JITTER_PRINTER_TOKEN_LONG,
JITTER_PRINTER_TOKEN_ULONGLONG,
JITTER_PRINTER_TOKEN_LONGLONG,
/* ... */
};
```
This way the user of jitter have full control over the printing.
And I think all other `printer`s can be implemented by this generic interface.
They only need one function.
e.g.,
```c
FILE* file = fopen (/* ... */);
int fd = open (/* ... */);
jitter_print_context ctxfile = jitter_print_context_make_callback (
jitter_print_file_star_printer, file);
jitter_print_context ctxfd = jitter_print_context_make_callback (
jitter_print_file_desc_printer, fd);
```
The `jitter_print_file_star_printer` and `jitter_print_file_desc_printer`
functions are defined by jitter.
If you use this generic callback, you can drop the `_callback` suffix of
`jitter_print_context_make_callback` to make the name shorter and simpler.
I think Jose will be much happier with this API.
Jose! WDYT?
Regards,
Mohammad-Reza