From 29eac70f24bcb8ca347b7a40ddb5b26d390808e7 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Mon, 10 Aug 2026 12:00:34 +0800 Subject: [PATCH] =?UTF-8?q?captcha2:=20CLI=20=E6=A0=87=E5=87=86=E5=8C=96?= =?UTF-8?q?=EF=BC=88--host/--config/--log-level/--instance=20+=20LogC=20?= =?UTF-8?q?=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(别名 --addr/-a)覆盖 [server] host - 新增 --config 覆盖 confs 拉取的配置名(默认 captcha.ini) - 新增 --log-level debug|info|warn|error(LogC 加级别过滤) - 新增 --instance 实例名(注册中心用) - LogC.hpp: 新增 set_level() + push() 级别过滤(atomic 阈值,大写兼容) - 配置优先级: CLI > ENV > confs > 默认值 - services.yaml: captcha2 声明 --instance 和 --log-level --- captcha2/inc/LogC.hpp | 28 ++++++++++++++++++++++++++++ captcha2/src/main.cpp | 32 +++++++++++++++++++++++++------- core/services.yaml | 2 +- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/captcha2/inc/LogC.hpp b/captcha2/inc/LogC.hpp index 0b21a1b..67e201f 100644 --- a/captcha2/inc/LogC.hpp +++ b/captcha2/inc/LogC.hpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include namespace captcha2 { @@ -30,11 +32,31 @@ struct LogEntry { class LogC { 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; + } + static LogC& instance() { static LogC inst; return inst; } + // 设置日志级别阈值(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 info(const std::string& svc, const std::string& msg) { push(svc, "INFO", msg); } @@ -64,6 +86,11 @@ private: ~LogC() { if (queue_) queue_->shutdown(); } void push(const std::string& svc, const std::string& level, const std::string& msg) { + // 日志级别过滤:低于阈值直接丢弃 + 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; + auto now = std::chrono::system_clock::now(); auto t = std::chrono::system_clock::to_time_t(now); char buf[32]; @@ -105,6 +132,7 @@ private: std::string db_host_; int db_port_; + std::atomic level_threshold_{1}; // 日志级别阈值,默认 INFO(1) std::unique_ptr> queue_; }; diff --git a/captcha2/src/main.cpp b/captcha2/src/main.cpp index 3c8737b..b54706e 100644 --- a/captcha2/src/main.cpp +++ b/captcha2/src/main.cpp @@ -70,12 +70,13 @@ struct CaptchaConfig { int rate_limit_per_min = 600; // P1-1: 全局限流仅作最后防线,主要限流由 gateway 按 IP 负责 }; -// 从 confs 拉取 captcha.ini 全文,阻塞等待 confs 就绪 -static std::string fetch_config_from_confs(const std::string& confs_host, int confs_port) { +// 从 confs 拉取指定名称的配置文件全文,阻塞等待 confs 就绪 +static std::string fetch_config_from_confs(const std::string& confs_host, int confs_port, + const std::string& config_name) { pmc::net::RpcClient client(confs_host, confs_port, "captcha2", /*timeout=*/5.0, /*retry_on_refused=*/true); try { - auto data = client.call("get", {{"name", "captcha.ini"}}); + auto data = client.call("get", {{"name", config_name}}); auto* content = data.as_object().if_contains("content"); if (!content || !content->is_string()) { std::cerr << "[captcha2] confs 返回的 captcha.ini 无 content 字段" << std::endl; @@ -531,6 +532,9 @@ int main(int argc, char* argv[]) { // ── 命令行参数 ── std::string confs_host = "127.0.0.1"; int confs_port = 8010; + std::string confs_config_name = "captcha.ini"; // --config 覆盖 + std::string instance = "captcha2"; // --instance 覆盖 + std::string log_level = "info"; // --log-level 覆盖 { const char* env; if ((env = std::getenv("CONFS_HOST"))) confs_host = env; @@ -540,9 +544,13 @@ int main(int argc, char* argv[]) { po::options_description desc("captcha2 — pymsg 验证码服务"); desc.add_options() ("help,h", "显示帮助") - ("addr,a", po::value(), "监听地址(覆盖 confs)") + ("addr,a", po::value(), "监听地址(覆盖 confs,别名 --host)") + ("host", po::value(), "监听地址(覆盖 confs,别名 --addr/-a)") ("port,p", po::value(), "监听端口(覆盖 confs)") - ("workers,w", po::value(), "工作线程数(覆盖 confs)"); + ("workers,w", po::value(), "工作线程数(覆盖 confs)") + ("config", po::value(), "confs 拉取的配置名(默认 captcha.ini)") + ("log-level", po::value(), "日志级别 debug|info|warn|error,默认 info") + ("instance", po::value(), "实例名(注册中心用,默认 captcha2)"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); @@ -552,20 +560,30 @@ int main(int argc, char* argv[]) { return 0; } + // 预扫描 --config / --instance / --log-level / --host(confs 拉取前处理) + if (vm.count("config")) confs_config_name = vm["config"].as(); + if (vm.count("instance")) instance = vm["instance"].as(); + if (vm.count("log-level")) log_level = vm["log-level"].as(); + // ── 配置加载:confs → env → 命令行 ── - std::string ini_text = fetch_config_from_confs(confs_host, confs_port); + std::string ini_text = fetch_config_from_confs(confs_host, confs_port, confs_config_name); merge_ini(g_config, ini_text); apply_env_overrides(g_config); if (vm.count("addr")) g_config.host = vm["addr"].as(); + if (vm.count("host")) g_config.host = vm["host"].as(); if (vm.count("port")) g_config.port = vm["port"].as(); if (vm.count("workers")) g_config.workers = vm["workers"].as(); validate_config(g_config); - std::cerr << "[captcha2] 配置来源: confs captcha.ini @ " + g_log.set_level(log_level); + + std::cerr << "[captcha2] 配置来源: confs " << confs_config_name << " @ " << confs_host << ":" << confs_port << std::endl; + std::cerr << "[captcha2] instance=" << instance + << " log-level=" << log_level << std::endl; std::cerr << "[captcha2] captcha_len=" << g_config.len << " ttl=" << g_config.ttl << " max_store=" << g_config.max_store diff --git a/core/services.yaml b/core/services.yaml index 735379c..2fcdfcb 100644 --- a/core/services.yaml +++ b/core/services.yaml @@ -90,7 +90,7 @@ services: # --- 验证码 --- - name: captcha2 cmd: bash /opt/micro_servers/captcha2/_run.sh - args: [] + args: ["--instance", "captcha2", "--log-level", "info"] depends_on: [] sleep_before: 1