' debug_logger2.pb — Logging system with levels
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL logLevels(200) AS LONG
    LOCAL logMsgIDs(200) AS LONG
    LOCAL logCount AS LONG
    LOCAL maxLogs AS LONG
    LOCAL currentLevel AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL msgID AS LONG
    LOCAL logLevel AS LONG
    LOCAL levelName AS STRING
    LOCAL iter AS LONG
    LOCAL numEntries AS LONG
    LOCAL errorCount AS LONG
    LOCAL warnCount AS LONG
    LOCAL infoCount AS LONG
    LOCAL debugCount AS LONG
    LOCAL filterLevel AS LONG
    LOCAL displayLevel AS LONG
    LOCAL levelStr AS STRING
    LOCAL msgStr AS STRING

    maxLogs = 50
    currentLevel = 1
    filterLevel = 2

    PRINT "=== Logger v2: Multi-Level ==="
    PRINT "Filter level: " + STR$(filterLevel)

    FOR numEntries = 1 TO 25
        IF numEntries MOD 4 = 0 THEN logLevel = 0
        IF numEntries MOD 4 = 1 THEN logLevel = 1
        IF numEntries MOD 4 = 2 THEN logLevel = 2
        IF numEntries MOD 4 = 3 THEN logLevel = 3

        IF numEntries = 1 THEN msgID = 1
        IF numEntries = 2 THEN msgID = 2
        IF numEntries = 3 THEN msgID = 3
        IF numEntries = 4 THEN msgID = 4
        IF numEntries = 5 THEN msgID = 5
        IF numEntries = 6 THEN msgID = 6
        IF numEntries = 7 THEN msgID = 7
        IF numEntries = 8 THEN msgID = 8
        IF numEntries = 9 THEN msgID = 9
        IF numEntries = 10 THEN msgID = 10
        IF numEntries > 10 THEN msgID = 11

        IF logLevel = 0 THEN
            levelName = "ERROR"
            errorCount = errorCount + 1
        END IF
        IF logLevel = 1 THEN
            levelName = "WARN"
            warnCount = warnCount + 1
        END IF
        IF logLevel = 2 THEN
            levelName = "INFO"
            infoCount = infoCount + 1
        END IF
        IF logLevel = 3 THEN
            levelName = "DEBUG"
            debugCount = debugCount + 1
        END IF

        logLevels(logCount) = logLevel
        logMsgIDs(logCount) = msgID
        logCount = logCount + 1
    NEXT numEntries

    PRINT "--- Log Output (level >= " + STR$(filterLevel) + ") ---"
    FOR i = 0 TO logCount - 1
        displayLevel = logLevels(i)
        msgID = logMsgIDs(i)
        IF displayLevel >= filterLevel THEN
            IF displayLevel = 0 THEN levelStr = "ERROR"
            IF displayLevel = 1 THEN levelStr = "WARN"
            IF displayLevel = 2 THEN levelStr = "INFO"
            IF displayLevel = 3 THEN levelStr = "DEBUG"

            IF msgID = 1 THEN msgStr = "Application started"
            IF msgID = 2 THEN msgStr = "Configuration loaded"
            IF msgID = 3 THEN msgStr = "Memory pool initialized"
            IF msgID = 4 THEN msgStr = "Disk space low"
            IF msgID = 5 THEN msgStr = "Network timeout detected"
            IF msgID = 6 THEN msgStr = "User logged in: admin"
            IF msgID = 7 THEN msgStr = "Cache hit ratio: 87%"
            IF msgID = 8 THEN msgStr = "Database connection pool full"
            IF msgID = 9 THEN msgStr = "Processing request #" + STR$(i)
            IF msgID = 10 THEN msgStr = "Shutdown signal received"
            IF msgID = 11 THEN msgStr = "Heartbeat: OK"

            PRINT "  [" + levelStr + "] " + msgStr
        END IF
    NEXT i

    PRINT "--- Stats ---"
    PRINT "ERROR: " + STR$(errorCount) + " WARN: " + STR$(warnCount)
    PRINT "INFO: " + STR$(infoCount) + " DEBUG: " + STR$(debugCount)
    PRINT "Total entries: " + STR$(logCount)
    PRINT "=== End Logger2 ==="
    FUNCTION = 0
END FUNCTION
