Skip to content

Commit 22595bd

Browse files
committed
feat(main,util):1.12.29
1. 添加util::json::Parse()函数,用于包装js = Json::parse()操作; 2. 添加FillDefaultConfig()函数,允许用户在程序中指定全局的默认配置; 3. 修改终端,将/apps/dump更名为/dump/apps; 4. 添加了终端命令:/dump/conf,用于打印当前的配置JSON;
1 parent 3280a55 commit 22595bd

File tree

8 files changed

+69
-28
lines changed

8 files changed

+69
-28
lines changed

modules/main/context_imp.cpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ bool ContextImp::initialize(const char* proc_name, const Json &cfg, Module *modu
115115
{
116116
TBOX_ASSERT(module != nullptr);
117117
module_ = module;
118+
js_conf_ = &cfg;
118119

119120
if (util::json::HasObjectField(cfg, "loop")) {
120121
auto &js_loop = cfg["loop"];
@@ -344,8 +345,9 @@ void ContextImp::initShell()
344345
util::string::Replace(txt, "\n", "\r\n");
345346
s.send(txt);
346347
(void)args;
347-
}
348-
, "print Loop's stat data");
348+
},
349+
"print Loop's stat data"
350+
);
349351
wp_nodes->mountNode(loop_stat_node, loop_stat_print_node, "print");
350352

351353
auto loop_stat_reset_node = wp_nodes->createFuncNode(
@@ -355,8 +357,9 @@ void ContextImp::initShell()
355357
ss << "done\r\n";
356358
s.send(ss.str());
357359
(void)args;
358-
}
359-
, "reset Loop's stat data");
360+
},
361+
"reset Loop's stat data"
362+
);
360363
wp_nodes->mountNode(loop_stat_node, loop_stat_reset_node, "reset");
361364

362365
{
@@ -365,8 +368,9 @@ void ContextImp::initShell()
365368
s.send(ToString(running_time()));
366369
s.send("\r\n");
367370
(void)args;
368-
}
369-
, "Print running times");
371+
},
372+
"Print running times"
373+
);
370374
wp_nodes->mountNode(ctx_node, func_node, "running_time");
371375
}
372376
}
@@ -387,8 +391,9 @@ void ContextImp::initShell()
387391

388392
s.send(ss.str());
389393
(void)args;
390-
}
391-
, "Print start time point");
394+
},
395+
"Print start time point"
396+
);
392397
wp_nodes->mountNode(ctx_node, func_node, "start_time");
393398
}
394399

@@ -414,8 +419,9 @@ void ContextImp::initShell()
414419
oss << "undo_task_peak_num: " << snapshot.undo_task_peak_num << "\r\n";
415420
s.send(oss.str());
416421
(void)args;
417-
}
418-
, "Print thread pool's snapshot");
422+
},
423+
"Print thread pool's snapshot"
424+
);
419425
wp_nodes->mountNode(threadpool_node, func_node, "snapshot");
420426
}
421427
}
@@ -433,8 +439,9 @@ void ContextImp::initShell()
433439
ss << 'v' << major << '.' << minor << '.' << rev << '_' << build << "\r\n";
434440
s.send(ss.str());
435441
(void)args;
436-
}
437-
, "Print app version");
442+
},
443+
"Print app version"
444+
);
438445
wp_nodes->mountNode(info_node, func_node, "app_ver");
439446
}
440447
{
@@ -446,8 +453,9 @@ void ContextImp::initShell()
446453
ss << 'v' << major << '.' << minor << '.' << rev << "\r\n";
447454
s.send(ss.str());
448455
(void)args;
449-
}
450-
, "Print tbox version");
456+
},
457+
"Print tbox version"
458+
);
451459
wp_nodes->mountNode(info_node, func_node, "tbox_ver");
452460
}
453461
{
@@ -457,8 +465,9 @@ void ContextImp::initShell()
457465
ss << GetAppBuildTime() << "\r\n";
458466
s.send(ss.str());
459467
(void)args;
460-
}
461-
, "Print buildtime");
468+
},
469+
"Print buildtime"
470+
);
462471
wp_nodes->mountNode(info_node, func_node, "build_time");
463472
}
464473
{
@@ -468,27 +477,34 @@ void ContextImp::initShell()
468477
ss << GetAppDescribe() << "\r\n";
469478
s.send(ss.str());
470479
(void)args;
471-
}
472-
, "Print app describe");
480+
},
481+
"Print app describe"
482+
);
473483
wp_nodes->mountNode(info_node, func_node, "what");
474484
}
475485
}
476486

