all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to automatically write *Messages" buffer to a file?
@ 2004-11-24  3:43 Brad Collins
  0 siblings, 0 replies; 14+ messages in thread
From: Brad Collins @ 2004-11-24  3:43 UTC (permalink / raw)



Is there is a simple way of getting Emacs to automatically append
messages in the message buffer to a file so messages work as a log file?

I've been looking around and it seems this should be dead easy but I
can't figure it out?

b/

-- 
Brad Collins <brad@chenla.org>, Bangkok, Thailand

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

* Re: How to automatically write *Messages" buffer to a file?
       [not found] <mailman.1150.1101268518.27204.help-gnu-emacs@gnu.org>
@ 2004-11-24 14:06 ` Pascal Bourguignon
  2004-11-24 15:52   ` Eli Zaretskii
  2004-12-01 19:08 ` Kevin Rodgers
  1 sibling, 1 reply; 14+ messages in thread
From: Pascal Bourguignon @ 2004-11-24 14:06 UTC (permalink / raw)


Brad Collins <brad@chenla.org> writes:

> Is there is a simple way of getting Emacs to automatically append
> messages in the message buffer to a file so messages work as a log file?
> 
> I've been looking around and it seems this should be dead easy but I
> can't figure it out?

The best I can figure is that the simple way to do it would be to
modify the C source of emacs.  message is a buit-in function.  I know
of no way from emacs lisp to append a line to a file.  If you advised
the message function, you'd have to load the log file, append the
lines to the buffer and save back the buffer.  In addition, messages
coming from the other built-in functions would not be logged...

<ad>By the way, there's Hemlock (and PortableHemlock) that is an
emacs-like programmed entirely in Common-Lisp.  In Hemlock to add such
a feature would not have to go back to C, you could do it in Common-Lisp</ad>

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.

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

* Re: How to automatically write *Messages" buffer to a file?
  2004-11-24 14:06 ` How to automatically write *Messages" buffer to a file? Pascal Bourguignon
@ 2004-11-24 15:52   ` Eli Zaretskii
  2004-11-24 19:01     ` Joe Corneli
       [not found]     ` <mailman.1295.1101323448.27204.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 14+ messages in thread
From: Eli Zaretskii @ 2004-11-24 15:52 UTC (permalink / raw)


> From: Pascal Bourguignon <spam@mouse-potato.com>
> Date: 24 Nov 2004 15:06:38 +0100
> 
> Brad Collins <brad@chenla.org> writes:
> 
> > Is there is a simple way of getting Emacs to automatically append
> > messages in the message buffer to a file so messages work as a log file?
> > 
> > I've been looking around and it seems this should be dead easy but I
> > can't figure it out?
> 
> The best I can figure is that the simple way to do it would be to
> modify the C source of emacs.  message is a buit-in function.

How about an after-change function that'd do this only for the
*Messages* buffer?

> I know of no way from emacs lisp to append a line to a file.

??? What's wrong with append-to-file?

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

* Re: How to automatically write *Messages" buffer to a file?
  2004-11-24 15:52   ` Eli Zaretskii
@ 2004-11-24 19:01     ` Joe Corneli
       [not found]     ` <mailman.1295.1101323448.27204.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 14+ messages in thread
From: Joe Corneli @ 2004-11-24 19:01 UTC (permalink / raw)


   > From: Pascal Bourguignon <spam@mouse-potato.com>
   > Date: 24 Nov 2004 15:06:38 +0100
   > 
   > Brad Collins <brad@chenla.org> writes:
   > 
   > > Is there is a simple way of getting Emacs to automatically append
   > > messages in the message buffer to a file so messages work as a log file?
   > > 
   > > I've been looking around and it seems this should be dead easy but I
   > > can't figure it out?
   > 
   > The best I can figure is that the simple way to do it would be to
   > modify the C source of emacs.  message is a buit-in function.

   How about an after-change function that'd do this only for the
   *Messages* buffer?

If you want a faithful account of what is actually in the *Messages*
buffer, that would definitely be better than defadvising `message' --
because a significant amount of stuff that is written to the
*Messages* buffer is not put there by `message'.

   > I know of no way from emacs lisp to append a line to a file.

   ??? What's wrong with append-to-file?

