Skip to content

Commit d01cede

Browse files
committed
日志配置修改
1 parent 53ac5f1 commit d01cede

File tree

6 files changed

+75
-76
lines changed

6 files changed

+75
-76
lines changed

public/cpp/actor/actor_base/actor_base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ngl
1919

2020
std::shared_ptr<np_actor_logitem> tools_log::get_log(const std::source_location& asource, ELOGLEVEL aloglevel, bool anet)
2121
{
22-
if(m_actor == nullptr || aloglevel < ngl::sysconfig::loglevel())
22+
if(m_actor == nullptr || !np_actor_logitem::check_level(aloglevel))
2323
{
2424
return g_actor_nonelog;
2525
}

public/cpp/actor/actor_logic/log/actor_log.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ namespace ngl
8080
bool actor_log::timer_handle(const message<timerparm>& adata)
8181
{
8282
std::shared_ptr<log_timerparm> lparm = std::static_pointer_cast<log_timerparm>(adata.get_data()->m_parm);
83-
8483
if (lparm->m_type == log_timerparm::e_logflush)
8584
{
8685
m_log->flush();
@@ -92,7 +91,6 @@ namespace ngl
9291
m_log->create();
9392
return true;
9493
}
95-
9694
return true;
9795
}
9896
}//namespace ngl

public/cpp/actor/nprotocol.h

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -765,50 +765,53 @@ namespace ngl
765765
}
766766

767767
void send(std::shared_ptr<np_logitem> pro);
768+
769+
768770
public:
771+
static bool check_level(ELOGLEVEL alevel)
772+
{
773+
return !(alevel < ngl::sysconfig::logconsolelevel() && alevel < ngl::sysconfig::logwritelevel());
774+
}
775+
769776
template <typename ...ARGS>
770777
void print(const std::format_string<ARGS...>& aformat, const ARGS&... aargs)
771778
{
772-
if (m_level >= ngl::sysconfig::loglevel())
779+
if (m_actortype == ENUM_ACTOR::ACTOR_LOG)
780+
return;
781+
if (m_init == false)
782+
return;
783+
if (!check_level(m_level))
784+
return;
785+
786+
std::string ldata = m_stream.str();
787+
ldata += std::vformat(aformat.get(), std::make_format_args(aargs...));
788+
789+
int32_t ltime = (int32_t)localtime::gettime();
790+
set_source();
791+
792+
if (m_level >= ngl::sysconfig::logconsolelevel())
793+
{
794+
char ltimebuff[1024];
795+
ngl::localtime::time2str(ltimebuff, 1024, ltime, "%Y/%m/%d %H:%M:%S");
796+
logprintf::printf(m_level, m_src.c_str(), ltimebuff, ldata.c_str());
797+
}
798+
799+
if (m_level >= ngl::sysconfig::logwritelevel())
773800
{
774-
if (sysconfig::logconsole() == false && sysconfig::logiswrite() == false)
775-
return;
776-
try
777-
{
778-
std::string ldata = m_stream.str();
779-
ldata += std::vformat(aformat.get(), std::make_format_args(aargs...));
780-
int32_t ltime = (int32_t)localtime::gettime();
781-
set_source();
782-
if (sysconfig::logconsole())
783-
{
784-
char ltimebuff[1024];
785-
ngl::localtime::time2str(ltimebuff, 1024, ltime, "%Y/%m/%d %H:%M:%S");
786-
logprintf::printf(m_level, m_src.c_str(), ltimebuff, ldata.c_str());
787-
}
788-
if (m_actortype == ENUM_ACTOR::ACTOR_LOG)
789-
return;
790-
if (m_init == false || sysconfig::logiswrite() == false)
791-
return;
792-
793-
auto pro = std::make_shared<np_logitem>();
794-
pro->m_loglevel = m_level;
795-
pro->m_serverid = nconfig::m_nodeid;
796-
pro->m_time = ltime;
797-
pro->m_src.swap(m_src);
798-
pro->m_data.swap(ldata);
799-
send(pro);
800-
}
801-
catch (...)
802-
{
803-
std::cout << "log error!" << std::endl;
804-
}
801+
auto pro = std::make_shared<np_logitem>();
802+
pro->m_loglevel = m_level;
803+
pro->m_serverid = nconfig::m_nodeid;
804+
pro->m_time = ltime;
805+
pro->m_src.swap(m_src);
806+
pro->m_data.swap(ldata);
807+
send(pro);
805808
}
806809
}
807810

