[Gambas-user] Logging errors in apps running as Application.Daemon

Tobias Boege taboege at ...626...
Wed Jun 21 13:51:11 CEST 2017


On Wed, 21 Jun 2017, adamnt42 at ...626... wrote:
> On Wed, 21 Jun 2017 02:17:27 -0700 (MST)
> alexchernoff <alexchernoff at ...67...> wrote:
> 
> > Peace to all,
> > 
> > I have some Gb projects that are in Application.Daemon mode. But sometimes
> > if process dies for whatever reason, I can't see what the last error was. I
> > have to run it in a console without daemonizing and wait for it to die and
> > show last reason (like "#13: Null object" or something).
> > 
> > Can this be logged to a file? So at least the last output the interpreter
> > spits out is logged...
> > 
> > Cheers!
> > 
> > 
> > 
> > 
> > 
> or now that I think of it, you could just:
> 
> $ yourdeamonstarter > output.txt 2>&1
> 
> :-)
> 
> (Oh where might I have seen that somewhere before ???)
> B
> 

I doubt this works, because Application.Daemon = True calls daemon(3) which
redirects stdout and stderr (which were previously set by the shell to your
log files) to /dev/null, so you won't find anything in your files.

Output To and Error To should still work because they don't replace stdout
and stderr (which are killed by daemon()) but change the default file the
Print and Error statements operate on, at the Gambas level.

I don't know if it's possible to capture interpreter errors this way though.
Generally the interpreter would use fprintf(stderr, ...) to report errors
(fixed on stderr which, again, is sent to /dev/null after daemon()).

Attached are two scripts which showcase these reflections. Both scripts have
three stages: (1) print something, (2) daemonise and print something, and
(3) raise an error and then print something. daemon1.gbs3 uses normal Print
and relies on the shell redirection:

  $ ./daemon1.gbs3
  a
  $ ./daemon1.gbs3 >/tmp/log 2>&1
  $ cat /tmp/log

Of course, only the output after the first stage is shown as stdout is
closed after daemonising. The interpreter error is not shown. Curiously
the shell redirection results in an empty file, so not even the first
stage output arrives(?)

Whereas daemon2 creates its own log files (one for stdout and stderr each)
and uses Output To and Error To:

  $ ./daemon2.gbs3
  $ cat /tmp/log*
  a
  b

It can at least store its output after the daemonisation (the "b"), but
the interpreter error is not logged. To get this, you could look into
using the global error handler Static Public Sub Application_Error() [1]
in your startup class or using the C library via Extern to force your
log files into the standard fds again (this latter one would be the
least intrusive for the code you may have already written).

Regards,
Tobi

[1] http://gambaswiki.org/wiki/comp/gb/application

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list