Subject: Re: bug#79253: Add timestamps to *Messages*

From: Eli Zaretskii <[email-masked]>
Date: Sun, 17 Aug 2025 10:11:57 +0300

severity 79253 wishlist
thanks

> Date: Sat, 16 Aug 2025 23:01:50 +0200
> Cc: [email-masked]
> From: "R. Diez" <[email-masked]>
> 
> 
> >> Could you implement an easy way to add timestamps to the *Messages* buffer?
> > 
> > Did you try using set-message-function to install your function as the
> > one that adds a time stamp to the text of the message to be shown?  If
> > you tried, and it didn't work, please tell what didn't work.  If you
> > didn't try, please do.
> 
> First of all, thanks for the hint. The 'message' function does not mention 'set-message-function' or the like, so I missed it.
> 
> I could find very little information about 'set-message-function', and no examples whatsoever, so I came up with this:
> 
> (defun my-message-function (msg)
>    "" ; No docstring yet.
>    (concat "Test: " msg))
> 
> (setq set-message-function #'my-message-function)
> 
> The effect is that messages in the minibuffer do get the "Test:" prefix, but messages in the *Messages* buffer do not.
> 
> But that is exactly the opposite of what I am trying to achieve!

OK, thanks for testing.

One problem with your request is that Emacs many times generates
messages by multiple calls to the 'message' function.  Prepending the
current time to each message passed to 'message' will thus produce
somewhat sub-optimal, maybe even ugly, results.  For example, if you
type C-p at the first line of a buffer, Emacs signals an error that
logs in the *Messages* buffer as

  previous-line: Beginning of buffer

But if we prepend each logged message with current time, it will
instead look like this:

  17/08/2025 00:09:44 previous-line
  17/08/2025 00:09:44 :
  17/08/2025 00:09:44 Beginning of buffer

This happens because in this case we call 'message' 3 times, with the
following 3 strings:

  "previous-line"
  ": "
  "Beginning of buffer"

It also means that the usual Emacs feature of compacting *Messages* by
combining duplicate messages will not work, because each of those
duplicate lines will now have different time stamps prepended to it,
and also potentially broken into several lines.

If this is acceptable, we can implement the feature you are asking
for.  Otherwise, I don't see how this could be implemented, except by
having each Lisp program that calls 'message' inject the time string
where appropriate.

What do people think about the acceptability of the above?
Do people have other ideas for implementing this?