Skip to content

Commit 62e5be1

Browse files
authored
Code specification (#3463)
* format , logger point * classname toupper * fix tests * fix tests [2]
1 parent f72b2b8 commit 62e5be1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+499
-532
lines changed

.clang-format

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
Language: Cpp
33
# BasedOnStyle: Google
4-
AccessModifierOffset: -1
4+
AccessModifierOffset: -2
55
AlignAfterOpenBracket: Align
66
AlignConsecutiveAssignments: false
77
AlignConsecutiveDeclarations: false
@@ -11,7 +11,7 @@ AlignTrailingComments: true
1111
AllowAllParametersOfDeclarationOnNextLine: true
1212
AllowShortBlocksOnASingleLine: false
1313
AllowShortCaseLabelsOnASingleLine: false
14-
AllowShortFunctionsOnASingleLine: Inline
14+
AllowShortFunctionsOnASingleLine: Empty
1515
AllowShortIfStatementsOnASingleLine: true
1616
AllowShortLoopsOnASingleLine: true
1717
AlwaysBreakAfterDefinitionReturnType: None

.editorconfig

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://editorconfig.org/
2+
3+
root = true
4+
5+
[*]
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
end_of_line = lf
9+
charset = utf-8
10+
indent_style = space
11+
tab_width = 4
12+
13+
[{*.{awk,sh},Makefile*}]
14+
indent_size = 4
15+
indent_style = table
16+
max_line_length = 80
17+
18+
[*.{c,cc,cpp,h,html,inc,php,phpt}]
19+
indent_size = 4
20+
indent_style = space
21+
max_line_length = 120
22+
23+
[*.md]
24+
indent_style = space
25+
max_line_length = 120
26+
27+
[COMMIT_EDITMSG]
28+
indent_size = 4
29+
indent_style = space
30+
max_line_length = 80
31+

core-tests/src/core/log.cpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ using namespace swoole;
77
const char *file = "/tmp/swoole_log_test.log";
88

99
TEST(log, level) {
10-
sw_logger().reset();
11-
sw_logger().set_level(SW_LOG_NOTICE);
12-
sw_logger().open(file);
10+
sw_logger()->reset();
11+
sw_logger()->set_level(SW_LOG_NOTICE);
12+
sw_logger()->open(file);
1313

14-
sw_logger().put(SW_LOG_INFO, SW_STRL("hello info"));
15-
sw_logger().put(SW_LOG_NOTICE, SW_STRL("hello notice"));
16-
sw_logger().put(SW_LOG_WARNING, SW_STRL("hello warning"));
14+
sw_logger()->put(SW_LOG_INFO, SW_STRL("hello info"));
15+
sw_logger()->put(SW_LOG_NOTICE, SW_STRL("hello notice"));
16+
sw_logger()->put(SW_LOG_WARNING, SW_STRL("hello warning"));
1717

1818
swoole::String content(swoole_file_get_contents(file));
1919

20-
sw_logger().close();
20+
sw_logger()->close();
2121
unlink(file);
2222

2323
ASSERT_FALSE(swString_contains(content.get(), SW_STRL("hello info")));
@@ -26,14 +26,14 @@ TEST(log, level) {
2626
}
2727

2828
TEST(log, date_format) {
29-
sw_logger().reset();
30-
sw_logger().set_date_format("day %d of %B in the year %Y. Time: %I:%S %p");
31-
sw_logger().open(file);
29+
sw_logger()->reset();
30+
sw_logger()->set_date_format("day %d of %B in the year %Y. Time: %I:%S %p");
31+
sw_logger()->open(file);
3232

33-
sw_logger().put(SW_LOG_WARNING, SW_STRL("hello world"));
33+
sw_logger()->put(SW_LOG_WARNING, SW_STRL("hello world"));
3434
swoole::String content(swoole_file_get_contents(file));
3535

36-
sw_logger().close();
36+
sw_logger()->close();
3737
unlink(file);
3838

3939
int data[16];
@@ -55,48 +55,48 @@ TEST(log, date_format) {
5555
}
5656

5757
TEST(log, date_format_long_string) {
58-
sw_logger().reset();
59-
sw_logger().set_level(SW_LOG_ERROR);
58+
sw_logger()->reset();
59+
sw_logger()->set_level(SW_LOG_ERROR);
6060
swoole::String content(swString_new(256));
6161
auto str = content.get();
6262

6363
swString_repeat(str, "x", 1, 120);
6464
swString_append_ptr(str, SW_STRL("day %d of %B in the year %Y. Time: %I:%S %p"));
6565

66-
int retval = sw_logger().set_date_format(str->str);
66+
int retval = sw_logger()->set_date_format(str->str);
6767

6868
ASSERT_EQ(retval, SW_ERR);
6969
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_INVALID_PARAMS);
7070
}
7171

7272
TEST(log, date_with_microseconds) {
73-
sw_logger().reset();
74-
sw_logger().set_date_with_microseconds(true);
75-
sw_logger().open(file);
73+
sw_logger()->reset();
74+
sw_logger()->set_date_with_microseconds(true);
75+
sw_logger()->open(file);
7676

77-
sw_logger().put(SW_LOG_WARNING, SW_STRL("hello world"));
77+
sw_logger()->put(SW_LOG_WARNING, SW_STRL("hello world"));
7878
swoole::String content(swoole_file_get_contents(file));
7979

80-
sw_logger().close();
80+
sw_logger()->close();
8181
unlink(file);
8282

8383
std::regex e("\\[\\S+\\s\\d{2}:\\d{2}:\\d{2}\\<\\.(\\d+)\\>\\s@\\d+\\.\\d+\\]\tWARNING\thello world");
8484
ASSERT_TRUE(std::regex_search(content.value(), e));
8585
}
8686

8787
TEST(log, rotation) {
88-
sw_logger().reset();
89-
sw_logger().set_rotation(SW_LOG_ROTATION_DAILY);
90-
sw_logger().open(file);
88+
sw_logger()->reset();
89+
sw_logger()->set_rotation(SW_LOG_ROTATION_DAILY);
90+
sw_logger()->open(file);
9191

92-
sw_logger().put(SW_LOG_WARNING, SW_STRL("hello world"));
92+
sw_logger()->put(SW_LOG_WARNING, SW_STRL("hello world"));
9393

94-
ASSERT_EQ(access(sw_logger().get_file(), R_OK), -1);
94+
ASSERT_EQ(access(sw_logger()->get_file(), R_OK), -1);
9595
ASSERT_EQ(errno, ENOENT);
96-
ASSERT_EQ(access(sw_logger().get_real_file(), R_OK), 0);
96+
ASSERT_EQ(access(sw_logger()->get_real_file(), R_OK), 0);
9797

98-
sw_logger().close();
99-
unlink(sw_logger().get_real_file());
98+
sw_logger()->close();
99+
unlink(sw_logger()->get_real_file());
100100
}
101101

102102
TEST(log, redirect) {
@@ -106,20 +106,20 @@ TEST(log, redirect) {
106106
return;
107107
}
108108

109-
sw_logger().reset();
110-
retval = sw_logger().open(file);
109+
sw_logger()->reset();
110+
retval = sw_logger()->open(file);
111111
ASSERT_EQ(retval, SW_OK);
112112

113-
retval = sw_logger().redirect_stdout_and_stderr(1);
113+
retval = sw_logger()->redirect_stdout_and_stderr(1);
114114
ASSERT_EQ(retval, SW_OK);
115115
printf("hello world\n");
116116
swoole::String content(swoole_file_get_contents(file));
117117
ASSERT_NE(content.get(), nullptr);
118118

119-
sw_logger().close();
120-
retval = sw_logger().redirect_stdout_and_stderr(0);
119+
sw_logger()->close();
120+
retval = sw_logger()->redirect_stdout_and_stderr(0);
121121
ASSERT_EQ(retval, SW_OK);
122-
unlink(sw_logger().get_real_file());
122+
unlink(sw_logger()->get_real_file());
123123

124124
ASSERT_TRUE(swString_contains(content.get(), SW_STRL("hello world\n")));
125125
}

core-tests/src/memory/lru_cache.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LRUCache cache(2);
1313

1414
int dtor_num = 0;
1515
class lru_cache_test_class {
16-
public:
16+
public:
1717
lru_cache_test_class() {}
1818

1919
~lru_cache_test_class() { ++dtor_num; }

core-tests/src/memory/table.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@ struct row_t {
3939
};
4040

4141
class table_t {
42-
private:
42+
private:
4343
swTableColumn *column_id;
4444
swTableColumn *column_name;
4545
swTableColumn *column_score;
4646

4747
swTable *table;
4848

49-
public:
49+
public:
5050
table_t(uint32_t rows_size, float conflict_proportion = 0.2) {
5151
table = swTable_new(rows_size, conflict_proportion);
5252
if (swTable_create(table), 0) {
53-
throw exception_t("alloc failed", SwooleG.error);
53+
throw exception_t("alloc failed", swoole_get_last_error());
5454
}
5555

5656
swTableColumn_add(table, "id", SW_TABLE_INT, 0);
5757
swTableColumn_add(table, "name", SW_TABLE_STRING, 32);
5858
swTableColumn_add(table, "score", SW_TABLE_FLOAT, 0);
5959

6060
if (swTable_create(table) < 0) {
61-
throw exception_t("create failed", SwooleG.error);
61+
throw exception_t("create failed", swoole_get_last_error());
6262
}
6363
column_id = swTableColumn_get(table, std::string("id"));
6464
column_name = swTableColumn_get(table, std::string("name"));

core-tests/src/network/stream.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ TEST(stream, send) {
2727
swServer serv;
2828
serv.worker_num = 1;
2929
serv.factory_mode = SW_MODE_BASE;
30-
int ori_log_level = sw_logger().get_level();
31-
sw_logger().set_level(SW_LOG_ERROR);
30+
int ori_log_level = sw_logger()->get_level();
31+
sw_logger()->set_level(SW_LOG_ERROR);
3232

3333
swListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, TEST_PORT);
3434
if (!port) {
@@ -104,5 +104,5 @@ TEST(stream, send) {
104104
serv.start();
105105
t1.join();
106106

107-
sw_logger().set_level(ori_log_level);
107+
sw_logger()->set_level(ori_log_level);
108108
}

core-tests/src/os/timer.cpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ TEST(timer, sys) {
3131

3232
uint64_t ms1 = swoole::time<std::chrono::milliseconds>();
3333

34-
swoole_timer_add(20, 0, [](swTimer *, swTimer_node *) { timer1_count++; }, nullptr);
35-
36-
swoole_timer_add(100,
37-
1,
38-
[](swTimer *, swTimer_node *tnode) {
39-
timer2_count++;
40-
if (timer2_count == 5) {
41-
swoole_timer_del(tnode);
42-
timer_running = false;
43-
}
44-
},
45-
nullptr);
34+
swoole_timer_add(
35+
20, 0, [](swTimer *, swTimer_node *) { timer1_count++; }, nullptr);
36+
37+
swoole_timer_add(
38+
100,
39+
1,
40+
[](swTimer *, swTimer_node *tnode) {
41+
timer2_count++;
42+
if (timer2_count == 5) {
43+
swoole_timer_del(tnode);
44+
timer_running = false;
45+
}
46+
},
47+
nullptr);
4648

4749
while (1) {
4850
sleep(10);

core-tests/src/server/buffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ TEST(server, send_buffer) {
3030
swServer serv(SW_MODE_BASE);
3131
serv.worker_num = 1;
3232

33-
sw_logger().set_level(SW_LOG_WARNING);
33+
sw_logger()->set_level(SW_LOG_WARNING);
3434

3535
swListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
3636
if (!port) {

core-tests/src/server/http.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void test_run_server(function<void(swServer *)> fn) {
3535
serv.set_document_root(test::get_root_path());
3636
serv.add_static_handler_location("/examples");
3737

38-
sw_logger().set_level(SW_LOG_WARNING);
38+
sw_logger()->set_level(SW_LOG_WARNING);
3939

4040
swListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
4141
if (!port) {

core-tests/src/server/server.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ TEST(server, base) {
5454
serv.worker_num = 1;
5555
serv.factory_mode = SW_MODE_BASE;
5656

57-
sw_logger().set_level(SW_LOG_WARNING);
57+
sw_logger()->set_level(SW_LOG_WARNING);
5858

5959
swListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
6060
if (!port) {
@@ -106,7 +106,7 @@ TEST(server, process) {
106106

107107
SwooleG.running = 1;
108108

109-
sw_logger().set_level(SW_LOG_WARNING);
109+
sw_logger()->set_level(SW_LOG_WARNING);
110110

111111
swLock *lock = (swLock *) SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(*lock));
112112
swMutex_create(lock, 1);

examples/cpp/test_server.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ static int g_receive_count = 0;
2222
int main(int argc, char **argv) {
2323
swoole_init();
2424

25-
sw_logger().set_date_format("%F %T");
26-
sw_logger().set_date_with_microseconds(true);
25+
sw_logger()->set_date_format("%F %T");
26+
sw_logger()->set_date_with_microseconds(true);
2727

2828
swServer serv;
2929

include/channel.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum Channel_flag {
3131
};
3232

3333
class Channel {
34-
private:
34+
private:
3535
off_t head;
3636
off_t tail;
3737
size_t size;
@@ -52,7 +52,7 @@ class Channel {
5252
swLock lock;
5353
swPipe *notify_pipe;
5454

55-
public:
55+
public:
5656
inline bool empty() { return num == 0; }
5757
inline bool full() { return ((head == tail && tail_tag != head_tag) || (bytes + sizeof(int) * num == size)); }
5858
int pop(void *out_buf, int buffer_length);
@@ -66,7 +66,7 @@ class Channel {
6666
void print();
6767
static Channel *make(size_t size, size_t maxlen, int flags);
6868

69-
private:
69+
private:
7070
Channel();
7171
~Channel();
7272
};

include/context.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef void (*coroutine_func_t)(void *);
4646

4747
namespace swoole {
4848
class Context {
49-
public:
49+
public:
5050
Context(size_t stack_size, coroutine_func_t fn, void *private_data);
5151
~Context();
5252
bool swap_in();
@@ -57,7 +57,7 @@ class Context {
5757
inline bool is_end() { return end_; }
5858
static void context_func(void *arg);
5959

60-
protected:
60+
protected:
6161
coroutine_func_t fn_;
6262
#ifdef SW_USE_THREAD_CONTEXT
6363
std::thread thread_;

include/coroutine.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct socket_poll_fd {
6969
};
7070

7171
class Coroutine {
72-
public:
72+
public:
7373
void resume();
7474
void yield();
7575

@@ -144,7 +144,7 @@ class Coroutine {
144144

145145
static void print_list();
146146

147-
protected:
147+
protected:
148148
static Coroutine *current;
149149
static long last_cid;
150150
static uint64_t peak_num;

0 commit comments

Comments
 (0)