ca: CLI 标准化(--host/--port/--log-level/--instance + LogC 级别过滤)
- 新增 --host/--port 覆盖 [Server] address/port - 新增 --config 覆盖 confs 拉取的配置名(默认 ca.ini) - 新增 --log-level debug|info|warn|error(LogC 加级别过滤) - 新增 --instance 实例名(注册中心用) - LogC.hpp: 新增 set_level() + push() 级别过滤(atomic 阈值) - 配置优先级: CLI > ENV > confs > 默认值 - services.yaml: ca 声明 --instance 和 --log-level
This commit is contained in:
1 parent
4275f002e0
commit
4df510712e
3 files changed
+74
-6
No files matched your search
@@ -18,6 +18,9 @@
|
|||||||
*
|
*
|
||||||
* // 进程退出前:
|
* // 进程退出前:
|
||||||
* qing::LogC::instance().shutdown(); // 停止后台线程并 flush 剩余日志(幂等)
|
* qing::LogC::instance().shutdown(); // 停止后台线程并 flush 剩余日志(幂等)
|
||||||
|
*
|
||||||
|
* // 设置日志级别(main 启动时调用,低于此级别的日志直接丢弃)
|
||||||
|
* qing::LogC::instance().set_level("warn"); // 只输出 warn/error
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
@@ -27,6 +30,7 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <boost/json.hpp>
|
#include <boost/json.hpp>
|
||||||
@@ -48,6 +52,17 @@ public:
|
|||||||
std::optional<std::string> extra; // 已序列化的 JSON 字符串(存 Logs.extra TEXT)
|
std::optional<std::string> extra; // 已序列化的 JSON 字符串(存 Logs.extra TEXT)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 日志级别 → 数值(大写兼容)
|
||||||
|
static const std::unordered_map<std::string, int>& level_order() {
|
||||||
|
static const std::unordered_map<std::string, int> 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& instance() {
|
||||||
static LogC inst;
|
static LogC inst;
|
||||||
return inst;
|
return inst;
|
||||||
@@ -55,11 +70,24 @@ public:
|
|||||||
|
|
||||||
// ---- 公开接口 --------------------------------------------------------
|
// ---- 公开接口 --------------------------------------------------------
|
||||||
|
|
||||||
|
// 设置日志级别阈值(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,
|
void push(const std::string& service, const std::string& level,
|
||||||
const std::string& message,
|
const std::string& message,
|
||||||
std::optional<std::string> trace_id = std::nullopt,
|
std::optional<std::string> trace_id = std::nullopt,
|
||||||
std::optional<std::string> extra = std::nullopt) {
|
std::optional<std::string> 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,
|
_queue.push(Entry{service, level, message,
|
||||||
std::move(trace_id), std::move(extra)});
|
std::move(trace_id), std::move(extra)});
|
||||||
}
|
}
|
||||||
@@ -193,6 +221,7 @@ private:
|
|||||||
std::string _db_addr = "127.0.0.1";
|
std::string _db_addr = "127.0.0.1";
|
||||||
int _db_port = 8005;
|
int _db_port = 8005;
|
||||||
std::mutex _endpoint_mtx; // 保护 _db_addr/_db_port 读写(set_endpoint 与后台线程并发)
|
std::mutex _endpoint_mtx; // 保护 _db_addr/_db_port 读写(set_endpoint 与后台线程并发)
|
||||||
|
std::atomic<int> _level_threshold{1}; // 日志级别阈值,默认 INFO(1)
|
||||||
|
|
||||||
static constexpr size_t _BATCH_SIZE = 50; // 队列满 50 条时 flush
|
static constexpr size_t _BATCH_SIZE = 50; // 队列满 50 条时 flush
|
||||||
static constexpr double _FLUSH_INTERVAL = 2.0; // 超时 2 秒强制 flush
|
static constexpr double _FLUSH_INTERVAL = 2.0; // 超时 2 秒强制 flush
|
||||||
|
|||||||
+44
-5
@@ -406,6 +406,9 @@ static std::unordered_map<std::string, std::string> _WS_PROXY_ROUTES;
|
|||||||
/* instance items */
|
/* instance items */
|
||||||
static std::unique_ptr<HttpServer> _SERVE;
|
static std::unique_ptr<HttpServer> _SERVE;
|
||||||
static std::unique_ptr<MyRSA::Generator> _KEYGEN;
|
static std::unique_ptr<MyRSA::Generator> _KEYGEN;
|
||||||
|
/* CLI 标准化参数 */
|
||||||
|
static std::string _INSTANCE = "ca"; // --instance,注册中心用
|
||||||
|
static std::string _LOG_LEVEL = "info"; // --log-level,debug|info|warn|error
|
||||||
//static std::unique_ptr<Sqlite3> _SQLDB;
|
//static std::unique_ptr<Sqlite3> _SQLDB;
|
||||||
static RateLimiter _LOGIN_RATE;
|
static RateLimiter _LOGIN_RATE;
|
||||||
static RateLimiter _REGISTER_RATE;
|
static RateLimiter _REGISTER_RATE;
|
||||||
@@ -769,6 +772,11 @@ int main(int argc, char **argv) {
|
|||||||
desc.add_options()
|
desc.add_options()
|
||||||
("help,h", "output help message")
|
("help,h", "output help message")
|
||||||
("version,v", "output version message")
|
("version,v", "output version message")
|
||||||
|
("host", po::value<std::string>(), "绑定地址(默认 127.0.0.1),覆盖 [Server] address")
|
||||||
|
("port", po::value<int>(), "监听端口(默认 8443),覆盖 [Server] port")
|
||||||
|
("config", po::value<std::string>(), "confs 拉取的配置名(默认 ca.ini)")
|
||||||
|
("log-level", po::value<std::string>(), "日志级别 debug|info|warn|error(默认 info)")
|
||||||
|
("instance", po::value<std::string>(), "实例名(注册中心用,默认 ca)")
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
@@ -815,15 +823,21 @@ int main(int argc, char **argv) {
|
|||||||
_CONFS_HOST = ConfIni::env("CONFS_HOST", "127.0.0.1");
|
_CONFS_HOST = ConfIni::env("CONFS_HOST", "127.0.0.1");
|
||||||
_CONFS_PORT = ConfIni::env_int("CONFS_PORT", 8010);
|
_CONFS_PORT = ConfIni::env_int("CONFS_PORT", 8010);
|
||||||
|
|
||||||
|
/* --config 覆盖 confs 拉取的配置名(在 confs 拉取前读取) */
|
||||||
|
std::string confs_config_name = "ca.ini";
|
||||||
|
if (vm.count("config")) {
|
||||||
|
confs_config_name = vm["config"].as<std::string>();
|
||||||
|
}
|
||||||
|
|
||||||
ConfIni cf;
|
ConfIni cf;
|
||||||
try {
|
try {
|
||||||
/* 从 confs 拉取 ca.ini */
|
/* 从 confs 拉取配置 */
|
||||||
std::cout << "[ca] 从 confs (" << _CONFS_HOST << ":" << _CONFS_PORT
|
std::cout << "[ca] 从 confs (" << _CONFS_HOST << ":" << _CONFS_PORT
|
||||||
<< ") 拉取 ca.ini ..." << std::endl;
|
<< ") 拉取 " << confs_config_name << " ..." << std::endl;
|
||||||
pmc::net::MsgClient confs_client(_CONFS_HOST, _CONFS_PORT, "ca");
|
pmc::net::MsgClient confs_client(_CONFS_HOST, _CONFS_PORT, "ca");
|
||||||
json::object req;
|
json::object req;
|
||||||
req["_route"] = "get";
|
req["_route"] = "get";
|
||||||
req["name"] = "ca.ini";
|
req["name"] = confs_config_name;
|
||||||
std::string resp_str = confs_client.call(
|
std::string resp_str = confs_client.call(
|
||||||
json::serialize(req), /*timeout=*/5.0,
|
json::serialize(req), /*timeout=*/5.0,
|
||||||
/*retry_on_refused=*/true);
|
/*retry_on_refused=*/true);
|
||||||
@@ -917,7 +931,28 @@ int main(int argc, char **argv) {
|
|||||||
int env_smtp_port = ConfIni::env_int("SMTP_PORT", -1);
|
int env_smtp_port = ConfIni::env_int("SMTP_PORT", -1);
|
||||||
if (env_smtp_port > 0) _SMTP_PORT = env_smtp_port;
|
if (env_smtp_port > 0) _SMTP_PORT = env_smtp_port;
|
||||||
|
|
||||||
/* 4. 配置完整性校验 */
|
/* 4. CLI 参数覆盖(最高优先级) */
|
||||||
|
if (vm.count("host")) _ADDR = vm["host"].as<std::string>();
|
||||||
|
if (vm.count("port")) _PORT = vm["port"].as<int>();
|
||||||
|
if (vm.count("instance")) _INSTANCE = vm["instance"].as<std::string>();
|
||||||
|
if (vm.count("log-level")) {
|
||||||
|
_LOG_LEVEL = vm["log-level"].as<std::string>();
|
||||||
|
// 归一化 warning → warn
|
||||||
|
if (_LOG_LEVEL == "warning") _LOG_LEVEL = "warn";
|
||||||
|
// 合法性校验
|
||||||
|
static const char* valid[] = {"debug", "info", "warn", "error"};
|
||||||
|
bool ok = false;
|
||||||
|
for (const char* v : valid) {
|
||||||
|
if (_LOG_LEVEL == v) { ok = true; break; }
|
||||||
|
}
|
||||||
|
if (!ok) {
|
||||||
|
std::cerr << "[ca] 致命错误: 非法 --log-level: " << _LOG_LEVEL
|
||||||
|
<< "(合法值: debug|info|warn|error)" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 5. 配置完整性校验 */
|
||||||
if (_ADDR.empty() || _PORT == -1) {
|
if (_ADDR.empty() || _PORT == -1) {
|
||||||
std::cerr << "[ca] 致命错误: 配置项 [Server] address/port 缺失"
|
std::cerr << "[ca] 致命错误: 配置项 [Server] address/port 缺失"
|
||||||
"(ini 中未配置且无 LISTEN_ADDR/LISTEN_PORT 环境变量覆盖)" << std::endl;
|
"(ini 中未配置且无 LISTEN_ADDR/LISTEN_PORT 环境变量覆盖)" << std::endl;
|
||||||
@@ -937,8 +972,12 @@ int main(int argc, char **argv) {
|
|||||||
DbC::init(_DB_HOST, _DB_PORT);
|
DbC::init(_DB_HOST, _DB_PORT);
|
||||||
logs().set_endpoint(_DB_HOST, _DB_PORT);
|
logs().set_endpoint(_DB_HOST, _DB_PORT);
|
||||||
|
|
||||||
|
/* 设置日志级别过滤 */
|
||||||
|
logs().set_level(_LOG_LEVEL);
|
||||||
|
|
||||||
std::cout << "[ca] 监听: " << _ADDR << ":" << _PORT
|
std::cout << "[ca] 监听: " << _ADDR << ":" << _PORT
|
||||||
<< " (TLS: " << (_TLS ? "on" : "off") << ")" << std::endl;
|
<< " (TLS: " << (_TLS ? "on" : "off") << ")" << std::endl;
|
||||||
|
std::cout << "[ca] 实例: " << _INSTANCE << " 日志级别: " << _LOG_LEVEL << std::endl;
|
||||||
std::cout << "[ca] 下游服务: db=" << _DB_HOST << ":" << _DB_PORT
|
std::cout << "[ca] 下游服务: db=" << _DB_HOST << ":" << _DB_PORT
|
||||||
<< ", captcha=" << _CAPTCHA_HOST << ":" << _CAPTCHA_PORT
|
<< ", captcha=" << _CAPTCHA_HOST << ":" << _CAPTCHA_PORT
|
||||||
<< ", smtp=" << _SMTP_HOST << ":" << _SMTP_PORT << std::endl;
|
<< ", smtp=" << _SMTP_HOST << ":" << _SMTP_PORT << std::endl;
|
||||||
@@ -984,7 +1023,7 @@ int main(int argc, char **argv) {
|
|||||||
// 等待服务器启动完成(简单延迟,确保started日志打印后再输出提示)
|
// 等待服务器启动完成(简单延迟,确保started日志打印后再输出提示)
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
std::cout << "CA " << _VER << " started" << std::endl;
|
std::cout << "CA " << _VER << " started" << std::endl;
|
||||||
logs().info("ca", std::string("CA ") + _VER + " started");
|
logs().info("ca", std::string("CA ") + _VER + " started (instance=" + _INSTANCE + ")");
|
||||||
|
|
||||||
/* 主线程:等待关闭信号,带超时轮询 */
|
/* 主线程:等待关闭信号,带超时轮询 */
|
||||||
while (!_stop_requested && !server_exited) {
|
while (!_stop_requested && !server_exited) {
|
||||||
|
|||||||
+1
-1
@@ -77,7 +77,7 @@ services:
|
|||||||
|
|
||||||
- name: ca
|
- name: ca
|
||||||
cmd: bash /opt/micro_servers/ca/_run.sh
|
cmd: bash /opt/micro_servers/ca/_run.sh
|
||||||
args: []
|
args: ["--instance", "ca", "--log-level", "info"]
|
||||||
depends_on: [db2]
|
depends_on: [db2]
|
||||||
sleep_before: 2
|
sleep_before: 2
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user