# Daily-Session-Log 
#
# Session log with automatic daily log file rotation.
# Assumes a connection (of any kind) is already open,
# e.g. to the control / logging port of a PBX, router, etc.
# Note: in C-Kermit 8.0.211 you can use INPUT /NOMATCH rather
# than specifying a "string that will never come".
#
# F. da Cruz, Columbia University, June 2004
#
# Parameters for this run, change as needed.
# HINT: Use forward slash (/) in Windows pathnames.

.current := c:/logs/                      # Directory to run in
.archive := c:/archive/                   # Directory to move old logs to
set session-log text                      # Log in text mode or...
set session-log timestamped-text          # Do this if you want timesamps added
set input echo on                         # If you also want display on screen

# Local variables and macros

local logname                             # Name of log file
local mode                                # Mode in which to open it

def ON_CTRLC if open session-log close session-log # Ctrl-C trap
def ERRCHK if fail stop 1 "TROUBLE: \%1 - \v(errstring)" # Error handler

# Check directories and CD

if not directory \m(current) stop 1 "TROUBLE: \m(current) is not a directory"
if not directory \m(archive) stop 1 "TROUBLE: \m(archive) is not a directory"

cd \m(current)                            # CD to desired directory

# Loop to open session log, log to it until just before midnight, then
# close the log and move it to the archive directory.

while true {                              # Loop until interrupted
    if open session-log close session-log # If a log is open close it
    .logname := \v(ndate).log             # Get name for new log
    .mode := new                          # Mode in which to open it
    if exist \m(logname) .mode := append  # If file exists append to it
    log session \m(logname) \m(mode)      # Open the log file
    errchk "Open Log \m(logname)"
    if eq \m(mode) new {                  # Heading only if new file
        writeln session-log \v(day) \v(date) \v(time)
    }
    # Record everything that arrives until just before midnight.

    input 23:59:59 STRING_THAT_WILL_NEVER_COME
    if success stop 1 "TROUBLE: Log \m(logname) terminated early"
    if not open connection stop 1 "TROUBLE: Connection lost"

    # At midnight close the log and rotate it, taking care not to
    # overwrite any existing files.  Assuming a reliable flow-controlled
    # connection, no data will be lost during log rotation.

    close session-log
    errchk "Close Log \m(logname)"
    if not exist \m(archive)\m(logname) {
        rename \m(logname) \m(archive)
        errchk "Rename Log \m(logname) to \m(archive)"
    }
    sleep 2                               # Sleep until after midnight
}