From 8eb14cad4a36caa66aded0927ece8ef5e7bb5437 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Mon, 10 Aug 2026 11:55:54 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20CLI=20=E6=A0=87=E5=87=86=E5=8C=96?= =?UTF-8?q?=EF=BC=88--host/--port/--config/--log-level/--instance=20+=20Lo?= =?UTF-8?q?gC=20=E7=BA=A7=E5=88=AB=E8=BF=87=E6=BB=A4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 --host/--port 覆盖 [Server] address/port - 新增 --config 覆盖 confs 拉取的配置名(默认 gateway.ini) - 新增 --log-level debug|info|warn|error(LogC 加级别过滤) - 新增 --instance 实例名(注册中心用) - LogC.hpp: 新增 set_level() + push() 级别过滤(atomic 阈值,大写兼容) - 配置优先级: CLI > ENV > confs > 默认值 - services.yaml: gateway 声明 --instance 和 --log-level --- core/services.yaml | 2 +- gateway/inc/LogC.hpp | 29 +++++++++++++++++++++++++++++ gateway/src/main.cpp | 31 +++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/core/services.yaml b/core/services.yaml index 1be432e..735379c 100644 --- a/core/services.yaml +++ b/core/services.yaml @@ -83,7 +83,7 @@ services: - name: gateway cmd: bash /opt/micro_servers/gateway/_run.sh - args: [] + args: ["--instance", "gateway", "--log-level", "info"] depends_on: [db2] sleep_before: 2 diff --git a/gateway/inc/LogC.hpp b/gateway/inc/LogC.hpp index aef0923..a77d23d 100644 --- a/gateway/inc/LogC.hpp +++ b/gateway/inc/LogC.hpp @@ -18,6 +18,9 @@ * * // 进程退出前: * qing::LogC::instance().shutdown(); // 停止后台线程并 flush 剩余日志(幂等) + * + * // 设置日志级别(main 启动时调用,低于此级别的日志直接丢弃) + * qing::LogC::instance().set_level("warn"); // 只输出 warn/error */ #include @@ -27,6 +30,7 @@ #include #include #include +#include #include #include @@ -55,11 +59,35 @@ public: // ---- 公开接口 -------------------------------------------------------- + // 日志级别 → 数值(大写兼容) + static const std::unordered_map& level_order() { + static const std::unordered_map k = { + {"debug", 0}, {"info", 1}, {"warn", 2}, {"warning", 2}, + {"error", 3}, {"critical", 3}, + {"DEBUG", 0}, {"INFO", 1}, {"WARN", 2}, {"WARNING", 2}, + {"ERROR", 3}, {"CRITICAL", 3}, + }; + return k; + } + + // 设置日志级别阈值(debug|info|warn|error,大小写不限) + // 低于此级别的日志在 push 时直接丢弃 + void set_level(const std::string& level) { + auto it = level_order().find(level); + if (it != level_order().end()) { + _level_threshold.store(it->second, std::memory_order_relaxed); + } + } + // 推送一条日志(非阻塞,立即返回) void push(const std::string& service, const std::string& level, const std::string& message, std::optional trace_id = std::nullopt, std::optional extra = std::nullopt) { + // 日志级别过滤:低于阈值直接丢弃 + auto it = level_order().find(level); + int lvl = (it != level_order().end()) ? it->second : 1; + if (lvl < _level_threshold.load(std::memory_order_relaxed)) return; _queue.push(Entry{service, level, message, std::move(trace_id), std::move(extra)}); } @@ -193,6 +221,7 @@ private: std::string _db_addr = "127.0.0.1"; int _db_port = 8005; std::mutex _endpoint_mtx; // 保护 _db_addr/_db_port 读写(set_endpoint 与后台线程并发) + std::atomic _level_threshold{1}; // 日志级别阈值,默认 INFO(1) static constexpr size_t _BATCH_SIZE = 50; // 队列满 50 条时 flush static constexpr double _FLUSH_INTERVAL = 2.0; // 超时 2 秒强制 flush diff --git a/gateway/src/main.cpp b/gateway/src/main.cpp index 789a3b9..3fd2bc9 100644 --- a/gateway/src/main.cpp +++ b/gateway/src/main.cpp @@ -109,6 +109,11 @@ static int _DB_PORT = 8005; /* confs 引导配置(不依赖 confs 本身,环境变量 CONFS_HOST/CONFS_PORT 覆盖,默认 127.0.0.1:8010) */ static std::string _CONFS_HOST = "127.0.0.1"; static int _CONFS_PORT = 8010; +/* confs 配置名(--config 覆盖,默认 gateway.ini) */ +static std::string _CONFS_CONFIG_NAME = "gateway.ini"; +/* CLI 标准化参数(注册中心 / 日志级别) */ +static std::string _INSTANCE = "gateway"; +static std::string _LOG_LEVEL = "info"; /* instance items */ static std::unique_ptr _SERVE; @@ -164,6 +169,11 @@ int main(int argc, char **argv) { desc.add_options() ("help,h", "output help message") ("version,v", "output version message") + ("host", po::value(), "绑定地址(覆盖 [Server] address,默认 127.0.0.1)") + ("port", po::value(), "监听端口(覆盖 [Server] port,默认 8445)") + ("config", po::value(), "confs 拉取的配置名(默认 gateway.ini)") + ("log-level", po::value(), "日志级别 debug|info|warn|error,默认 info") + ("instance", po::value(), "实例名(注册中心用,默认 gateway)") ; @@ -195,6 +205,10 @@ int main(int argc, char **argv) { return 0; } + /* CLI 预扫描:--config 必须在 confs 拉取前处理(其余 CLI 覆盖在 ENV 之后接入) */ + if (vm.count("config")) + _CONFS_CONFIG_NAME = vm["config"].as(); + /* ==================== 配置加载(两层:confs 强依赖 → 环境变量覆盖) ==================== * 与 Python 服务(dnfs/db/static)保持一致的加载模式: * 1. 引导配置:confs 地址从 CONFS_HOST/CONFS_PORT 环境变量读取,缺省 127.0.0.1:8010 @@ -214,11 +228,11 @@ int main(int argc, char **argv) { try { /* 从 confs 拉取 gateway.ini */ std::cout << "[gateway] 从 confs (" << _CONFS_HOST << ":" << _CONFS_PORT - << ") 拉取 gateway.ini ..." << std::endl; + << ") 拉取 " << _CONFS_CONFIG_NAME << " ..." << std::endl; pmc::net::MsgClient confs_client(_CONFS_HOST, _CONFS_PORT, "gateway"); json::object req; req["_route"] = "get"; - req["name"] = "gateway.ini"; + req["name"] = _CONFS_CONFIG_NAME; std::string resp_str = confs_client.call( json::serialize(req), /*timeout=*/5.0, /*retry_on_refused=*/true); @@ -268,6 +282,16 @@ int main(int argc, char **argv) { int env_db_port = ConfIni::env_int("DB_PORT", -1); if (env_db_port > 0) _DB_PORT = env_db_port; + /* 3b. CLI 覆盖(最高优先级):--host / --port / --instance / --log-level */ + if (vm.count("host")) + _ADDR = vm["host"].as(); + if (vm.count("port")) + _PORT = vm["port"].as(); + if (vm.count("instance")) + _INSTANCE = vm["instance"].as(); + if (vm.count("log-level")) + _LOG_LEVEL = vm["log-level"].as(); + /* 4. 配置完整性校验 */ if (_ADDR.empty() || _PORT == -1) { std::cerr << "[gateway] 致命错误: 配置项 [Server] address/port 缺失" @@ -295,9 +319,12 @@ int main(int argc, char **argv) { /* 初始化日志推送端点 */ logs().set_endpoint(_DB_HOST, _DB_PORT); + logs().set_level(_LOG_LEVEL); std::cout << "[gateway] 监听: " << _ADDR << ":" << _PORT << " (TLS: " << (_TLS ? "on" : "off") << ")" << std::endl; + std::cout << "[gateway] instance=" << _INSTANCE + << " log-level=" << _LOG_LEVEL << std::endl; // 根据 Server.tls 选择 HTTPS(带证书)或 HTTP(无证书)构造函数 // HTTP 模式用于 ca 前置后的内部转发链路