' Log level macros: ERROR/WARN/INFO/DEBUG with #IF filtering
#COMPILE EXE
#DIM ALL

%LOG_LEVEL_ERROR = 0
%LOG_LEVEL_WARN = 1
%LOG_LEVEL_INFO = 2
%LOG_LEVEL_DEBUG = 3

%CURRENT_LOG_LEVEL = 2

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL numEntries  AS LONG
    LOCAL level  AS LONG
    LOCAL lineNum  AS LONG
    LOCAL logMessage(20) AS STRING
    LOCAL logLevel(20) AS LONG
    LOCAL logLine(20) AS LONG
    LOCAL logTag AS STRING
    LOCAL s AS STRING

    PRINT "=== Logging Macros ==="
    PRINT
    PRINT "Current log level: " & STR$(%CURRENT_LOG_LEVEL) & " (0=ERROR, 1=WARN, 2=INFO, 3=DEBUG)"
    PRINT

    numEntries = 10
    logLevel(0) = 0
    logMessage(0) = "File not found: config.ini                       "
    logLine(0) = 42

    logLevel(1) = 1
    logMessage(1) = "Deprecated API call detected                     "
    logLine(1) = 87

    logLevel(2) = 2
    logMessage(2) = "Server started on port 8080                      "
    logLine(2) = 120

    logLevel(3) = 3
    logMessage(3) = "Cache miss for key 'user:123'                    "
    logLine(3) = 201

    logLevel(4) = 2
    logMessage(4) = "Connection established to database               "
    logLine(4) = 156

    logLevel(5) = 0
    logMessage(5) = "Out of memory: allocation failed                 "
    logLine(5) = 67

    logLevel(6) = 3
    logMessage(6) = "Entering function parseConfig()                  "
    logLine(6) = 33

    logLevel(7) = 1
    logMessage(7) = "Disk usage above 85% threshold                   "
    logLine(7) = 298

    logLevel(8) = 3
    logMessage(8) = "Variable 'count' = 42 at iteration 7            "
    logLine(8) = 175

    logLevel(9) = 2
    logMessage(9) = "Shutdown signal received, cleaning up            "
    logLine(9) = 312

    PRINT "--- Log Output (filtered by level " & FORMAT$(%CURRENT_LOG_LEVEL) & ") ---"
    PRINT

    FOR i = 0 TO numEntries - 1
        IF logLevel(i) <= %CURRENT_LOG_LEVEL THEN
            level = logLevel(i)
            IF level = 0 THEN logTag = "[ERROR]"
            IF level = 1 THEN logTag = "[WARN] "
            IF level = 2 THEN logTag = "[INFO] "
            IF level = 3 THEN logTag = "[DEBUG]"
            s = TRIM$(logMessage(i))
            PRINT "  " & logTag & " " & s & " (line " & FORMAT$(logLine(i)) & ")"
        END IF
    NEXT i

    PRINT
    PRINT "--- Suppressed Entries ---"
    FOR i = 0 TO numEntries - 1
        IF logLevel(i) > %CURRENT_LOG_LEVEL THEN
            level = logLevel(i)
            IF level = 3 THEN logTag = "DEBUG"
            s = TRIM$(logMessage(i))
            PRINT "  [SUPPRESSED] " & logTag & ": " & s
        END IF
    NEXT i

    PRINT
    PRINT "--- Level Change Simulation ---"
    PRINT "If level = 3 (DEBUG), " & STR$(numEntries) & " entries shown"
    PRINT "If level = 0 (ERROR), 2 entries shown"
    PRINT "If level = 1 (WARN),  4 entries shown"
    PRINT "If level = 2 (INFO),  7 entries shown (current)"

    PRINT
    PRINT "--- Log Format ---"
    PRINT "  [LEVEL] timestamp file:line - message"
    PRINT "  Example: [ERROR] 2026-06-03 12:00:00 main.pb:42 - msg"

    PRINT
    PRINT "Logging macros demonstration complete."

    FUNCTION = 0
END FUNCTION
