26

I am trying to debug an Emacs program performance wise. In particular, I suffer an extremely long startup time (~5' compared to ~1' for a bare Emacs) on a remote connection via WLAN, cellphone etc. In this context, any message written is no help, for the display is not refreshed at all.

What I would like to do is to write onto the "standard output" of the Linux process. I am aware of the --batch mode but this is no help to me because I want to use Emacs interactively.

So how can I write messages out to the Linux-standard output (as opposed to the Emacs standard output)?

3 Answers 3

28
+350

You can output to standard error like this:

(print "hello world" #'external-debugging-output)

or

(princ "hello world" #'external-debugging-output)

This can buffer, so be careful.It's not possible to output to standard out at the moment. I'm going to add that, I think!

3
  • 1
    Be praised, and RMS with You!
    – false
    Aug 25, 2016 at 20:32
  • 3
    I actually wrote a patch to add this, before I discovered that function. It's been there for years!
    – Phil Lord
    Aug 25, 2016 at 21:33
  • 2
    Woa, didn't know that this existed 😳 Thanks for sharing it; I've removed my answer.
    – user355252
    Sep 1, 2016 at 15:24
7

Start emacs as a daemon (emacs --daemon) and any messages during the start-up sequence will be sent to stdout or stderr, as described by lunaryorn.

Connect to the server with emacsclient

The simplest way to kill the server is M-x kill-emacs RET

For details see C-hig (emacs) Emacs Server RET

7

Works for me in centos 6.8 (GNU Emacs 23.1.1):

(append-to-file "here I come to save the day\n" nil "/dev/stdout")

Try also using "/dev/tty" in place of "/dev/stdout":

Unclear from question if you intend to redirect "emacs -nw" stdout to a file and monitor that file externally (then use "/dev/stdout"); or are ok with writing to "/dev/tty" thus polluting the self-same tty of the main "emacs -nw" display.

If starting a GUI version of emacs, in such a way it may lose attachment to originating tty, can abuse environment variables to communicate an originating shell's tty to elisp.

This works for me using Aquamacs in Mac OS X. Launching from a bash shell:

$ MY_TTY=$(tty) open /Applications/Aquamacs\ Emacs.app &

then in emacs:

(append-to-file "here I come to save the day\n" nil (getenv "MY_TTY"))
1
  • This works but affects the minibuffer which can be a disadvantage at times.
    – Phil Lord
    Jan 10, 2017 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.