|
12 | 12 | #include <thread>
|
13 | 13 | #include <memory>
|
14 | 14 | #include <cstdlib>
|
| 15 | + |
| 16 | +#ifndef _WIN32 |
15 | 17 | #include <syslog.h>
|
| 18 | +#endif |
16 | 19 |
|
17 | 20 | #define CPPHTTPLIB_OPENSSL_SUPPORT
|
18 | 21 | #include "httplib.hpp"
|
@@ -383,34 +386,41 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
|
383 | 386 |
|
384 | 387 | string host_str = host.GetString();
|
385 | 388 |
|
| 389 | + |
386 | 390 | #ifndef _WIN32
|
387 |
| - // Debug and Syslog Events |
388 |
| - const char* use_syslog = std::getenv("DUCKDB_HTTPSERVER_SYSLOG"); |
389 | 391 | const char* debug_env = std::getenv("DUCKDB_HTTPSERVER_DEBUG");
|
390 |
| - if (use_syslog != nullptr && std::string(use_syslog) == "1") { |
391 |
| - openlog("duckdb-httpserver", LOG_PID | LOG_NDELAY, LOG_LOCAL0); |
| 392 | + const char* use_syslog = std::getenv("DUCKDB_HTTPSERVER_SYSLOG"); |
| 393 | + |
| 394 | + if (debug_env != nullptr && std::string(debug_env) == "1") { |
392 | 395 | global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
|
393 |
| - syslog(LOG_INFO, "%s %s - %d", |
| 396 | + time_t now_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
| 397 | + char timestr[32]; |
| 398 | + strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_time)); |
| 399 | + // Use \r\n for consistent line endings |
| 400 | + fprintf(stdout, "[%s] %s %s - %d - from %s:%d\r\n", |
| 401 | + timestr, |
394 | 402 | req.method.c_str(),
|
395 | 403 | req.path.c_str(),
|
396 |
| - res.status); |
397 |
| - }); |
398 |
| - std::atexit([]() { |
399 |
| - closelog(); |
| 404 | + res.status, |
| 405 | + req.remote_addr.c_str(), |
| 406 | + req.remote_port); |
| 407 | + fflush(stdout); |
400 | 408 | });
|
401 |
| - } else if (debug_env != nullptr && std::string(debug_env) == "1") { |
| 409 | + } else if (use_syslog != nullptr && std::string(use_syslog) == "1") { |
| 410 | + openlog("duckdb-httpserver", LOG_PID | LOG_NDELAY, LOG_LOCAL0); |
402 | 411 | global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
|
403 |
| - auto now = std::chrono::system_clock::now(); |
404 |
| - auto now_time = std::chrono::system_clock::to_time_t(now); |
405 |
| - fprintf(stdout, "[%s] %s %s - %d\n", |
406 |
| - std::ctime(&now_time), |
| 412 | + syslog(LOG_INFO, "%s %s - %d - from %s:%d", |
407 | 413 | req.method.c_str(),
|
408 | 414 | req.path.c_str(),
|
409 |
| - res.status); |
410 |
| - fflush(stdout); |
| 415 | + res.status, |
| 416 | + req.remote_addr.c_str(), |
| 417 | + req.remote_port); |
411 | 418 | });
|
412 |
| - } |
413 |
| -#endif |
| 419 | + std::atexit([]() { |
| 420 | + closelog(); |
| 421 | + }); |
| 422 | + } |
| 423 | +#endif |
414 | 424 |
|
415 | 425 | const char* run_in_same_thread_env = std::getenv("DUCKDB_HTTPSERVER_FOREGROUND");
|
416 | 426 | bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1");
|
|
0 commit comments