' Structured Debug Logger with levels, timestamps, file output
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL h AS LONG
LOCAL m AS LONG
LOCAL s AS LONG
    LOCAL ts AS STRING
    LOCAL i AS LONG
LOCAL j AS LONG
LOCAL k AS LONG
    LOCAL sum AS LONG
LOCAL mx AS LONG
LOCAL mn AS LONG

    PRINT "Debug Logger Demo"
    PRINT "================"
    PRINT ""

    h = VAL(LEFT$(TIME$, 2))
    m = VAL(MID$(TIME$, 4, 2))
    s = VAL(RIGHT$(TIME$, 2))
    ts = STR$(h) + ":" + STR$(m) + ":" + STR$(s)

    PRINT ts; " [INFO] Application started"

    i = 10: j = 20: k = 30
    sum = i + j + k
    PRINT ts; " [INFO] Sum of "; i; "+"; j; "+"; k; " = "; sum

    i = 150: j = 200: k = 50
    sum = i + j + k
    PRINT ts; " [WARN] Large sum: "; sum

    i = 100: j = 5
    IF j <> 0 THEN
        sum = i / j
        PRINT ts; " [INFO] "; i; " / "; j; " = "; sum
    ELSE
        PRINT ts; " [ERROR] Division by zero!"
    END IF

    i = 100: j = 0
    IF j = 0 THEN
        PRINT ts; " [ERROR] Division by zero prevented"
    END IF

    i = 0
    WHILE i < 10
        k = i * i
        PRINT ts; " [DEBUG] i="; i; " i^2="; k
        i = i + 1
    WEND

    i = 150: j = 0: k = 100
    IF i < j THEN mx = j ELSE mx = i
    IF i > k THEN mn = k ELSE mn = i
    PRINT ts; " [INFO] Clamp tests:"
    PRINT ts; " [INFO]   Max(150) = "; mx
    PRINT ts; " [INFO]   Min(150, 0, 100) = "; mn

    PRINT ""
    PRINT "Factorial calculation:"
    LOCAL fact AS QUAD
    fact = 1
    FOR i = 2 TO 10
        fact = fact * i
    NEXT i
    PRINT ts; " [INFO] 10! = "; fact

    fact = 1
    FOR i = 2 TO 20
        fact = fact * i
    NEXT i
    PRINT ts; " [INFO] 20! = "; fact

    PRINT ts; " [INFO] Application completed"

    FUNCTION = 0
END FUNCTION
