I'd like to redirect gnuplot error messages (that are normally displayed in the gnuplot terminal) to a file for logging. Is there any way to do this?
Example: if I type
gnuplot> Hi!
in the gnuplot terminal, Then I get
gnuplot> Hi!
^
invalid command
gnuplot>
Is there any way to redirect "^\ninvalid command" into another file, e.g. err.txt?
Context: I'm using gnuplot embedded in a c++ application using gnuplot-iostream by Dan Stahlke. It works great! But I have no idea how to get error messages from this pipe, so this would be a good work-around.
I don't know exactly if this applies on your c++ application (probably not), but I thought I'd mention anyway. If you do:
[user#server]$ gnuplot 2> err.txt
gnuplot> Hi!
gnuplot> exit
[user#server]$ cat err.txt
G N U P L O T
Version 5.0 patchlevel 1 last modified 2015-06-07
Copyright (C) 1986-1993, 1998, 2004, 2007-2015
Thomas Williams, Colin Kelley and many others
gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help FAQ"
immediate help: type "help" (plot window: hit 'h')
Terminal type set to 'aqua'
^
invalid command
Maybe you could incorporate something like this. Hope it helps!
I don't think one can achieve this internally within Gnuplot without tinkering with the source code.
The message "invalid command" is produced in command.c by calling the function int_error (defined in util.c) within which is stderr specified explicitly...
It seems gnuplot redirects by default both stdout and stderr to stderr.
Instead, terminal file output is send to stdout. Here is an example to check. The content of tmp.gnu.
print 'some text';
set terminal postscript;
set out
pl sin(x)
gnuplot tmp.gnu 2>tmp.txt 1>sin.ps
tmp.txt contains the stdout and sin.ps the postscript file.
Check also this answer https://stackoverflow.com/a/27375957/11769765 to use
set print "-", set print "/dev/fd/2" or set print "filename" to redirect stdout.
Related
I fount a strange phenomenon. If we run the following gnuplot script (in a folder with gnuplot-lua-tikz-common.tex, gnuplot-lua-tikz.sty, gnuplot-lua-tikz.tex, t-gnuplot-lua-tikz.tex)
tikzfile="test.tex"
plot x**2
set term tikz standalone monochrome
set output tikzfile #
replot #
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)
we found the following fatal error
! Emergency stop.
<*> test.tex
! ==> Fatal error occurred, no output PDF file produced!
Anyway we have the test.tex file. Hence if we re-run the same script with the #-marked lines commented we obtain no error and the perfect test.pdf file.
During the first exec, with set term we have an empty file, with replot we fill it, but until the end of the exec we can't use it as an input of pdflatex. Why?
During the second exec we already have the test.tex file, so if we comment set term and replot we can use it as an input of pdflatex. Why?
Thank you.
Gnuplot doesn't automatically flush and finish an output file after a plot.
So, if you want to further process an output file from within the gnuplot script you must explicitly close the file with set output beforehand:
tikzfile="test.tex"
set term tikz standalone
set output tikzfile
plot x**2
set output
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)
How to programmatically get the name of current gnuplot script? I know that I can call gnuplot script from bash and get it file name but I am wondering if it is possible from inside gnuplot. My goal is to make something like:
date=system("date +%F_%T | sed 's/:/-/g'")
my_name=$0 # THIS IS HOW TO DO IT IN BASH
set term png
set output my_name.date.".png"
I've tried:
my_name=system("cat /proc/$$/cmdline")
but it returned sh instead of script name
Not quite an answer to your question, but this might help with what you want to do:
You can leave my_name unset in the script, and set it either inside gnuplot, just before you load the script (where you need to know the script name anyway):
my_name=...
load(my_name)
or set it when you invoke gnuplot from the shell:
$ gnuplot -e "my_name=${FILE}" ${FILE}
A few more things:
date=system("date +%F_%T | sed 's/:/-/g'")
can be replaced with
date=system("date +%F_%H-%M-%S")
(which is shorter and doesn't need to be parsed through sed) or without any forking at all:
date=strftime("%F_%H-%M-%S",time(0.0))
Using gnuplot version 5 you have access to the file called with load via the variable ARG0
Consider the script test.gp which contains only
print ARG0
Now, calling this with
gnuplot -e "load 'test.gp'"
prints you test.gp on the screen. With earlier versions you don't have access to a similar variable (also not when using call). For earlier versions you must stick to one of the solutions given by #chw21
following up from the question on SO about command line parameters to gnuplot scripts, and its corresponding answer, I see that I can make a script myscript.gp like
if(!exist("parameters")) parameters=""
plot "< myprog ".parameters u 1:2
and call it with
$ gnuplot -e "parameters='-m 5'" myscript.gp
However, I could achieve a similar result when running in interactive mode with myscript2.gp as
parameters="$0"
plot "< myprog ".parameters u 1:2
and calling it from the interactive prompt as
gnuplot> call "myscript2.gp" "-m 5"
And now the question. As I see it those two methods are not connected and the $O would fail when when running in batch, whereas the 'parameters' is not updated when using call.
How do I set a script which could be called from the shell command line AS WELL AS from the gnuplot interactive command line? Another way to ask it, is it possible to test in which conditions was the script called?
Thanks,
Playing around, I found a quite inelegant solution, but that could help to achieve what I want.
parameters="" # sets default value
if ("$#" eq sprintf("%d",1)) parameters="$0" # if using call with 1 parameter: $#=1
if (exist("bpar")) parameters=bpar # checking for batch call
plot "< myprog ".parameters u 1:2
which can then be called by either
$ gnuplot -e "bpar='-m 5'" myscript.gp
or
gnuplot> call "myscript2.gp" "-m 5"
When I use the following in my script, gnuplot will print test to stderr:
print "test"
However, I want to write test to stdout as only errors should be written to stderr.
As stated in the gnuplot docs (type help print in interactive gnuplot console):
The output file can be set with set print.
So, let's look up set print:
Without "<filename>", the output file is restored to <STDERR>. The
<filename> "-" means <STDOUT>.
So simply add a set print "-" at the top of your gnuplot script and everything printed by the print-statement is written to stdout.
I know I can plot with data from stdin by using '-' as the data source, but is there any way I can plot data output from a command the same way? I.e., instead of running the command and piping to the gnuplot script, can I specify the command in the gnuplot script itself?
Something like this (but this doesn't work):
set terminal pngcairo
set output 'test.png'
cmd = "`./mycmd`" # running ./mycmd in terminal gives my data to stdout.
# the command can be several other commands, piped together.
# i'm only interested in whatever comes to stdout after running
# the entire thing.
plot cmd u 2:3 w lines # etc...
The above makes cmd contain a single long line with all the lines of output smashed together.
Yes, you can:
plot '< ./mycmd'