[Top][All Lists]

[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


> 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:

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:

clbk(JITTER_PRINT_TOKEN_INT, "10", clbkdata);

One possible implementation of `clbk` (defined by user of jitter):

clbk(enum jitter_print_token tok, const char* str, void* clbkdata)
  struct buf *buf = (struct buffer *)clbkdata;

  switch (tok)
      bufappend (buf, "(integer %s) ", str);
    case JITTER_PRINT_TOKEN_INDENT: /* whitespace prefix used for indentation */
      bufappend (buf, "(indentation-whitespace %s) ", str);
      bufappend (buf, "(misc %s) ", str);

Tokens can be defined like this in jitter:

enum jitter_printer_token
  JITTER_PRINTER_TOKEN_,INDENT, /* indentation whitespace */
  JITTER_PRINTER_TOKEN_INST, /* instruction */
  /* ... */

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.


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?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]