Skip to content

Commit 219aaa4

Browse files
committed
feat(base):1.13.9,修改CatchThrow(),添加tag
1 parent aabbd6b commit 219aaa4

File tree

15 files changed

+163
-16
lines changed

15 files changed

+163
-16
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# .============.
3+
# // M A K E / \
4+
# // C++ DEV / \
5+
# // E A S Y / \/ \
6+
# ++ ----------. \/\ .
7+
# \\ \ \ /\ /
8+
# \\ \ \ /
9+
# \\ \ \ /
10+
# -============'
11+
#
12+
# Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
#
14+
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
# Use of this source code is governed by MIT license that can be found
16+
# in the LICENSE file in the root of the source tree. All contributing
17+
# project authors may be found in the CONTRIBUTORS.md file in the root
18+
# of the source tree.
19+
#
20+
21+
PROJECT := examples/main/07_stop_process
22+
EXE_NAME := ${PROJECT}
23+
24+
CPP_SRC_FILES = app.cpp main.cpp
25+
26+
CXXFLAGS := -DMODULE_ID='"$(EXE_NAME)"' $(CXXFLAGS)
27+
LDFLAGS += \
28+
-ltbox_main \
29+
-ltbox_coroutine \
30+
-ltbox_trace \
31+
-ltbox_terminal \
32+
-ltbox_network \
33+
-ltbox_eventx \
34+
-ltbox_eventx \
35+
-ltbox_event \
36+
-ltbox_log \
37+
-ltbox_util \
38+
-ltbox_base \
39+
-lpthread \
40+
-ldl \
41+
-rdynamic
42+
43+
include $(TOP_DIR)/mk/exe_common.mk
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
#include "app.h"
21+
#include <tbox/base/log.h>
22+
#include <tbox/main/main.h>
23+
24+
App::App(tbox::main::Context &ctx) :
25+
Module("app", ctx)
26+
{ }
27+
28+
bool App::onStart()
29+
{
30+
LogInfo("process will exit after 5 sec");
31+
ctx().timer_pool()->doAfter(std::chrono::seconds(5),
32+
[this] {
33+
tbox::main::RaiseStopSignal();
34+
}
35+
);
36+
return true;
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
#ifndef TBOX_MAIN_EXAMPLE_SAMPLE_H_20211226
21+
#define TBOX_MAIN_EXAMPLE_SAMPLE_H_20211226
22+
23+
#include <tbox/main/main.h>
24+
25+
class App : public tbox::main::Module
26+
{
27+
public:
28+
App(tbox::main::Context &ctx);
29+
30+
protected:
31+
virtual bool onStart() override;
32+
};
33+
34+
#endif //TBOX_MAIN_EXAMPLE_SAMPLE_H_20211226
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
#include <tbox/main/main.h>
21+
#include "app.h"
22+
23+
namespace tbox {
24+
namespace main {
25+
26+
void RegisterApps(Module &apps, Context &ctx)
27+
{
28+
apps.add(new ::App(ctx));
29+
}
30+
31+
}
32+
}

modules/base/catch_throw.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void CatchType()
5151

5252
}
5353

54-
bool CatchThrow(const std::function<void()> &func,
54+
bool CatchThrow(const std::function<void()> &func, const char *tag,
5555
bool print_backtrace, bool abort_process) noexcept
5656
{
5757
try {
@@ -61,22 +61,22 @@ bool CatchThrow(const std::function<void()> &func,
6161

6262
} catch (const std::exception &e) {
6363
CatchType();
64-
LogWarn("what(): %s", e.what());
64+
LogWarn("'%s' what(): %s", tag, e.what());
6565
} catch (const char *e) {
6666
CatchType();
67-
LogWarn("value: %s", e);
67+
LogWarn("'%s' value: %s", tag, e);
6868
} catch (int e) {
6969
CatchType();
70-
LogWarn("value: %d", e);
70+
LogWarn("'%s' value: %d", tag, e);
7171
} catch (double e) {
7272
CatchType();
73-
LogWarn("value: %f", e);
73+
LogWarn("'%s' value: %f", tag, e);
7474
} catch (const std::string &e) {
7575
CatchType();
76-
LogWarn("value: %s", e.c_str());
76+
LogWarn("'%s' value: %s", tag, e.c_str());
7777
} catch (...) {
7878
CatchType();
79-
LogWarn("can't print value");
79+
LogWarn("'%s' can't print value", tag);
8080
}
8181

8282
if (print_backtrace) {

modules/base/catch_throw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace tbox {
3434
* \return true 运行过程中捕获到了异常
3535
*/
3636
bool CatchThrow(const std::function<void()> &func,
37+
const char *tag = "?",
3738
bool print_backtrace = false,
3839
bool abort_process = false) noexcept;
3940

modules/base/catch_throw_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ TEST(CatchThrow, ThrowPrintStack)
114114

115115
bool has_catch = CatchThrow([&]{
116116
throw 10;
117-
}, true);
117+
}, "ThrowPrintStack", true);
118118

119119
EXPECT_TRUE(has_catch);
120120

modules/eventx/thread_pool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ void ThreadPool::threadProc(ThreadToken thread_token)
360360

361361
{
362362
RECORD_SCOPE();
363-
CatchThrow(item->backend_task, true);
363+
CatchThrow(item->backend_task, "tbox::eventx::ThreadPool", true);
364364
}
365365

366366
auto exec_time_cost = Clock::now() - exec_time_point;

modules/eventx/work_thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void WorkThread::threadProc()
236236

237237
{
238238
RECORD_SCOPE();
239-
CatchThrow(item->backend_task, true);
239+
CatchThrow(item->backend_task, "tbox::eventx::WorkThread", true);
240240
}
241241

242242
auto exec_time_cost = Clock::now() - exec_time_point;

modules/jsonrpc/protos/header_stream_proto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ ssize_t HeaderStreamProto::onRecvData(const void *data_ptr, size_t data_size)
8383
LogTrace("%s recv: %s", log_label_.c_str(), json_text.c_str());
8484

8585
Json js;
86-
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(json_text); });
86+
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(json_text); }, "tbox::jsonrpc::HeaderStreamProto");
8787
if (is_throw) {
8888
LogNotice("parse json fail");
8989
return -1;

0 commit comments

Comments
 (0)