Csound Csound-dev Csound-tekno Search About

Re: API - message callback

Date2007-07-31 22:22
FromVictor Lazzarini
SubjectRe: API - message callback
I'm just eliminating the '\n' characters, because
PD's post() adds them and I don't want two newlines
after every msg string. It's just a formatting issue,
you don't need to do it if you are
just using printf() or fprintf() etc.

>
> i understand using that using vsnprintf would allow me to
> get it to   work .. few questions though:
>
>
> if i == 0 and csmess[i] == '\0'
>     wouldn't  x->csmess[i - 1] = ' '  be illegal?
>
>
> I haven't analyzed the stream .. but is it just that 0's
> are being   written into it and this just replaces the
> character before it with a   space?  Why are some strings
> formatted correctly in the first place?    Why is any of
> this buffer manipulation necessary in the first place?
>
> greg
>
>
> On Jul 31, 2007, at 2:48 PM, Victor Lazzarini wrote:
>
> > Use vsnprintf(...).
> >
> > This is an example callback from csoundapi_tilde.c:
> >
> > static void message_callback(CSOUND *csound,
> >                    int attr, const char *format,va_list
> >      valist){ int i;
> >      t_csoundapi *x = (t_csoundapi *)
> > csoundGetHostData(csound);
> >
> >      if(x->csmess != NULL)
> >      vsnprintf(x->csmess, MAXMESSTRING, format, valist);
> >      for(i=0;i >        if(x->csmess != NULL && x->csmess[i] == '\0'){
> >          x->csmess[i-1]= ' ';
> >          break;
> >        }
> >     if(x->csmess != NULL && x->messon) post(x->csmess);
> >   }
> >
> >
> >>
> >>
> >> I'm using the message callback because I want to pass
> the >> message   elsewhere (file, network, etc..):
> >>
> >> Here is my function:
> >>
> >> void csoundMessageHandler(CSOUND *csound, int attr,
> const >> char   *format, va_list valist) {
> >>     char messageData[256];
> >>      vsprintf(messageData, format, valist);
> >>      va_end(valist);
> >>
> >>     printf("messageData: %s\n", messageData);
> >> }
> >>
> >> Here is how I register it:
> >>      csoundSetMessageCallback(csound,
> >> csoundMessageHandler);
> >>
> >>
> >> Here is some of the output:
> >> messageData: rtaudio: PortAudio module enabled ...
> >> messageData: using callback interface
> >>
> >> messageData: rtmidi: PortMIDI module enabled
> >>
> >> messageData: orch compiler:
> >>
> >> messageData: 17 lines read
> >>
> >> messageData:
> >> messageData: instr
> >> messageData: 1
> >> messageData:
> >>
> >> messageData: error:
> >> messageData: input arg 'aOsc' used before defined
> >> messageData: , line 15:
> >>
> >> messageData:
> >> messageData: o
> >> messageData: u
> >> messageData: t
> >> messageData: s
> >> messageData:
> >> messageData: a
> >> messageData: O
> >> messageData: s
> >> messageData: c
> >> messageData: ,
> >> messageData:
> >> messageData: a
> >> messageData: O
> >> messageData: s
> >> messageData: c
> >> messageData:
> >>
> >> messageData: error:
> >> messageData: input arg 'aOsc' used before defined
> >> messageData: , line 15:
> >>
> >> messageData:
> >> messageData: o
> >> messageData: u
> >> messageData: t
> >> messageData: s
> >> messageData:
> >> messageData: a
> >> messageData: O
> >> messageData: s
> >> messageData: c
> >> messageData: ,
> >> messageData:
> >> messageData: a
> >> messageData: O
> >> messageData: s
> >> messageData: c
> >> messageData:
> >>
> >> messageData: 2 syntax errors in orchestra.  compilation
> >> invalid messageData:
> >>
> >>
> >> It only seems to happen on errors (which are the most
> >> important   messages).   Is there a way to avoid these
> >> newlines so it can be   readable?
> >>
> >> thanks
> >> greg
> >>
> >>
> > --
> > Send bugs reports to this list.
> > To unsubscribe, send email to
> csound-unsubscribe@lists.bath.ac.uk >
>
> --
> Send bugs reports to this list.
> To unsubscribe, send email to
> csound-unsubscribe@lists.bath.ac.uk

Date2007-08-01 13:41
FromGreg Thompson
SubjectRe: API - message callback
Well - I should have known.  I found the culprit.  Its calling the callback for every character .. only doing this for syntax errors though - so special code must be written to handle this case .. of course the message handler doesn't know what it is going to receive so .. special code must always be used.  It seems to me this synterr function should construct a string and then call the message handler once.


void synterr(CSOUND *csound, const char *s, ...)
{
    va_list args;
    char    *cp;
    int     c;

    csound->MessageS(csound, CSOUNDMSG_ERROR, Str("error:  "));
    va_start(args, s);
    csound->MessageV(csound, CSOUNDMSG_ERROR, s, args);
    va_end(args);


    /* FIXME - Removed temporarily for debugging
     * This function may not be necessary at all in the end if some of this is
     * done in the parser
     */
    if (ST(linadr) != NULL && (cp = ST(linadr)[ST(curline)]) != NULL
#if defined(ENABLE_NEW_PARSER)
        && !csound->oparms->newParser
#endif
    ) {
      csound->MessageS(csound, CSOUNDMSG_ERROR,
                               Str(", line %d:\n"), ST(curline));
      do {
        csound->MessageS(csound, CSOUNDMSG_ERROR, "%c", (c = *cp++));
      } while (c != '\n');
    }
    else {
      csound->MessageS(csound, CSOUNDMSG_ERROR, "\n");
    }
    csound->synterrcnt++;
}


greg

On Jul 31, 2007, at 5:22 PM, Victor Lazzarini wrote:

I'm just eliminating the '\n' characters, because
PD's post() adds them and I don't want two newlines
after every msg string. It's just a formatting issue,
you don't need to do it if you are
just using printf() or fprintf() etc.


i understand using that using vsnprintf would allow me to
get it to   work .. few questions though:


if i == 0 and csmess[i] == '\0'
    wouldn't  x->csmess[i - 1] = ' '  be illegal?


I haven't analyzed the stream .. but is it just that 0's
are being   written into it and this just replaces the
character before it with a   space?  Why are some strings
formatted correctly in the first place?    Why is any of
this buffer manipulation necessary in the first place?

greg


On Jul 31, 2007, at 2:48 PM, Victor Lazzarini wrote:

Use vsnprintf(...).

This is an example callback from csoundapi_tilde.c:

static void message_callback(CSOUND *csound,
                   int attr, const char *format,va_list
     valist){ int i;
     t_csoundapi *x = (t_csoundapi *)
csoundGetHostData(csound);

     if(x->csmess != NULL)
     vsnprintf(x->csmess, MAXMESSTRING, format, valist);
     for(i=0;i<MAXMESSTRING;i++)
       if(x->csmess != NULL && x->csmess[i] == '\0'){
         x->csmess[i-1]= ' ';
         break;
       }
    if(x->csmess != NULL && x->messon) post(x->csmess);
  }




I'm using the message callback because I want to pass
the >> message   elsewhere (file, network, etc..):

Here is my function:

void csoundMessageHandler(CSOUND *csound, int attr,
const >> char   *format, va_list valist) {
    char messageData[256];
     vsprintf(messageData, format, valist);
     va_end(valist);

    printf("messageData: %s\n", messageData);
}

Here is how I register it:
     csoundSetMessageCallback(csound,
csoundMessageHandler);


Here is some of the output:
messageData: rtaudio: PortAudio module enabled ...
messageData: using callback interface

messageData: rtmidi: PortMIDI module enabled

messageData: orch compiler:

messageData: 17 lines read

messageData:
messageData: instr
messageData: 1
messageData:

messageData: error:
messageData: input arg 'aOsc' used before defined
messageData: , line 15:

messageData:
messageData: o
messageData: u
messageData: t
messageData: s
messageData:
messageData: a
messageData: O
messageData: s
messageData: c
messageData: ,
messageData:
messageData: a
messageData: O
messageData: s
messageData: c
messageData:

messageData: error:
messageData: input arg 'aOsc' used before defined
messageData: , line 15:

messageData:
messageData: o
messageData: u
messageData: t
messageData: s
messageData:
messageData: a
messageData: O
messageData: s
messageData: c
messageData: ,
messageData:
messageData: a
messageData: O
messageData: s
messageData: c
messageData:

messageData: 2 syntax errors in orchestra.  compilation
invalid messageData:


It only seems to happen on errors (which are the most
important   messages).   Is there a way to avoid these
newlines so it can be   readable?

thanks
greg


--
Send bugs reports to this list.
To unsubscribe, send email to

--
Send bugs reports to this list.
To unsubscribe, send email to
-- 
Send bugs reports to this list.
To unsubscribe, send email to csound-unsubscribe@lists.bath.ac.uk