Skip to content

Commit 7d84705

Browse files
committed
opt(main): 1.12.0, 整理log的终端命令,用add_sink与del_sink替代add_stdout_sink, add_syslog_sink等
1 parent 4c2329e commit 7d84705

File tree

2 files changed

+125
-57
lines changed

2 files changed

+125
-57
lines changed

modules/main/log.cpp

Lines changed: 123 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -82,48 +82,14 @@ bool Log::initialize(const char *proc_name, Context &ctx, const Json &cfg)
8282
return true;
8383
}
8484

85-
void Log::initShell()
85+
void Log::cleanup()
8686
{
87-
auto log_node = terminal::AddDirNode(*shell_, shell_->rootNode(), "log", "This is log directory");
88-
sink_node_ = terminal::AddDirNode(*shell_, log_node, "sinks");
89-
90-
{
91-
terminal::IntegerFuncNodeProfile profile;
92-
profile.set_func = \
93-
[] (int max_len) {
94-
if (max_len > 0) {
95-
LogSetMaxLength(static_cast<size_t>(max_len));
96-
return true;
97-
}
98-
return false;
99-
};
100-
profile.get_func = [] { return LogGetMaxLength(); };
101-
profile.usage = "Usage: max_len # get max len, unit:byte\r\n"
102-
" max_len <len> # set max len, len>0\r\n";
103-
profile.help = "get or set log maxmum length";
104-
terminal::AddFuncNode(*shell_, log_node, "max_len", profile);
105-
}
106-
107-
terminal::AddFuncNode(*shell_, log_node, "add_stdout", [this] { installStdoutSink(); });
108-
terminal::AddFuncNode(*shell_, log_node, "add_syslog", [this] { installSyslogSink(); });
109-
terminal::AddFuncNode(*shell_, log_node, "del_stdout", [this] { uninstallStdoutSink(); });
110-
terminal::AddFuncNode(*shell_, log_node, "del_syslog", [this] { uninstallSyslogSink(); });
111-
112-
{
113-
terminal::StringFuncNodeProfile profile;
114-
profile.set_func = [this] (const std::string &name) {
115-
return installFileSink(name);
116-
};
117-
terminal::AddFuncNode(*shell_, log_node, "add_file", profile);
118-
}
87+
for (auto &item: file_sinks_)
88+
uninstallFileSink(item.second, item.first);
89+
file_sinks_.clear();
11990

120-
{
121-
terminal::StringFuncNodeProfile profile;
122-
profile.set_func = [this] (const std::string &name) {
123-
return uninstallFileSink(name);
124-
};
125-
terminal::AddFuncNode(*shell_, log_node, "del_file", profile);
126-
}
91+
uninstallSyslogSink();
92+
uninstallStdoutSink();
12793
}
12894

12995
bool Log::installStdoutSink()
@@ -148,9 +114,10 @@ bool Log::uninstallStdoutSink()
148114
if (stdout_sink_ == nullptr)
149115
return false;
150116

117+
stdout_sink_->sink.disable();
118+
151119
uninstallShellForSink(sink_node_, stdout_sink_->nodes, "stdout");
152120

153-
stdout_sink_->sink.disable();
154121
CHECK_DELETE_RESET_OBJ(stdout_sink_);
155122
return true;
156123
}
@@ -177,10 +144,11 @@ bool Log::uninstallSyslogSink()
177144
if (syslog_sink_ == nullptr)
178145
return false;
179146

180-
uninstallShellForSink(sink_node_, syslog_sink_->nodes, "syslog");
181-
182147
syslog_sink_->sink.disable();
183148
syslog_sink_->sink.cleanup();
149+
150+
uninstallShellForSink(sink_node_, syslog_sink_->nodes, "syslog");
151+
184152
CHECK_DELETE_RESET_OBJ(syslog_sink_);
185153
return true;
186154
}
@@ -241,22 +209,13 @@ bool Log::uninstallFileSink(const std::string &name)
241209

242210
bool Log::uninstallFileSink(FileSink *file_sink, const std::string &name)
243211
{
244-
uninstallShellForFileSink(file_sink->nodes, name);
245-
246212
file_sink->sink.disable();
247213
file_sink->sink.cleanup();
248-
CHECK_DELETE_OBJ(file_sink);
249-
return true;
250-
}
251214

252-
void Log::cleanup()
253-
{
254-
uninstallStdoutSink();
255-
uninstallSyslogSink();
215+
uninstallShellForFileSink(file_sink->nodes, name);
256216

257-
for (auto &item: file_sinks_)
258-
uninstallFileSink(item.second, item.first);
259-
file_sinks_.clear();
217+
CHECK_DELETE_OBJ(file_sink);
218+
return true;
260219
}
261220

