zsh-users
 help / color / mirror / code / Atom feed
* How to redirect output without escape sequences to a file
@ 2011-05-08 19:27 Thorsten Kampe
  2011-05-09  1:58 ` Aaron Davies
  2011-05-09 15:01 ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Thorsten Kampe @ 2011-05-08 19:27 UTC (permalink / raw)
  To: zsh-users

Hi,

I've modified my PS4 so it contains a little bit of colour[1]. When I 
redirect the debug output to a file ("&> trace.log") this naturally 
contains the escape sequences literally[2] (which makes the output 
somehow unclear).

Is there a way to either strip the escape sequences when redirected to a 
file or to modify PS4 when stderr is redirected to a file?

Thanks in advance,
Thorsten
[1] PS4='%{\e[1;36m%}+%{\e[m%}%1N[%i]%{\e[1;36m%}:%{\e[m%} '
[2] ^[[1;36m+^[[mslpsnoop.sh[53]^[[1;36m:^[[m print


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to redirect output without escape sequences to a file
  2011-05-08 19:27 How to redirect output without escape sequences to a file Thorsten Kampe
@ 2011-05-09  1:58 ` Aaron Davies
  2011-05-09 15:01 ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Aaron Davies @ 2011-05-09  1:58 UTC (permalink / raw)
  To: zsh-users

On May 8, 2011, at 3:27 PM, Thorsten Kampe wrote:

> I've modified my PS4 so it contains a little bit of colour[1]. When I 
> redirect the debug output to a file ("&> trace.log") this naturally 
> contains the escape sequences literally[2] (which makes the output 
> somehow unclear).
> 
> Is there a way to either strip the escape sequences when redirected to a 
> file or to modify PS4 when stderr is redirected to a file?

not sure about those, but "less -R" (or "less -r") may help--they make less output ANSI color sequences (or all control sequences) literally, instead of with caret notation, so you'd actually see the same colors you do on the console.
-- 
Aaron Davies
aaron.davies@gmail.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to redirect output without escape sequences to a file
  2011-05-08 19:27 How to redirect output without escape sequences to a file Thorsten Kampe
  2011-05-09  1:58 ` Aaron Davies
@ 2011-05-09 15:01 ` Bart Schaefer
  2011-05-12 18:10   ` Thorsten Kampe
  2011-05-16 11:03   ` Thorsten Kampe
  1 sibling, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2011-05-09 15:01 UTC (permalink / raw)
  To: zsh-users

On May 8,  9:27pm, Thorsten Kampe wrote:
} 
} I've modified my PS4 so it contains a little bit of colour[1].

I was going to suggest that you stop using the terminal escape
sequences directly and use the %F prompt escape instead:

    PS4='%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f '

Theoretically, these are only supposed to be output if the terminal
supports them, so I thought that setting TERM=dumb would then turn
off the coloring.  But no ... zsh goes for ANSI sequences if the
terminal definition doesn't specify, so something is always output.

} Is there a way to either strip the escape sequences when redirected to
} a file or to modify PS4 when stderr is redirected to a file?

So, the alternative is to do the stripping.  Instead of

    ... >& trace.log

use 

    ... 2>>(col > trace.log) >&2

Yes, that's somewhat inconvenient.

You could also create a conditional prompt, for example:

    PS4='%(9v.+%1N[%i]:.%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f) '

which says that if $psvar[9] has a value, then print the prompt with
no coloring, otherwise use coloring.  Then when you want to turn off
color for a while, just assign psvar[9]=1, and when you're ready for
color again, delete it with psvar[9]=().  This assumes you're not
already using 9 elements of psvar in other prompts, of course.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to redirect output without escape sequences to a file
  2011-05-09 15:01 ` Bart Schaefer
@ 2011-05-12 18:10   ` Thorsten Kampe
  2011-05-13  6:32     ` Bart Schaefer
  2011-05-16 11:03   ` Thorsten Kampe
  1 sibling, 1 reply; 7+ messages in thread
From: Thorsten Kampe @ 2011-05-12 18:10 UTC (permalink / raw)
  To: zsh-users

* Bart Schaefer (Mon, 09 May 2011 08:01:30 -0700)
> On May 8,  9:27pm, Thorsten Kampe wrote:
> } I've modified my PS4 so it contains a little bit of colour[1].
> 
> I was going to suggest that you stop using the terminal escape
> sequences directly and use the %F prompt escape instead:
> 
>     PS4='%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f '

