From 22c71c57b3a80dfff2fc584bc491bfafb5acd48a Mon Sep 17 00:00:00 2001 From: hevake_lcj Date: Wed, 20 Jul 2022 16:51:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8F=82=E6=95=B0=E4=B8=AD?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=8C=87=E5=AE=9A=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=AD=89=E7=BA=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/log.cpp | 79 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/main/log.cpp b/main/log.cpp index 5575eb8..1cfc5c6 100644 --- a/main/log.cpp +++ b/main/log.cpp @@ -27,17 +27,20 @@ void Log::fillDefaultConfig(Json &cfg) const { "stdout": { "enable": true, - "enable_color": true + "enable_color": true, + "levels": {"":6} }, "filelog": { "enable": true, + "enable_color": false, + "levels": {"":3}, "prefix": "sample", "path": "/tmp/tbox", - "max_size": 1024, - "enable_color": false + "max_size": 1024 }, "syslog": { - "enable": false + "enable": false, + "levels": {"":3} } } )"_json; @@ -71,6 +74,16 @@ bool Log::initialize(const char *proc_name, Context &ctx, const Json &cfg) cerr << "WARN: stdout.enable_color not boolean." << endl; } } + + if (js.contains("levels")) { + auto &js_levels = js.at("levels"); + if (js_levels.is_object()) { + for (auto it = js_levels.begin(); it != js_levels.end(); ++it) + stdout_.setLevel(it.value(), it.key()); + } else { + cerr << "WARN: stdout.levels not object." << endl; + } + } } if (js_log.contains("syslog")) { @@ -86,6 +99,16 @@ bool Log::initialize(const char *proc_name, Context &ctx, const Json &cfg) cerr << "WARN: syslog.enable not boolean." << endl; } } + + if (js.contains("levels")) { + auto &js_levels = js.at("levels"); + if (js_levels.is_object()) { + for (auto it = js_levels.begin(); it != js_levels.end(); ++it) + syslog_.setLevel(it.value(), it.key()); + } else { + cerr << "WARN: stdout.levels not object." << endl; + } + } } do { @@ -98,6 +121,35 @@ bool Log::initialize(const char *proc_name, Context &ctx, const Json &cfg) break; } + if (js.contains("enable")) { + auto &js_enable = js.at("enable"); + if (js_enable.is_boolean()) { + if (js_enable.get()) + filelog_.enable(); + else + filelog_.disable(); + } else + cerr << "WARN: filelog.enable not boolean" << endl; + } + + if (js.contains("enable_color")) { + auto &js_enable = js.at("enable_color"); + if (js_enable.is_boolean()) { + filelog_.enableColor(js_enable.get()); + } else + cerr << "WARN: filelog.enable_color not boolean" << endl; + } + + if (js.contains("levels")) { + auto &js_levels = js.at("levels"); + if (js_levels.is_object()) { + for (auto it = js_levels.begin(); it != js_levels.end(); ++it) + filelog_.setLevel(it.value(), it.key()); + } else { + cerr << "WARN: stdout.levels not object." << endl; + } + } + auto &js_path = js.at("path"); if (!js_path.is_string()) { cerr << "WARN: filelog.path not string." << endl; @@ -125,25 +177,6 @@ bool Log::initialize(const char *proc_name, Context &ctx, const Json &cfg) else cerr << "WARN: filelog.max_size not number" << endl; } - - if (js.contains("enable")) { - auto &js_enable = js.at("enable"); - if (js_enable.is_boolean()) { - if (js_enable.get()) - filelog_.enable(); - else - filelog_.disable(); - } else - cerr << "WARN: filelog.enable not boolean" << endl; - } - - if (js.contains("enable_color")) { - auto &js_enable = js.at("enable_color"); - if (js_enable.is_boolean()) { - filelog_.enableColor(js_enable.get()); - } else - cerr << "WARN: filelog.enable_color not boolean" << endl; - } } while (false); } -- Gitee