#!/bin/sh
log "Starting F08-logging..."
log "======================="

#=============================================
# 8) setup default logfile size
#---------------------------------------------

# min default log size: 1MB
Y2LOG_DEFAULT_MIN_SIZE=1000

# the max default log size depends on the environment
# NOTE: y2-core defines 10MB default in installed system
if [ -z "$Y2DEBUG" ]; then
    # max default log size: 20MB
    Y2LOG_DEFAULT_MAX_SIZE=20000
else
    # with Y2DEBUG allow bigger log: 40MB
    Y2LOG_DEFAULT_MAX_SIZE=40000
fi

#=============================================
# 8.1) setup logfile size as 1/4 of FreeRam
#      but not more/less than the limits
#---------------------------------------------

if [ -z "$Y2MAXLOGSIZE" ]; then
    USE=$(awk '/^MemFree:/{ n=2 ; printf "%d\n", $n/4 }' /proc/meminfo)
    log "\tComputed default log size: $USE kB"
    # more than the minimum default
    if [ "$USE" -gt "$Y2LOG_DEFAULT_MIN_SIZE" ]; then
        # more than the maximum default
        if [ "$USE" -gt "$Y2LOG_DEFAULT_MAX_SIZE" ]; then
	        export Y2MAXLOGSIZE=$Y2LOG_DEFAULT_MAX_SIZE
        else
	        export Y2MAXLOGSIZE=$USE
        fi
    else
        export Y2MAXLOGSIZE=$Y2LOG_DEFAULT_MIN_SIZE
    fi
fi

test -z "$Y2MAXLOGNUM" && export Y2MAXLOGNUM=5

# fate#302166: store y2debug messages and log them on crash
export Y2DEBUGONCRASH=1

#=============================================
# 8.2) report used logfile size and lognum
#---------------------------------------------
log "\tSet YaST2 LOG parameters:"
log "\tMaximum log size:  $Y2MAXLOGSIZE kB"
log "\tMaximum log count: $Y2MAXLOGNUM"

#=============================================
# 8.3) Start sampling memory usage data
#---------------------------------------------
log "\tStart sampling memory usage data:"
for WORD in ${Cmdline-}; do
    case $WORD in MEMSAMPLE=*) MEMSAMPLE=${WORD#MEMSAMPLE=};; esac
done
if [ "$MEMSAMPLE" = 0 ]; then
    log "\tdisabled"
else
    memsample --sleep="${MEMSAMPLE-5}" --archive=/var/log/YaST2/memsample.zcat &
    log "\tPID: $!"
fi