477487
{
478-
auto apps_node = wp_nodes->createDirNode("Applications directory");
479-
wp_nodes->mountNode(wp_nodes->rootNode(), apps_node, "apps");
480-
488+
auto dump_node = wp_nodes->createDirNode("dump directory");
489+
wp_nodes->mountNode(wp_nodes->rootNode(), dump_node, "dump");
481490
auto func_node = wp_nodes->createFuncNode(
482-
[this] (const Session &s, const Args &) {
491+
[this] (const Session &s, const Args &a) {
483492
Json js;
484-
module_->toJson(js);
493+
if (util::string::IsEndWith(a[0], "apps")) {
494+
module_->toJson(js);
495+
} else if (util::string::IsEndWith(a[0], "conf")){
496+
js = *js_conf_;
497+
}
485498
auto json_str = js.dump(2);
486499
util::string::Replace(json_str, "\n", "\r\n");
487500
s.send(json_str);
488501
s.send("\r\n");
489-
}
490-
, "Dump apps as JSON");
491-
wp_nodes->mountNode(apps_node, func_node, "dump");
502+
},
503+
"Dump as JSON"
504+
);
505+
506+
wp_nodes->mountNode(dump_node, func_node, "apps");
507+
wp_nodes->mountNode(dump_node, func_node, "conf");
492508
}
493509
}
494510

modules/main/context_imp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class ContextImp : public Context {
7979

8080
std::chrono::steady_clock::time_point start_time_point_;
8181

82-
Module *module_ = nullptr;
82+
const Module *module_ = nullptr;
83+
const Json *js_conf_ = nullptr;
8384
};
8485

8586
}

modules/main/misc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void GetAppVersion(int &major, int &minor, int &rev, int &build)
8181
major = minor = rev = build = 0;
8282
}
8383

84+
__attribute__((weak)) void FillDefaultConfig(Json &) { }
85+
8486
__attribute__((weak)) void OnAbnormalExit() { }
8587

8688
void SayHi()

modules/main/run_in_backend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern void UninstallErrorSignals();
4545
extern void InstallTerminate();
4646

4747
extern void RegisterApps(Module &root, Context &ctx);
48+
extern void FillDefaultConfig(Json &js_conf);
4849

4950
extern void SayHi();
5051
extern void SayBye();
@@ -129,6 +130,7 @@ bool Start(int argc, char **argv)
129130
Args args(js_conf);
130131
Trace trace;
131132

133+
FillDefaultConfig(js_conf);
132134
log.fillDefaultConfig(js_conf);
133135
ctx.fillDefaultConfig(js_conf);
134136
trace.fillDefaultConfig(js_conf);

modules/main/run_in_frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern void UninstallErrorSignals();
4545
extern void InstallTerminate();
4646

4747
extern void RegisterApps(Module &root, Context &ctx);
48+
extern void FillDefaultConfig(Json &js_conf);
4849

4950
extern void SayHi();
5051
extern void SayBye();
@@ -113,6 +114,7 @@ int Main(int argc, char **argv)
113114
Args args(js_conf);
114115
Trace trace;
115116

117+
FillDefaultConfig(js_conf);
116118
log.fillDefaultConfig(js_conf);
117119
ctx.fillDefaultConfig(js_conf);
118120
trace.fillDefaultConfig(js_conf);

modules/util/json.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <fstream>
2323

2424
#include <tbox/base/json.hpp>
25+
#include <tbox/base/catch_throw.h>
2526
#include <tbox/base/assert.h>
2627

2728
namespace tbox {
@@ -166,6 +167,13 @@ bool HasStringField(const Json &js, const std::string &field_name)
166167
return js_field.is_string();
167168
}
168169

170+
bool Parse(const std::string &text, Json &js) noexcept
171+
{
172+
return !CatchThrow([&] {
173+
js = Json::parse(text);
174+
});
175+
}
176+
169177
Json Load(const std::string &filename)
170178
{
171179
Json js;

modules/util/json.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ struct ParseJsonFileError : public std::runtime_error {
7777
std::runtime_error("parse json file " + filename + " fail, detail:" + detail) { }
7878
};
7979

80+
/// 解析JSON字串
81+
/**
82+
* \param text JSON字串
83+
* \param js JSON对象
84+
*
85+
* \return true 成功
86+
* \return false 失败
87+
*/
88+
bool Parse(const std::string &text, Json &js) noexcept;
89+
8090
/// 加载JSON文件
8191
/**
8292
* \param filename JSON文件名

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 12
24-
TBOX_VERSION_REVISION := 28
24+
TBOX_VERSION_REVISION := 29

0 commit comments

Comments
 (0)