file-handling · intermediate · ~30 min

Final Project: log-aggregator — count by level

Per-line classification and aggregate counting in one pass.

Challenge

Implement void log_count_levels(const char *log, int *info, int *warn, int *err). The log is a multi-line buffer where each line begins with INFO , WARN , or ERROR (or starts otherwise — ignore those lines). Sets the three output counters to the per-level totals.

Why this matters

Log aggregation is the centerpiece of observability. A function that groups counts by log level is the kernel of every dashboard you've ever looked at.

Input format

Null-terminated buffer with lines separated by \n. Each line may or may not start with a level tag.

Output format

Three counts via output pointers.

Constraints

One pass. No allocations.

Starter code

void log_count_levels(const char *log, int *info, int *warn, int *err) { /* TODO */ }

Common mistakes

Counting level-tag occurrences anywhere on a line (must be a prefix); double-counting when a line wraps; not handling the last (non-newline-terminated) line.

Edge cases to handle

Empty input. Unknown prefix lines. Multiple consecutive newlines.

Complexity

O(n) time.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.