view seobeo/LOGGING.md @ 102:1065c226e52b

[MrJuneJune] Optimize the binary.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 08:58:58 -0800
parents 70401cf61e97
children
line wrap: on
line source

# Seobeo Logging System

## Overview

All `printf` statements in the seobeo library have been replaced with `Seobeo_Log()` calls for consistent, level-based logging with optional debug mode.

## Log Levels

```c
typedef enum {
  SEOBEO_INFO = 0,     // General information (server start, mode, etc.)
  SEOBEO_WARNING,      // Warnings (SSL not compiled, etc.)
  SEOBEO_ERROR,        // Errors (allocation failures, connection errors, etc.)
  SEOBEO_DEBUG,        // Debug messages (only shown in development mode)
} Seobeo_Log_Level;
```

## Usage

```c
Seobeo_Log(SEOBEO_INFO, "Listening on port %s\n", port);
Seobeo_Log(SEOBEO_ERROR, "Failed to allocate %zu bytes\n", size);
Seobeo_Log(SEOBEO_DEBUG, "Request line (first %zu bytes)\n", len);
```

## Debug Mode Control

Debug logs are controlled by the `SEOBEO_ENABLE_DEBUG` macro:

- **Production builds**: Debug logs are disabled (not compiled in)
- **Development builds**: Debug logs are enabled via `-DSEOBEO_ENABLE_DEBUG`

## Build Targets

### Seobeo Library

#### Production (No Debug Logs)
```bash
# macOS
//seobeo:seobeo_server_macos

# Linux
//seobeo:seobeo_server_linux

# Platform-agnostic alias
//seobeo:seobeo_server
```

#### Development (With Debug Logs)
```bash
# macOS
//seobeo:seobeo_server_macos_dev

# Linux
//seobeo:seobeo_server_linux_dev

# Platform-agnostic alias
//seobeo:seobeo_server_dev
```

### MrJuneJune Server

#### Production Build
```bash
# Build
bazel build //mrjunejune:mrjunejune_server

# Run
bazel run //mrjunejune:mrjunejune_server

# Bundle (for deployment)
bazel build //mrjunejune:mrjunejune_server_bundle
```

#### Development Build (With Debug Logs)
```bash
# Build
bazel build //mrjunejune:mrjunejune_server_dev

# Run
bazel run //mrjunejune:mrjunejune_server_dev

# Bundle
bazel build //mrjunejune:mrjunejune_server_dev_bundle
```

## Log Output Format

All logs are prefixed with their level:

```
[INFO] Listening on port 6969
[INFO] Server mode: EDGE
[ERROR] Failed to allocate 1024 bytes for body
[DEBUG] Request line (first 200 bytes)
[DEBUG] sscanf returned 3 (method='GET', path='/', version='HTTP/1.1')
```

## Changes Summary

### Files Modified

1. **seobeo/s_web.c**
   - Replaced 26 printf statements with Seobeo_Log
   - Updated logging to use appropriate levels (INFO, ERROR, DEBUG)

2. **seobeo/s_linux_network.c**
   - Replaced 10 printf statements with Seobeo_Log
   - Added proper error and debug logging for network operations

3. **seobeo/s_ssl.c**
   - Replaced 5 printf/fprintf statements with Seobeo_Log
   - SSL errors now use SEOBEO_ERROR level
   - SSL info uses SEOBEO_INFO level

4. **seobeo/BUILD**
   - Added `seobeo_server_dev` alias for development builds
   - Added `seobeo_server_macos_dev` with `SEOBEO_ENABLE_DEBUG`
   - Added `seobeo_server_linux_dev` with `SEOBEO_ENABLE_DEBUG`

5. **mrjunejune/BUILD**
   - Added `mrjunejune_server_dev` binary target
   - Added `mrjunejune_server_dev_bundle` for development deployment

## Benefits

1. **Cleaner Production Logs**: Debug messages don't clutter production output
2. **Better Debugging**: Enable verbose logging during development
3. **Consistent Format**: All logs follow the same `[LEVEL] message` format
4. **Conditional Compilation**: Debug code is completely removed from production builds (zero runtime overhead)
5. **Easy Toggle**: Switch between dev and prod with different build targets

## Examples

### Running in Development Mode
```bash
$ bazel run //mrjunejune:mrjunejune_server_dev
[INFO] Listening on port 6969
[INFO] Server mode: EDGE
[DEBUG] Request line (first 30 bytes)
[DEBUG] sscanf returned 3 (method='GET', path='/', version='HTTP/1.1')
[DEBUG] Allocating method_copy
[DEBUG] Allocating version_copy
[DEBUG] Map now has 2 entries
[DEBUG] File path: /index.html
Body Size: 1234
[DEBUG] Request handled successfully
```

### Running in Production Mode
```bash
$ bazel run //mrjunejune:mrjunejune_server
[INFO] Listening on port 6969
[INFO] Server mode: EDGE
```

Notice that debug messages are completely absent in production mode!