' Log analyzer: severity counting, pattern extraction, summary stats
#COMPILE EXE
#DIM ALL

FUNCTION IsLogLine(BYVAL line AS STRING) AS LONG
    IF LEN(TRIM$(line)) = 0 THEN
        FUNCTION = 0
        EXIT FUNCTION
    END IF
    IF LEFT$(line, 1) = "#" OR LEFT$(line, 1) = ";" THEN
        FUNCTION = 0
        EXIT FUNCTION
    END IF
    FUNCTION = 1
END FUNCTION

SUB AnalyzeLog(BYVAL logText AS STRING)
    LOCAL i AS LONG
    LOCAL startPos AS LONG
    LOCAL lineNum AS LONG
    LOCAL totalLines AS LONG
    LOCAL errCount AS LONG
    LOCAL warnCount AS LONG
    LOCAL infoCount AS LONG
    LOCAL line AS STRING
    LOCAL upper AS STRING
    PRINT "=== Log Analysis ==="
    PRINT
    startPos = 1
    totalLines = 0
    errCount = 0
    warnCount = 0
    infoCount = 0
    FOR i = 1 TO LEN(logText)
        IF MID$(logText, i, 1) = CHR$(10) THEN
            totalLines = totalLines + 1
            line = MID$(logText, startPos, i - startPos)
            IF RIGHT$(line, 1) = CHR$(13) THEN line = LEFT$(line, LEN(line) - 1)
            IF IsLogLine(line) THEN
                upper = UCASE$(line)
                IF INSTR(upper, "ERROR") > 0 OR INSTR(upper, "ERR:") > 0 THEN
                    errCount = errCount + 1
                ELSEIF INSTR(upper, "WARN") > 0 THEN
                    warnCount = warnCount + 1
                ELSEIF INSTR(upper, "INFO") > 0 THEN
                    infoCount = infoCount + 1
                END IF
            END IF
            startPos = i + 1
        END IF
    NEXT i
    IF startPos <= LEN(logText) THEN totalLines = totalLines + 1
    PRINT "Total lines: " & FORMAT$(totalLines)
    PRINT "ERROR count: " & FORMAT$(errCount)
    PRINT "WARNING count: " & FORMAT$(warnCount)
    PRINT "INFO count: " & FORMAT$(infoCount)
    IF totalLines > 0 THEN
        PRINT "Error rate: " & FORMAT$(errCount / totalLines * 100, "#0.0") & "%"
    END IF
    PRINT
END SUB

SUB PrintLogEntries(BYVAL logText AS STRING)
    LOCAL i AS LONG
    LOCAL startPos AS LONG
    LOCAL lineNum AS LONG
    LOCAL line AS STRING
    LOCAL upper AS STRING
    startPos = 1
    lineNum = 0
    FOR i = 1 TO LEN(logText)
        IF MID$(logText, i, 1) = CHR$(10) THEN
            lineNum = lineNum + 1
            line = MID$(logText, startPos, i - startPos)
            IF RIGHT$(line, 1) = CHR$(13) THEN line = LEFT$(line, LEN(line) - 1)
            IF IsLogLine(line) THEN
                upper = UCASE$(line)
                IF INSTR(upper, "ERROR") > 0 THEN
                    PRINT "  [ERROR]  " & LEFT$(line, 60)
                ELSEIF INSTR(upper, "WARN") > 0 THEN
                    PRINT "  [WARN]   " & LEFT$(line, 60)
                ELSE
                    PRINT "  [INFO]   " & LEFT$(line, 60)
                END IF
            END IF
            startPos = i + 1
        END IF
    NEXT i
END SUB

SUB MakeSampleLog(BYREF log AS STRING)
    LOCAL crlf AS STRING
    crlf = CHR$(13) & CHR$(10)
    log = "INFO  Application started" & crlf
    log = log & "INFO  Loading configuration" & crlf
    log = log & "WARN  Config file not found, using defaults" & crlf
    log = log & "INFO  Connecting to database" & crlf
    log = log & "ERROR Connection timeout after 30 seconds" & crlf
    log = log & "INFO  Retrying connection" & crlf
    log = log & "ERROR Database connection refused" & crlf
    log = log & "WARN  Falling back to local cache" & crlf
    log = log & "INFO  Request completed" & crlf
    log = log & "ERROR Failed to write output file" & crlf
    log = log & "INFO  Shutting down" & crlf
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL logText AS STRING
    PRINT "=== Log Analyzer ==="
    PRINT
    CALL MakeSampleLog(logText)
    PRINT "Sample log:"
    PRINT logText
    CALL AnalyzeLog(logText)
    PRINT "Categorized entries:"
    CALL PrintLogEntries(logText)
    PRINT
    PRINT "Log analyzer tests complete."
    FUNCTION = 0
END FUNCTION
