From: sandy on
Hello,

I'm trying to embed tcl interepreter in my c code and wanted to
redirect stdout and stderr to
different files. stdout works fine but stderr (errlog.txt) is always
empty( even though tcl script
contain errors). I am trying this first time, please suggest.

---- Here is the code ---

main (int argc, char *argv[])
{
Tcl_Interp *interp;
int code, temp;

interp = Tcl_CreateInterp();

Tcl_Channel stdconsoleChannel;
stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
"w+", 777);
Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);

Tcl_Channel errconsoleChannel;
errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
"w+", 777);
Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);

code = Tcl_EvalFile(interp, argv[1]);

Tcl_DeleteInterp(interp);

exit(0);
}

From: Alexandre Ferrieux on
On Jul 3, 5:56 am, sandy <motif_u...(a)yahoo.com> wrote:
> Hello,
>
> I'm trying to embed tcl interepreter in my c code and wanted to
> redirect stdout and stderr to
> different files. stdout works fine but stderr (errlog.txt) is always
> empty( even though tcl script
> contain errors). I am trying this first time, please suggest.
>
> ---- Here is the code ---
>
> main (int argc, char *argv[])
> {
> Tcl_Interp *interp;
> int code, temp;
>
> interp = Tcl_CreateInterp();
>
> Tcl_Channel stdconsoleChannel;
> stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
> "w+", 777);
> Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);
>
> Tcl_Channel errconsoleChannel;
> errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
> "w+", 777);
> Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);
>
> code = Tcl_EvalFile(interp, argv[1]);
>
> Tcl_DeleteInterp(interp);
>
> exit(0);
>
> }

The fact that the script has errors doesn't imply immediate output to
stderr. Instead, Tcl_EvalFile will return TCL_ERROR, the interp's
result containing the error message, and it is up to the caller to
provide an outer "last chance handler".

So, my bet is that Tcl_SetStdChannel(TCL_STDERR) works fine, and you
can verify this prediction by adding into your script:

puts stderr Hey!
flush stderr

before any error.
Once this is verified, either write the outer handler at the C level
as suggested above, or in Tcl by wrapping your script in a [catch].

By the way, tclsh and wish both provide excellent outer handlers.
Are you sure you really want to embed, instead of extending Tcl ?
Please describe your situation.

-Alex
From: sandy on
On Jul 3, 1:24 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Jul 3, 5:56 am, sandy <motif_u...(a)yahoo.com> wrote:
>
>
>
>
>
> > Hello,
>
> > I'm trying to embed tcl interepreter in my c code and wanted to
> > redirect stdout and stderr to
> > different files. stdout works fine but stderr (errlog.txt) is always
> > empty( even though tcl script
> > contain errors). I am trying this first time, please suggest.
>
> > ---- Here is the code ---
>
> > main (int argc, char *argv[])
> > {
> >         Tcl_Interp *interp;
> >         int code, temp;
>
> >         interp = Tcl_CreateInterp();
>
> >         Tcl_Channel stdconsoleChannel;
> >         stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
> > "w+", 777);
> >         Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);
>
> >         Tcl_Channel errconsoleChannel;
> >         errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
> > "w+", 777);
> >         Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);
>
> >         code = Tcl_EvalFile(interp, argv[1]);
>
> >         Tcl_DeleteInterp(interp);
>
> >         exit(0);
>
> > }
>
> The fact that the script has errors doesn't imply immediate output to
> stderr. Instead, Tcl_EvalFile will return TCL_ERROR, the interp's
> result containing the error message, and it is up to the caller to
> provide an outer "last chance handler".
>
> So, my bet is that Tcl_SetStdChannel(TCL_STDERR) works fine, and you
> can verify this prediction by adding into your script:
>
>     puts stderr Hey!
>     flush stderr
>
> before any error.
> Once this is verified, either write the outer handler at the C level
> as suggested above, or in Tcl by wrapping your script in a [catch].
>
> By the way, tclsh and wish both provide excellent outer handlers.
> Are you sure you really want to embed, instead of extending Tcl ?
> Please describe your situation.
>
> -Alex- Hide quoted text -
>
> - Show quoted text -


Thanks Alex,

Just, one more question.

In my application I want to send device commands through serial port.

Suppose I am sending device commands through tcl loop, how can i make
tcl loop executes only as fast as my serial device can handle it? What
I have observed is that tcl loop finished much faster then the device
and data gets lost.

Thanks,
sandy