808811
template <typename T>
809812
np_actor_logitem& operator<<(const T& adata)
810813
{
811-
if (m_init && m_level >= ngl::sysconfig::loglevel())
814+
if (m_init && check_level(m_level))
812815
{
813816
m_stream << adata;
814817
}
@@ -818,7 +821,7 @@ namespace ngl
818821
// 重载 << 操作符以输出 std::endl
819822
np_actor_logitem& operator<<(std::ostream& (*manipulator)(std::ostream&))
820823
{
821-
if (m_init && m_level >= ngl::sysconfig::loglevel())
824+
if (m_init && check_level(m_level))
822825
{
823826
m_stream << manipulator;
824827
}

public/cpp/tools/log/nlog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ngl
2929

3030
std::shared_ptr<np_actor_logitem> get_log(const std::source_location& asource, ELOGLEVEL alevel, bool anet)
3131
{
32-
if (alevel >= ngl::sysconfig::loglevel())
32+
if (np_actor_logitem::check_level(alevel))
3333
{
3434
return std::make_shared<np_actor_logitem>(alevel, ACTOR_NONE, anet? ELOG_NETWORK:ELOG_LOCAL, asource);
3535
}

public/cpp/tools/tab/xml/sysconfig.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,42 @@
55

66
namespace ngl
77
{
8-
9-
ELOGLEVEL sysconfig::m_loglevel = ELOG_ERROR;
10-
int32_t sysconfig::m_logline = 10000;
11-
int32_t sysconfig::m_logflushtime = 10;
12-
bool sysconfig::m_logiswrite = true;
13-
bool sysconfig::m_logconsole = false;
14-
int32_t sysconfig::m_consumings = 100;
8+
ELOGLEVEL sysconfig::m_logwritelevel = ELOG_ERROR;
9+
ELOGLEVEL sysconfig::m_logconsolelevel = ELOG_ERROR;
10+
int32_t sysconfig::m_logline = 10000;
11+
int32_t sysconfig::m_logflushtime = 10;
12+
int32_t sysconfig::m_consumings = 100;
1513
std::string sysconfig::m_xorkey;
16-
int32_t sysconfig::m_xorkeynum = 0;
17-
bool sysconfig::m_isxor = false;
18-
bool sysconfig::m_varint = false;
19-
bool sysconfig::m_robot_test = false;
20-
int32_t sysconfig::m_kcpping = 10;
21-
int32_t sysconfig::m_kcppinginterval= 20;
22-
int32_t sysconfig::m_sessionewait = 1;
14+
int32_t sysconfig::m_xorkeynum = 0;
15+
bool sysconfig::m_isxor = false;
16+
bool sysconfig::m_varint = false;
17+
bool sysconfig::m_robot_test = false;
18+
int32_t sysconfig::m_kcpping = 10;
19+
int32_t sysconfig::m_kcppinginterval = 20;
20+
int32_t sysconfig::m_sessionewait = 1;
2321
std::string sysconfig::m_kcpsession;
24-
int32_t sysconfig::m_open_servertime = 0;
25-
int32_t sysconfig::m_head_version = 1;
26-
int32_t sysconfig::m_rate_interval = 1;
27-
int32_t sysconfig::m_rate_count = 20;
22+
int32_t sysconfig::m_open_servertime = 0;
23+
int32_t sysconfig::m_head_version = 1;
24+
int32_t sysconfig::m_rate_interval = 1;
25+
int32_t sysconfig::m_rate_count = 20;
2826
int32_t sysconfig::m_heart_beat_interval = 10;
29-
int32_t sysconfig::m_net_timeout = 600000;
27+
int32_t sysconfig::m_net_timeout = 600000;
3028
std::string sysconfig::m_gmurl;
3129
std::vector<i32_serverid> sysconfig::m_gatewayids;
3230

3331
void sysconfig::init()
3432
{
3533
ngl::xmlinfo* lpublicxml = ngl::xmlnode::get_publicconfig();
36-
lpublicxml->find("logiswrite", m_logiswrite);
3734
lpublicxml->find("logflushtime", m_logflushtime);
3835
lpublicxml->find("logline", m_logline);
39-
lpublicxml->find("logconsole", m_logconsole);
40-
int llevel = 0;
41-
lpublicxml->find("loglevel", llevel);
42-
m_loglevel = (ELOGLEVEL)llevel;
43-
36+
{
37+
int llevel = 0;
38+
lpublicxml->find("logwritelevel", llevel);
39+
m_logwritelevel = (ELOGLEVEL)llevel;
40+
lpublicxml->find("logconsolelevel", llevel);
41+
m_logconsolelevel = (ELOGLEVEL)llevel;
42+
}
43+
4444
lpublicxml->find("consumings", m_consumings);
4545

4646
do

public/cpp/tools/tab/xml/sysconfig.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ namespace ngl
1818
{
1919
private:
2020
//##### 日志相关
21-
static ELOGLEVEL m_loglevel; // 日志等级
21+
static ELOGLEVEL m_logwritelevel; // 日志等级(写入文件)
22+
static ELOGLEVEL m_logconsolelevel; // 日志等级(控制台显示)
2223
static int32_t m_logline; // 单个日志文件的行数
23-
static int32_t m_logflushtime; // 日志flush时间
24-
static bool m_logiswrite; // 日志是否写入文件
25-
static bool m_logconsole; // 是否打印到控制台
24+
static int32_t m_logflushtime; // 日志写入文件的间隔时间
2625

2726
static int32_t m_consumings; // 检测actor消息耗时
2827

@@ -56,15 +55,14 @@ namespace ngl
5655
public:
5756
static void init();
5857

59-
static ELOGLEVEL loglevel() { return m_loglevel; }
60-
static int32_t logline() { return m_logline; }
61-
static int32_t logflushtime() { return m_logflushtime; }
62-
static bool logiswrite() { return m_logiswrite; }
63-
static bool logconsole() { return m_logconsole; }
64-
static int32_t consumings() { return m_consumings; }
65-
static std::string& xorkey() { return m_xorkey; }
66-
static int32_t xorkeynum() { return m_xorkeynum; }
67-
static bool isxor() { return m_isxor; }
58+
static int32_t logline() { return m_logline; }
59+
static int32_t logflushtime() { return m_logflushtime; }
60+
static int32_t logwritelevel() { return m_logwritelevel; }
61+
static int32_t logconsolelevel() { return m_logconsolelevel; }
62+
static int32_t consumings() { return m_consumings; }
63+
static std::string& xorkey() { return m_xorkey; }
64+
static int32_t xorkeynum() { return m_xorkeynum; }
65+
static bool isxor() { return m_isxor; }
6866
static bool varint() { return m_varint; }
6967
static bool robot_test() { return m_robot_test; }
7068
static int32_t kcpping() { return m_kcpping; }

0 commit comments

Comments
 (0)