A command-line utility for logging messages, counting log entries, and managing log rotation.
- TCP server with thread-based concurrency
- Continuous logging with timestamps
- Log file rotation
- File locking for concurrent access
- Log entry counting
- Live configuration updates using memory-mapped files
The program provides four commands:
run- Start the web server (listens for TCP connections)count- Count the number of log entriesrotate- Rotate log files (renames http.log to http.1.log, etc.)update-config- Update server configuration while running
To start the web server:
# Start server on default port (8080) with default threads (4)
cargo run -- run
# Start server on specific port with custom number of threads
cargo run -- run --port 3000 --threads 8To count log entries:
cargo run -- countTo rotate log files:
cargo run -- rotateTo update server configuration:
# Update verbosity level
cargo run -- update-config --verbosity 2
# Update maximum connections
cargo run -- update-config --max-connections 200
# Update timeout
cargo run -- update-config --timeout 60The server implements a simple text-based protocol:
- Connect to the server using netcat:
# On macOS/Linux
nc localhost 8080
# On Windows (if you have netcat installed)
nc.exe localhost 8080- Send commands:
hello server- Server responds withhello client- Any other command - Server responds with
unknown command
Log entries are formatted as:
[YYYY-MM-DD HH:MM:SS] message
The program maintains up to 5 log files:
http.log(current log)http.1.log(most recent rotated log)http.2.loghttp.3.loghttp.4.loghttp.5.log(oldest rotated log, will be deleted on rotation)
When rotating logs:
http.5.logis deleted (if it exists)- Each log file is renamed to the next number (e.g.,
http.1.log→http.2.log) http.logis renamed tohttp.1.log
When running the server:
- Creates a fixed-size thread pool at startup
- Each incoming connection is handled by a worker thread from the pool
- Threads share configuration through atomic reference counting
- Default thread pool size is 4, but can be configured at startup
The server uses memory-mapped files to share configuration between threads. Configuration parameters include:
verbosity: Log verbosity level (0-3)max_connections: Maximum number of concurrent connectionstimeout_seconds: Connection timeout in seconds
Configuration changes are detected by worker threads in real-time, and they adjust their behavior accordingly. The configuration is stored in config.dat and is shared between all threads.
- Verbosity: 1
- Maximum Connections: 100
- Timeout: 30 seconds