use std::env;
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
eprintln!("Usage: {} <number-of-lines> <output-file>", args[0]);
std::process::exit(1);
}
let num_lines: usize = args[1].parse().expect("Invalid number of lines");
let file_path = &args[2];
println!(
"Creating log file with {} lines at {}",
num_lines, file_path
);
create_benchmark_logs(num_lines, file_path)?;
println!("Log file created successfully.");
Ok(())
}
fn create_benchmark_logs(lines: usize, file_path: &str) -> io::Result<()> {
if let Some(parent) = Path::new(file_path).parent() {
std::fs::create_dir_all(parent)?;
}
let mut file = File::create(file_path)?;
for i in 0..lines {
let level = match i % 5 {
0 => "ERROR",
1 => "WARN",
2 => "INFO",
3 => "DEBUG",
_ => "TRACE",
};
let message = match i % 20 {
0 => "NullPointerException in WebController.java:42",
1 => "Connection timeout in NetworkClient.java:86",
2 => "Database query took 2.3s in DatabaseService.java:128",
3 => "Application started successfully",
4 => "Session created for user_123",
5 => "OutOfMemoryError in SearchIndexer.java:212",
6 => "Failed to process request: invalid parameters",
7 => "Cache miss for key: user_profile_123",
8 => "Authentication successful for user_123",
9 => "Request processed in 150ms",
10 => "500 Internal Server Error: POST /api/orders",
11 => "403 Forbidden: Access denied for user_456",
12 => "Slow database operation detected (query took 3.5s)",
13 => "Memory usage at 75% of allocated heap",
14 => "Cache hit ratio: 65.4% (last hour)",
15 => "API rate limit exceeded for client_789",
16 => "Garbage collection cycle completed in 250ms",
17 => "System backup started (estimated time: 15m)",
18 => "Certificate expiring in 30 days (domain.com)",
_ => "Configuration loaded from /etc/config.json",
};
writeln!(
file,
"2025-03-{:02} {:02}:{:02}:{:02},{:03} [{}] {}",
(i % 31) + 1, (i / 3600) % 24, (i / 60) % 60, i % 60, i % 1000, level,
message
)?;
}
Ok(())
}