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

FUNCTION IsLogLine(BYVAL outLine AS STRING) AS LONG
    IF LEN(TRIM$(outLine)) = 0 THEN
        FUNCTION = 0
        EXIT FUNCTION
    END IF
    IF LEFT$(outLine, 1) = "#" OR LEFT$(outLine, 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 outLine 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
            outLine = MID$(logText, startPos, i - startPos)
            IF RIGHT$(outLine, 1) = CHR$(13) THEN outLine = LEFT$(outLine, LEN(outLine) - 1)
            IF IsLogLine(outLine) THEN
                upper = UCASE$(outLine)
                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: " & STR$(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) & "%"
    END IF
    PRINT
END SUB

SUB PrintLogEntries(BYVAL logText AS STRING)
    LOCAL i AS LONG
    LOCAL startPos AS LONG
    LOCAL lineNum AS LONG
    LOCAL outLine 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
            outLine = MID$(logText, startPos, i - startPos)
            IF RIGHT$(outLine, 1) = CHR$(13) THEN outLine = LEFT$(outLine, LEN(outLine) - 1)
            IF IsLogLine(outLine) THEN
                upper = UCASE$(outLine)
                IF INSTR(upper, "ERROR") > 0 THEN
                    PRINT "  [ERROR]  " & LEFT$(outLine, 60)
                ELSEIF INSTR(upper, "WARN") > 0 THEN
                    PRINT "  [WARN]   " & LEFT$(outLine, 60)
                ELSE
                    PRINT "  [INFO]   " & LEFT$(outLine, 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
