Here's a bug fix.  I didn't update some constant names in the *Stop Logging*
 script.

On Tuesday, May 16, 2023 at 6:31:04 PM UTC-4 Thomas Passin wrote:

> Today I wanted to continue some work I started a few weeks ago.  I 
> remember clearly what I did.  But I can't remember the outline, and I have 
> not succeeded in finding it even with the help of FileLocatorPro (a 
> terrific Windows program).  Somehow, the Recent Files list hasn't helped 
> either.  Other times I've been able to find the outline but not easily.
>
> I wondered if it would be useful to have Leo keep track of what I was 
> working on when.  Admittedly this situation doesn't happen often, but I 
> thought I'd try it out anyway.  Maybe it would be the start of something 
> more useful.  Or maybe I'll feel like I had a nanny and stop using it.
>
> It's still preliminary.  I have a script to start logging, and another to 
> stop it.  It writes to ~/.leo/temp (and adjusts that path for WIndows).  
> They could easily be turned into global commands in myLeoSettings.leo, 
> though I haven't done that yet.
>
> The script registers as an idle time handler.  If a minute has elapsed 
> since the last check, the script looks to see if the currently focused 
> outline and node have changed since the last check.  If so, it writes the 
> time and the node's UNL to the log file.
>
> Why the UNL? Because it shows the outline and node in a fairly readable 
> way, and because the UNL can by copied and used to open the outline, or 
> otherwise used by some other Leo script.
>
> Obviously a real logging framework could be used to get better file 
> management, but for exploring this seems good enough.
>
> To use, open the attached outline, select the *Start Logging node*, and 
> run it with CTRL-b.  To stop logging without closing Leo, select the *Stop 
> Logging* node and run it with <CTRL-b>
>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/8e34d49f-8cdc-4a20-a2ba-dff57951d9can%40googlegroups.com.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: https://leo-editor.github.io/leo-editor/leo_toc.html -->
<leo_file xmlns:leo="http://leo-editor.github.io/leo-editor/namespaces/leo-python-editor/1.1"; >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="tom.20230516105336.1"><vh>Activity Log</vh>
<v t="tom.20230516111939.1"><vh>Start Logging</vh>
<v t="tom.20230516105648.1"><vh>activity_log_proc()</vh></v>
</v>
<v t="tom.20230516223849.1"><vh>Stop Logging</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="tom.20230516105336.1"></t>
<t tx="tom.20230516105648.1">def activity_log_proc(tag, keywords):
    """If enough time has elapsed, the active node's unl.""" 
    global ud
    if not ud.get(FLAG):  # Must have been stopped already
        return

    c0 = keywords.get('c') # Commander of window being called

    # Is this commander the selected one?
    frame = c0.frame
    master = getattr(frame.top, 'leo_master', None)
    selected_window = master.currentWidget()
    cmdr = selected_window.leo_c

    if not cmdr.mFileName == c0.mFileName:
        return

    last = ud.get(LAST_TIME, None)
    now = time.time()
    if not last:
        ud[last] = now  # Seed the time register
        return

    if now - last &lt; INTERVAL:
        return

    p0 = c0.p
    unl = p0.get_UNL()
    last_unl = ud.get(LAST_UNL, '')
    if last_unl == unl: # No change, need to log
        return

    localtime = time.strftime("%Y-%m-%d %H:%M", time.localtime(now))
    msg = f'{localtime}  {unl}\n'
    # print(f'---- logging: {p0.h}')
    with open(ACTUALPATH, 'a', encoding = 'utf-8') as log:
        log.write(msg)

    ud[LAST_TIME] = now
    ud[LAST_UNL] = unl

</t>
<t tx="tom.20230516111939.1">"""Periodically write UNL of selected node to a log file."""

import time

LOG = '~/.leo/temp/leolog.txt'
FLAG = 'activity-logging'
PROC = 'activity_log_proc'
LAST_TIME = 'activity-last-time'
LAST_UNL = 'activity-last-unl'
INTERVAL = 1 * 60  # seconds

normed = g.os_path_normpath(LOG)
ACTUALPATH = g.finalize(normed)

ud = g.user_dict

@others

# Set up to store data globally
ud[FLAG] = True
now = time.time()
ud[LAST_TIME] = now - INTERVAL - 1

ud[PROC] = activity_log_proc
g.registerHandler('idle', activity_log_proc)
</t>
<t tx="tom.20230516223849.1">"""Stop Activity Logging."""
FLAG = 'activity-logging'
PROC = 'activity_log_proc'
LAST_TIME = 'activity-last-time'
LAST_UNL = 'activity-last-unl'

ud = g.user_dict
proc = ud.get(PROC, None)
if proc:
    g.unregisterHandler('idle', proc)
    g.es('Stopped activity logging')
    for k in (PROC, FLAG, LAST_TIME, LAST_UNL):
        if k in ud:
            del(ud[k])
</t>
</tnodes>
</leo_file>

Reply via email to