I did that and this looks much more readable now. Unfortunately on a 
machine of mine with an older zsh (4.2.6) this displays as {cyan}.

Whould it make sense to go back to the (older?) "autoload -U colors; 
colors"/"fg_bold[cyan]" invocation on this machine?

Thorsten


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to redirect output without escape sequences to a file
  2011-05-12 18:10   ` Thorsten Kampe
@ 2011-05-13  6:32     ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2011-05-13  6:32 UTC (permalink / raw)
  To: zsh-users

On May 12,  8:10pm, Thorsten Kampe wrote:
}
} >     PS4='%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f '
} 
} I did that and this looks much more readable now. Unfortunately on a 
} machine of mine with an older zsh (4.2.6) this displays as {cyan}.
} 
} Whould it make sense to go back to the (older?) "autoload -U colors; 
} colors"/"fg_bold[cyan]" invocation on this machine?

I'd say yes but I'm a bit biased (having written "colors") ...

I'd use %{$fg[cyan]%}%B+%{$reset_color%} (no need to %b, reset clears
the whole palette).  Or %b%{$fg[default]%} if you prefer symmetry.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to redirect output without escape sequences to a file
  2011-05-09 15:01 ` Bart Schaefer
  2011-05-12 18:10   ` Thorsten Kampe
@ 2011-05-16 11:03   ` Thorsten Kampe
  2011-05-16 21:36     ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Thorsten Kampe @ 2011-05-16 11:03 UTC (permalink / raw)
  To: zsh-users

* Bart Schaefer (Mon, 09 May 2011 08:01:30 -0700)
> On May 8,  9:27pm, Thorsten Kampe wrote:
> } I've modified my PS4 so it contains a little bit of colour[1].
> 
> I was going to suggest that you stop using the terminal escape
> sequences directly and use the %F prompt escape instead:
> 
>     PS4='%F{cyan}%B+%b%f%1N[%i]%F{cyan}%B:%b%f '
> 
> Theoretically, these are only supposed to be output if the terminal
> supports them, so I thought that setting TERM=dumb would then turn
> off the coloring.  But no ... zsh goes for ANSI sequences if the
> terminal definition doesn't specify, so something is always output.
> 
> } Is there a way to either strip the escape sequences when redirected to
> } a file or to modify PS4 when stderr is redirected to a file?
> 
> So, the alternative is to do the stripping.  Instead of
> 
>     ... >& trace.log
> 
> use 
> 
>     ... 2>>(col > trace.log) >&2
> 
> Yes, that's somewhat inconvenient.

...and somehow not working (form me). I still got (stripped) color stuff 
in the output file ("1m33m-0m39m service:directory-agent" for instance).

"| sed 's/\x1b\[[0-9]\{1,2\}m//g'" worked for me.

How would the command line look like if wanted to pipe stdout and stderr 
to sed, and then redirect both to trace.log?

Thorsten


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to redirect output without escape sequences to a file
  2011-05-16 11:03   ` Thorsten Kampe
@ 2011-05-16 21:36     ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2011-05-16 21:36 UTC (permalink / raw)
  To: zsh-users

On May 16,  1:03pm, Thorsten Kampe wrote:
}
} >     ... 2>>(col > trace.log) >&2
} 
} ...and somehow not working (form me). I still got (stripped) color stuff 
} in the output file ("1m33m-0m39m service:directory-agent" for instance).

Hmm, must depend on what "col" is able to determine are escape sequences.
 
} "| sed 's/\x1b\[[0-9]\{1,2\}m//g'" worked for me.
} 
} How would the command line look like if wanted to pipe stdout and stderr 
} to sed, and then redirect both to trace.log?

    { ... } |& sed 's/\x1b\[[0-9]\{1,2\}m//g' > trace.log

where "..." is your original command.  If you want to pipe ONLY stderr
to sed, and then redirect both to trace.log, you'd need something more
along the lines of

    { { ... } 2>>( sed 's/\x1b\[[0-9]\{1,2\}m//g' >&2 ) } >&trace.log

However in that case "sed" runs in the background so you're not
guaranteed to get the same interleaving of output in trace.log as if
you join stdout+stderr together sooner.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-05-16 21:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-08 19:27 How to redirect output without escape sequences to a file Thorsten Kampe
2011-05-09  1:58 ` Aaron Davies
2011-05-09 15:01 ` Bart Schaefer
2011-05-12 18:10   ` Thorsten Kampe
2011-05-13  6:32     ` Bart Schaefer
2011-05-16 11:03   ` Thorsten Kampe
2011-05-16 21:36     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).