From: Alexandre Ferrieux on
On Jul 4, 11:47 am, sandy <motif_u...(a)yahoo.com> wrote:
> On Jul 3, 1:24 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> wrote:
>
>
>
> > On Jul 3, 5:56 am, sandy <motif_u...(a)yahoo.com> wrote:
>
> > > Hello,
>
> > > I'm trying to embed tcl interepreter in my c code and wanted to
> > > redirect stdout and stderr to
> > > different files. stdout works fine but stderr (errlog.txt) is always
> > > empty( even though tcl script
> > > contain errors). I am trying this first time, please suggest.
>
> > > ---- Here is the code ---
>
> > > main (int argc, char *argv[])
> > > {
> > > Tcl_Interp *interp;
> > > int code, temp;
>
> > > interp = Tcl_CreateInterp();
>
> > > Tcl_Channel stdconsoleChannel;
> > > stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
> > > "w+", 777);
> > > Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);
>
> > > Tcl_Channel errconsoleChannel;
> > > errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
> > > "w+", 777);
> > > Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);
>
> > > code = Tcl_EvalFile(interp, argv[1]);
>
> > > Tcl_DeleteInterp(interp);
>
> > > exit(0);
>
> > > }
>
> > The fact that the script has errors doesn't imply immediate output to
> > stderr. Instead, Tcl_EvalFile will return TCL_ERROR, the interp's
> > result containing the error message, and it is up to the caller to
> > provide an outer "last chance handler".
>
> > So, my bet is that Tcl_SetStdChannel(TCL_STDERR) works fine, and you
> > can verify this prediction by adding into your script:
>
> > puts stderr Hey!
> > flush stderr
>
> > before any error.
> > Once this is verified, either write the outer handler at the C level
> > as suggested above, or in Tcl by wrapping your script in a [catch].
>
> > By the way, tclsh and wish both provide excellent outer handlers.
> > Are you sure you really want to embed, instead of extending Tcl ?
> > Please describe your situation.
>
> > -Alex- Hide quoted text -
>
> > - Show quoted text -
>
> Thanks Alex,
>
> Just, one more question.
>
> In my application I want to send device commands through serial port.
>
> Suppose I am sending device commands through tcl loop, how can i make
> tcl loop executes only as fast as my serial device can handle it? What
> I have observed is that tcl loop finished much faster then the device
> and data gets lost.

The problem is not with the event loop, it has to do with low-level
flow control being enabled or not. When you [puts] to a channel in
Tcl, sonner or later a write() syscall will occur. For a socket, pipe,
or file, you have a stream semantics, in that no data is lost: the
write is done either in blocking or nonblocking mode, but in the
latter case data not immediately writte are retried later.

For a serial port, if flow control is enabled, it should work the
same. But maybe this requires the physical device on the other side to
cooperate...

Now you didn't answer *my* question ;-)

-Alex
From: sandy on
On Jul 4, 3:00 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Jul 4, 11:47 am, sandy <motif_u...(a)yahoo.com> wrote:
>
>
>
>
>
> > On Jul 3, 1:24 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> > wrote:
>
> > > On Jul 3, 5:56 am, sandy <motif_u...(a)yahoo.com> wrote:
>
> > > > Hello,
>
> > > > I'm trying to embed tcl interepreter in my c code and wanted to
> > > > redirect stdout and stderr to
> > > > different files. stdout works fine but stderr (errlog.txt) is always
> > > > empty( even though tcl script
> > > > contain errors). I am trying this first time, please suggest.
>
> > > > ---- Here is the code ---
>
> > > > main (int argc, char *argv[])
> > > > {
> > > >         Tcl_Interp *interp;
> > > >         int code, temp;
>
> > > >         interp = Tcl_CreateInterp();
>
> > > >         Tcl_Channel stdconsoleChannel;
> > > >         stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
> > > > "w+", 777);
> > > >         Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);
>
> > > >         Tcl_Channel errconsoleChannel;
> > > >         errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
> > > > "w+", 777);
> > > >         Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);
>
> > > >         code = Tcl_EvalFile(interp, argv[1]);
>
> > > >         Tcl_DeleteInterp(interp);
>
> > > >         exit(0);
>
> > > > }
>
> > > The fact that the script has errors doesn't imply immediate output to
> > > stderr. Instead, Tcl_EvalFile will return TCL_ERROR, the interp's
> > > result containing the error message, and it is up to the caller to
> > > provide an outer "last chance handler".
>
> > > So, my bet is that Tcl_SetStdChannel(TCL_STDERR) works fine, and you
> > > can verify this prediction by adding into your script:
>
> > >     puts stderr Hey!
> > >     flush stderr
>
> > > before any error.
> > > Once this is verified, either write the outer handler at the C level
> > > as suggested above, or in Tcl by wrapping your script in a [catch].
>
> > > By the way, tclsh and wish both provide excellent outer handlers.
> > > Are you sure you really want to embed, instead of extending Tcl ?
> > > Please describe your situation.
>
> > > -Alex- Hide quoted text -
>
> > > - Show quoted text -
>
> > Thanks Alex,
>
> > Just, one more question.
>
> > In my application I want to send device commands through serial port.
>
> > Suppose I am sending device commands through tcl loop, how can i make
> > tcl loop executes only as fast as my serial device can handle it? What
> > I have observed is that tcl loop finished much faster then the device
> > and data gets lost.
>
> The problem is not with the event loop, it has to do with low-level
> flow control being enabled or not. When you [puts] to a channel in
> Tcl, sonner or later a write() syscall will occur. For a socket, pipe,
> or file, you have a stream semantics, in that no data is lost: the
> write is done either in blocking or nonblocking mode, but in the
> latter case data not immediately writte are retried later.
>
> For a serial port, if flow control is enabled, it should work the
> same. But maybe this requires the physical device on the other side to
> cooperate...
>
> Now you didn't answer *my* question ;-)
>
> -Alex- Hide quoted text -
>
> - Show quoted text -

thanks again Alex, In my situations i am trying to provide tcl support
to a MFC application, hence thinking of embedding and not extending. I
hope i am not wrong ;-)

Regards,
Sandy