262221
void Log::initSinkByJson(log::Sink &sink, const Json &js)
@@ -280,6 +239,115 @@ void Log::initSinkByJson(log::Sink &sink, const Json &js)
280239
}
281240
}
282241

242+
void Log::initShell()
243+
{
244+
auto log_node = terminal::AddDirNode(*shell_, shell_->rootNode(), "log", "This is log directory");
245+
sink_node_ = terminal::AddDirNode(*shell_, log_node, "sinks");
246+
247+
{
248+
terminal::IntegerFuncNodeProfile profile;
249+
profile.set_func = \
250+
[] (int max_len) {
251+
if (max_len > 0) {
252+
LogSetMaxLength(static_cast<size_t>(max_len));
253+
return true;
254+
}
255+
return false;
256+
};
257+
profile.get_func = [] { return LogGetMaxLength(); };
258+
profile.usage = "Usage: max_len # get max len, unit:byte\r\n"
259+
" max_len <len> # set max len, len>0\r\n";
260+
profile.help = "get or set log maxmum length";
261+
terminal::AddFuncNode(*shell_, log_node, "max_len", profile);
262+
}
263+
264+
{
265+
auto func_node = shell_->createFuncNode(
266+
[this] (const Session &s, const Args &a) {
267+
std::ostringstream oss;
268+
bool print_usage = true;
269+
if (a.size() == 2) {
270+
if (a[1] == "stdout") {
271+
print_usage = false;
272+
if (installStdoutSink())
273+
oss << "done.\r\n";
274+
else
275+
oss << "fail, already exist.\r\n";
276+
277+
} else if (a[1] == "syslog") {
278+
print_usage = false;
279+
if (installSyslogSink())
280+
oss << "done.\r\n";
281+
else
282+
oss << "fail, already exist.\r\n";
283+
}
284+
285+
} else if (a.size() == 3 && a[1] == "file") {
286+
print_usage = false;
287+
if (installFileSink(a[2]))
288+
oss << "done\r\n";
289+
else
290+
oss << "fail, already exist.\r\n";
291+
}
292+
293+
if (print_usage) {
294+
oss << "Install log sink.\r\n"
295+
<< "Usage: " << a[0] << " stdout # install stdout sink.\r\n"
296+
<< " " << a[0] << " syslog # install syslog sink.\r\n"
297+
<< " " << a[0] << " file <name> # install file sink by name.\r\n";
298+
}
299+
300+
s.send(oss.str());
301+
},
302+
"install log sink"
303+
);
304+
shell_->mountNode(log_node, func_node, "add_sink");
305+
}
306+
307+
{
308+
auto func_node = shell_->createFuncNode(
309+
[this] (const Session &s, const Args &a) {
310+
std::ostringstream oss;
311+
bool print_usage = true;
312+
if (a.size() == 2) {
313+
if (a[1] == "stdout") {
314+
print_usage = false;
315+
if (uninstallStdoutSink())
316+
oss << "done.\r\n";
317+
else
318+
oss << "fail, not exist.\r\n";
319+
320+
} else if (a[1] == "syslog") {
321+
print_usage = false;
322+
if (uninstallSyslogSink())
323+
oss << "done.\r\n";
324+
else
325+
oss << "fail, not exist.\r\n";
326+
}
327+
328+
} else if (a.size() == 3 && a[1] == "file") {
329+
print_usage = false;
330+
if (uninstallFileSink(a[2]))
331+
oss << "done.\r\n";
332+
else
333+
oss << "fail, not exist.\r\n";
334+
}
335+
336+
if (print_usage) {
337+
oss << "Uninstall log sink.\r\n"
338+
<< "Usage: " << a[0] << " stdout # uninstall stdout sink.\r\n"
339+
<< " " << a[0] << " syslog # uninstall syslog sink.\r\n"
340+
<< " " << a[0] << " file <name> # uninstall file sink by name.\r\n";
341+
}
342+
343+
s.send(oss.str());
344+
},
345+
"uninstall log sink"
346+
);
347+
shell_->mountNode(log_node, func_node, "del_sink");
348+
}
349+
}
350+
283351
void Log::installShellForSink(log::Sink &sink, terminal::NodeToken parent_node, SinkShellNodes &nodes, const std::string &name)
284352
{
285353
nodes.dir = terminal::AddDirNode(*shell_, parent_node, name);

version.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020

2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
23-
TBOX_VERSION_MINOR := 11
24-
TBOX_VERSION_REVISION := 14
23+
TBOX_VERSION_MINOR := 12
24+
TBOX_VERSION_REVISION := 0

0 commit comments

Comments
 (0)