Something fairly minor... namely that it adds text to the *Messages*
buffer (not through the `message' mechanism), so one has to delicately
advise it to shut up (for use in this application).

Here is a little code that *does not work as desired* but I think it
is pretty close.  I'd be happy if someone could point out the bug(s).


(defvar messaging-on t
  "Control whether or not messages will be printed.
By default, they are.")

(defadvice write-region (around no-wrote-file activate)
  "Turn off the printout associated with writing files.
This is necessary to add as a supplement to `nomessage' because
the \"Wrote file\" or \"Added to file\" message is not printed
through the `message' mechanism.  The observed effect of this
piece of advice is that neither `save-buffer' nor `write-file'
will print anything out when they run."
  (if messaging-on
      ad-do-it
    (set-buffer-modified-p nil)
    (ad-set-arg 4 t)
    (ad-set-arg 6 nil)
    ad-do-it))

(defun turn-on-logging-advice ()
  (ad-enable-advice 'write-region 'around 'no-wrote-file)
  (ad-activate 'write-region))

(defun turn-off-logging-advice ()
  (ad-disable-advice 'write-region 'around 'no-wrote-file)
  (ad-activate 'write-region))

(turn-off-logging-advice)

;;

(defun setup-logging ()
  (interactive)
  (message "begin logging")
  (set-buffer "*Messages*")
  (make-variable-buffer-local 'after-change-functions)
  ;; be less destructive if you care
  (setq after-change-functions
         '(log-message))
  (turn-on-logging-advice))

(defun stop-logging ()
  (interactive)
  (message "stop logging")
  ;; see prev comment
  (setq after-change-functions nil)
  (turn-off-logging-advice))

(defun log-message (beg end range)
  (let ((messaging-on nil))
    (append-to-file beg end "~/LOG")))

(defun test-message ()
  (interactive)
  (message "hi"))

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

* Re: How to automatically write *Messages" buffer to a file?
       [not found]     ` <mailman.1295.1101323448.27204.help-gnu-emacs@gnu.org>
@ 2004-11-24 20:16       ` Stefan Monnier
  2004-11-24 20:28         ` Joe Corneli
       [not found]         ` <mailman.1305.1101328729.27204.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 14+ messages in thread
From: Stefan Monnier @ 2004-11-24 20:16 UTC (permalink / raw)


>   (make-variable-buffer-local 'after-change-functions)

Never use `make-variable-buffer-local' on a variable which belongs to some
other package.  You want to use `make-local-variable' instead.

Note that in the above case you don't need either of them because
after-change-functions is already automatically made buffer-local.  And even
if you wanted to make it buffer-local manually, you shouldn't use
make-local-variable but make-local-hook since after-change-functions is
a hook.

>   (setq after-change-functions
>          '(log-message))
[...]
>   (setq after-change-functions nil)

Never use `setq' on a hook.  Use `add-hook' or `remove-hook'.


        Stefan


PS: As for your question, don't use `append-to-file', use `write-region'
instead, and pass `quiet' as the VISIT argument to prevent the message.

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

* Re: How to automatically write *Messages" buffer to a file?
  2004-11-24 20:16       ` Stefan Monnier
@ 2004-11-24 20:28         ` Joe Corneli
       [not found]         ` <mailman.1305.1101328729.27204.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 14+ messages in thread
From: Joe Corneli @ 2004-11-24 20:28 UTC (permalink / raw)


   >   (make-variable-buffer-local 'after-change-functions)

   Never use `make-variable-buffer-local' on a variable which belongs to some
   other package.  You want to use `make-local-variable' instead.

   Note that in the above case you don't need either of them because
   after-change-functions is already automatically made buffer-local.  And even
   if you wanted to make it buffer-local manually, you shouldn't use
   make-local-variable but make-local-hook since after-change-functions is
   a hook.

   >   (setq after-change-functions
   >          '(log-message))
   [...]
   >   (setq after-change-functions nil)

   Never use `setq' on a hook.  Use `add-hook' or `remove-hook'.

I didn't know it was a hook (though I did think to myself that it was
very hook-like).  The variable name is not descriptive.  Maybe it
should be aliased to or renamed `after-change-hook'.

   As for your question, don't use `append-to-file', use `write-region'
   instead, and pass `quiet' as the VISIT argument to prevent the message.

Shouldn't advice to `write-region' be sufficient?  `append-to-file'
just calls this function.  True, using `write-region' is more
transparent (and I had an error in my advice to this function), but I
don't see why advising `write-region' and then calling
`append-to-file' wouldn't work.

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

* Re: How to automatically write *Messages" buffer to a file?
       [not found]         ` <mailman.1305.1101328729.27204.help-gnu-emacs@gnu.org>
@ 2004-11-24 20:55           ` Stefan Monnier
  2004-11-24 21:52             ` Joe Corneli
                               ` (3 more replies)
  2004-11-24 22:00           ` Thien-Thi Nguyen
  1 sibling, 4 replies; 14+ messages in thread
From: Stefan Monnier @ 2004-11-24 20:55 UTC (permalink / raw)


>> (setq after-change-functions nil)

>    Never use `setq' on a hook.  Use `add-hook' or `remove-hook'.

> I didn't know it was a hook (though I did think to myself that it was
> very hook-like).  The variable name is not descriptive.  Maybe it
> should be aliased to or renamed `after-change-hook'.

Normal hooks have names that end in `-hook'.  Special hooks
(e.g. hooks whose functions take arguments, or whose return value is not
ignored) have names that end in `-functions'.  It's all explained in the
elisp manual, of course.

>    As for your question, don't use `append-to-file', use `write-region'
>    instead, and pass `quiet' as the VISIT argument to prevent the message.
> Shouldn't advice to `write-region' be sufficient?

You can't put "advice" and "sufficient" together like that without adding
a smiley somewhere.  `advice' is a sledghammer: it's the last resort before
modifying the source code.

`append-to-file' is only defined so as to make appending possible
interactively.  From elisp you should jsut call `write-region' directly.

> `append-to-file' just calls this function.  True, using `write-region' is
> more transparent (and I had an error in my advice to this function), but
> I don't see why advising `write-region' and then calling `append-to-file'
> wouldn't work.

Advising is a source of major headaches because of unexpected interactions
with other pieces of code which do not expect write-region to
behave differently.  Advice have to be written *extremely* carefully.
I.e. if you can do it without `advice', then don't use `advice'.


        Stefan

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

* Re: How to automatically write *Messages" buffer to a file?
  2004-11-24 20:55           ` Stefan Monnier
@ 2004-11-24 21:52             ` Joe Corneli
  2004-11-24 22:19             ` Drew Adams
                               ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Joe Corneli @ 2004-11-24 21:52 UTC (permalink / raw)



   Advising is a source of major headaches because of unexpected interactions
   with other pieces of code which do not expect write-region to
   behave differently.  Advice have to be written *extremely* carefully.
   I.e. if you can do it without `advice', then don't use `advice'.

OK, that sounds like good advice :).

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

* Re: How to automatically write *Messages" buffer to a file?
       [not found]         ` <mailman.1305.1101328729.27204.help-gnu-emacs@gnu.org>
  2004-11-24 20:55           ` Stefan Monnier
@ 2004-11-24 22:00           ` Thien-Thi Nguyen
  2004-11-25  4:52             ` Joe Corneli
  1 sibling, 1 reply; 14+ messages in thread
From: Thien-Thi Nguyen @ 2004-11-24 22:00 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> writes:

> True, using `write-region' is more transparent (and I had an
> error in my advice to this function), but I don't see why
> advising `write-region' and then calling `append-to-file'
> wouldn't work.

i don't understand what you mean by transparent, here.

thi

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

* RE: How to automatically write *Messages" buffer to a file?
  2004-11-24 20:55           ` Stefan Monnier
  2004-11-24 21:52             ` Joe Corneli
@ 2004-11-24 22:19             ` Drew Adams
  2004-11-25  4:56             ` Joe Corneli
       [not found]             ` <mailman.1370.1101359173.27204.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 14+ messages in thread
From: Drew Adams @ 2004-11-24 22:19 UTC (permalink / raw)


Something that I don't like about advised functions is that I cannot use the
debugger with them (unless I'm missing something). - Drew

   Stefan's "advice" advice:

    `advice' is a sledghammer: it's the last resort before
    modifying the source code.

    Advising is a source of major headaches because of unexpected
    interactions with other pieces of code...
    Advice have to be written *extremely* carefully.
    I.e. if you can do it without `advice', then don't use `advice'.

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

* Re: How to automatically write *Messages" buffer to a file?
  2004-11-24 22:00           ` Thien-Thi Nguyen
@ 2004-11-25  4:52             ` Joe Corneli
  0 siblings, 0 replies; 14+ messages in thread
From: Joe Corneli @ 2004-11-25  4:52 UTC (permalink / raw)


   Joe Corneli <jcorneli@math.utexas.edu> writes:

   > True, using `write-region' is more transparent (and I had an
   > error in my advice to this function), but I don't see why
   > advising `write-region' and then calling `append-to-file'
   > wouldn't work.

   i don't understand what you mean by transparent, here.

 Easy to understand.  
 Simple. 
 Direct.

Since `append-to-file' is just a wrapper for `write-region', using
`write-region' to stand for itself is clearer, hence "more
transparent".  Also the complications of advice can be avoided,
again, making the code clearer.

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

* Re: How to automatically write *Messages" buffer to a file?
  2004-11-24 20:55           ` Stefan Monnier
  2004-11-24 21:52             ` Joe Corneli
  2004-11-24 22:19             ` Drew Adams
@ 2004-11-25  4:56             ` Joe Corneli
       [not found]             ` <mailman.1370.1101359173.27204.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 14+ messages in thread
From: Joe Corneli @ 2004-11-25  4:56 UTC (permalink / raw)


OK, so I tried rewriting the code without advice.

But there are still a number of problems.

First of all, it seems that the arguments being sent to the
`after-change-functions' aren't quite right.  

There may be other problems too.

;; messages to log file

(defun start-logging ()
  (interactive)
  (set-buffer "*Messages*")
  (add-hook 'after-change-functions 'write-to-log)
  (message (concat "begin logging " (current-time-string))))

(defun write-to-log (beg end len) 
  ;; This message reveals that the *Messages* buffer
  ;; and the echo area are joined at the hip.  `beg'
  ;; and `end' are positions in the echo area!
; (message (format "%s %s" (- (point-max) (- end beg)) (point-max)))
  (save-excursion (set-buffer "*Messages*")
                  (write-region 
                   ;; this region doesn't seem to include the
                   ;; final character, but I can't change the
                   ;; way it is written or I get error.
                   ;;
                   ;; the obvious choice 
                   ;;   beg end
                   ;; doesn't work either.
                   (- (point-max) 
                      (- end beg))
                   (point-max)
                   "~/LOG" t 'quiet)))

(defun stop-logging ()
  (interactive)
  (message (concat "stop logging " (current-time-string)))
  (set-buffer "*Messages*")
  (remove-hook 'after-change-functions 'write-to-log))

(defun test-message ()
  (interactive)
  (message "hi"))

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

* Re: How to automatically write *Messages" buffer to a file?
       [not found]             ` <mailman.1370.1101359173.27204.help-gnu-emacs@gnu.org>
@ 2004-11-25 20:20               ` Stefan Monnier
  0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2004-11-25 20:20 UTC (permalink / raw)


>   (add-hook 'after-change-functions 'write-to-log)

Please pass set `local' argument.  It doesn't make a difference, but
it's cleaner.  Same thing for remove-hook.

>                    ;; this region doesn't seem to include the
>                    ;; final character, but I can't change the
>                    ;; way it is written or I get error.
>                    ;;
>                    ;; the obvious choice 
>                    ;;   beg end
>                    ;; doesn't work either.
>                    (- (point-max) 
>                       (- end beg))
>                    (point-max)
>                    "~/LOG" t 'quiet)))

Try M-x report-emacs-bug.


        Stefan

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

* Re: How to automatically write *Messages" buffer to a file?
       [not found] <mailman.1150.1101268518.27204.help-gnu-emacs@gnu.org>
  2004-11-24 14:06 ` How to automatically write *Messages" buffer to a file? Pascal Bourguignon
@ 2004-12-01 19:08 ` Kevin Rodgers
  1 sibling, 0 replies; 14+ messages in thread
From: Kevin Rodgers @ 2004-12-01 19:08 UTC (permalink / raw)


Brad Collins wrote:
 > Is there is a simple way of getting Emacs to automatically append
 > messages in the message buffer to a file so messages work as a log file?

This should get you most of the way there (when you exit emacs, it
should ask you whether you want to save that log file):

(with-current-buffer "*Messages*"
   (set buffer-file-name "~/*Messages*")) ; or set-visited-file-name

-- 
Kevin Rodgers

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

end of thread, other threads:[~2004-12-01 19:08 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1150.1101268518.27204.help-gnu-emacs@gnu.org>
2004-11-24 14:06 ` How to automatically write *Messages" buffer to a file? Pascal Bourguignon
2004-11-24 15:52   ` Eli Zaretskii
2004-11-24 19:01     ` Joe Corneli
     [not found]     ` <mailman.1295.1101323448.27204.help-gnu-emacs@gnu.org>
2004-11-24 20:16       ` Stefan Monnier
2004-11-24 20:28         ` Joe Corneli
     [not found]         ` <mailman.1305.1101328729.27204.help-gnu-emacs@gnu.org>
2004-11-24 20:55           ` Stefan Monnier
2004-11-24 21:52             ` Joe Corneli
2004-11-24 22:19             ` Drew Adams
2004-11-25  4:56             ` Joe Corneli
     [not found]             ` <mailman.1370.1101359173.27204.help-gnu-emacs@gnu.org>
2004-11-25 20:20               ` Stefan Monnier
2004-11-24 22:00           ` Thien-Thi Nguyen
2004-11-25  4:52             ` Joe Corneli
2004-12-01 19:08 ` Kevin Rodgers
2004-11-24  3:43 Brad Collins

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.