Skip to content

Commit

Permalink
style: lint codes
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Feb 1, 2024
1 parent 71140fe commit 0317e13
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CheckOptions:
readability-identifier-naming.ConstexprVariableCase: CamelCase
readability-identifier-naming.EnumCase: CamelCase
readability-identifier-naming.EnumConstantCase: CamelCase
readability-identifier-naming.FunctionCase: lower_case
readability-identifier-naming.FunctionCase: aNy_CasE
readability-identifier-naming.GlobalConstantCase: UPPER_CASE
readability-identifier-naming.GlobalConstantPointerCase: CamelCase
readability-identifier-naming.GlobalVariableCase: CamelCase
Expand Down
1 change: 0 additions & 1 deletion include/endstone/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class Plugin {

private:
friend class PluginLoader;
friend class PluginLoaderBase;

/**
* Sets the enabled state of this plugin
Expand Down
2 changes: 1 addition & 1 deletion include/endstone/plugin/plugin_description.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PluginDescription {
return prefix_;
}

inline const static std::regex ValidName{"^[A-Za-z0-9 _.-]+$"};
inline const static std::regex VALID_NAME{"^[A-Za-z0-9 _.-]+$"};

private:
std::string name_;
Expand Down
8 changes: 4 additions & 4 deletions include/endstone_core/spdlog/bedrock_log_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class BedrockLogSink : public spdlog::sinks::base_sink<spdlog::details::console_mutex::mutex_t> {
public:
explicit BedrockLogSink(FILE *target_file, spdlog::color_mode mode = spdlog::color_mode::automatic);
void set_color_mode(spdlog::color_mode mode);
void setColorMode(spdlog::color_mode mode);

protected:
void sink_it_(const spdlog::details::log_msg &msg) override;
Expand Down Expand Up @@ -68,9 +68,9 @@ class BedrockLogSink : public spdlog::sinks::base_sink<spdlog::details::console_
const spdlog::string_view_t bold_on_red = "\033[1m\033[41m";

private:
void print_ccode_(const spdlog::string_view_t &color_code);
void print_range_(const spdlog::memory_buf_t &formatted, size_t start, size_t end);
static std::string to_string(const spdlog::string_view_t &sv);
void printColorCode(const spdlog::string_view_t &color_code);
void printRange(const spdlog::memory_buf_t &formatted, size_t start, size_t end);
static std::string toString(const spdlog::string_view_t &sv);

FILE *target_file_;
bool should_do_colors_;
Expand Down
3 changes: 1 addition & 2 deletions python/src/endstone/_internal/bootstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys

import click

from endstone._internal.version import __version__, __version_tuple__

logging.basicConfig(level=logging.INFO, format="[%(asctime)s %(levelname)s] [%(name)s] %(message)s")
Expand Down Expand Up @@ -52,7 +51,7 @@ def wrapper(*args, **kwargs):
)
@click.version_option(__version__)
@catch_exceptions
def cli(install_folder: str, install: bool, remote: str) -> int:
def cli(install_folder: str, install: bool, remote: str) -> None:
"""
Starts an endstone server.
Expand Down
6 changes: 3 additions & 3 deletions python/src/endstone/_internal/bootstrap/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _linked_libpython_path(self) -> Path:
(Path): Path object representing the path of the linked libpython.
"""

class Dl_info(ctypes.Structure):
class DlInfo(ctypes.Structure):
# https://www.man7.org/linux/man-pages/man3/dladdr.3.html
_fields_ = [
("dli_fname", ctypes.c_char_p),
Expand All @@ -55,10 +55,10 @@ class Dl_info(ctypes.Structure):
]

libdl = ctypes.CDLL(ctypes.util.find_library("dl"))
libdl.dladdr.argtypes = [ctypes.c_void_p, ctypes.POINTER(Dl_info)]
libdl.dladdr.argtypes = [ctypes.c_void_p, ctypes.POINTER(DlInfo)]
libdl.dladdr.restype = ctypes.c_int

dlinfo = Dl_info()
dlinfo = DlInfo()
retcode = libdl.dladdr(ctypes.cast(ctypes.pythonapi.Py_GetVersion, ctypes.c_void_p), ctypes.pointer(dlinfo))
assert retcode != 0, ValueError(
"dladdr cannot match the address of ctypes.pythonapi.Py_GetVersion to a shared object"
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def test_import_endstone():
module = importlib.import_module("endstone")
importlib.import_module("endstone")


def test_import_server():
Expand Down
36 changes: 18 additions & 18 deletions src/endstone_core/spdlog/bedrock_log_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ BedrockLogSink::BedrockLogSink(FILE *target_file, spdlog::color_mode mode)
: target_file_(target_file), spdlog::sinks::base_sink<spdlog::details::console_mutex::mutex_t>(
spdlog::details::make_unique<spdlog::pattern_formatter>())
{
set_color_mode(mode);
setColorMode(mode);
auto *formatter = dynamic_cast<spdlog::pattern_formatter *>(formatter_.get());
formatter->add_flag<BedrockLevelFormatter>('L');
formatter->add_flag<BedrockTextFormatter>('v', should_do_colors_);
formatter->set_pattern("%^[%Y-%m-%d %H:%M:%S.%e %L] [%n] %v%$");
colors_.at(spdlog::level::trace) = to_string(white);
colors_.at(spdlog::level::debug) = to_string(cyan);
colors_.at(spdlog::level::info) = to_string(reset);
colors_.at(spdlog::level::warn) = to_string(yellow_bold);
colors_.at(spdlog::level::err) = to_string(red_bold);
colors_.at(spdlog::level::critical) = to_string(bold_on_red);
colors_.at(spdlog::level::off) = to_string(reset);
colors_.at(spdlog::level::trace) = toString(white);
colors_.at(spdlog::level::debug) = toString(cyan);
colors_.at(spdlog::level::info) = toString(reset);
colors_.at(spdlog::level::warn) = toString(yellow_bold);
colors_.at(spdlog::level::err) = toString(red_bold);
colors_.at(spdlog::level::critical) = toString(bold_on_red);
colors_.at(spdlog::level::off) = toString(reset);
}

void BedrockLogSink::set_color_mode(spdlog::color_mode mode)
void BedrockLogSink::setColorMode(spdlog::color_mode mode)
{
switch (mode) {
case spdlog::color_mode::always:
Expand All @@ -53,7 +53,7 @@ void BedrockLogSink::set_color_mode(spdlog::color_mode mode)
}
}

std::string BedrockLogSink::to_string(const spdlog::string_view_t &sv)
std::string BedrockLogSink::toString(const spdlog::string_view_t &sv)
{
return {sv.data(), sv.size()};
}
Expand All @@ -66,17 +66,17 @@ void BedrockLogSink::sink_it_(const spdlog::details::log_msg &msg)
formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range
print_range_(formatted, 0, msg.color_range_start);
printRange(formatted, 0, msg.color_range_start);
// in color range
print_ccode_(colors_.at(static_cast<size_t>(msg.level)));
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset);
printColorCode(colors_.at(static_cast<size_t>(msg.level)));
printRange(formatted, msg.color_range_start, msg.color_range_end);
printColorCode(reset);
// after color range
print_range_(formatted, msg.color_range_end, formatted.size());
printRange(formatted, msg.color_range_end, formatted.size());
}
else // no color
{
print_range_(formatted, 0, formatted.size());
printRange(formatted, 0, formatted.size());
}
fflush(target_file_);
}
Expand All @@ -86,12 +86,12 @@ void BedrockLogSink::flush_()
fflush(target_file_);
}

void BedrockLogSink::print_ccode_(const spdlog::string_view_t &color_code)
void BedrockLogSink::printColorCode(const spdlog::string_view_t &color_code)
{
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
}

void BedrockLogSink::print_range_(const spdlog::memory_buf_t &formatted, size_t start, size_t end)
void BedrockLogSink::printRange(const spdlog::memory_buf_t &formatted, size_t start, size_t end)
{
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
}
4 changes: 1 addition & 3 deletions src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

#include <pybind11/pybind11.h>

namespace py = pybind11;

PYBIND11_MODULE(endstone_python, m)
PYBIND11_MODULE(endstone_python, m) // NOLINT(*-use-anonymous-namespace)
{
def_color_format(m);
def_logger(m);
Expand Down
10 changes: 5 additions & 5 deletions src/endstone_runtime/bedrock/bedrock_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
#include "endstone/logger.h"
#include "endstone_core/logger_factory.h"

void BedrockLog::log_va(BedrockLog::LogCategory category, std::bitset<3> flags, BedrockLog::LogRule rule,
LogAreaID area, LogLevel level, const char *function, int line, const char *format,
void BedrockLog::log_va(BedrockLog::LogCategory /*category*/, std::bitset<3> /*flags*/, BedrockLog::LogRule /*rule*/,
LogAreaID area, LogLevel level, const char * /*function*/, int /*line*/, const char *format,
va_list args)
{
auto name = magic_enum::enum_name(area);
auto &logger = LoggerFactory::getLogger(std::string(name));

static const std::unordered_map<LogLevel, Logger::Level> LevelMapping = {
static const std::unordered_map<LogLevel, Logger::Level> log_levels = {
{1, Logger::Level::Debug},
{2, Logger::Level::Info},
{4, Logger::Level::Warning},
{8, Logger::Level::Error},
};

Logger::Level log_level = Logger::Level::Critical;
auto iter = LevelMapping.find(level);
if (iter != LevelMapping.end()) {
auto iter = log_levels.find(level);
if (iter != log_levels.end()) {
log_level = iter->second;
}

Expand Down
3 changes: 2 additions & 1 deletion src/endstone_runtime/bedrock/type_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
Bedrock::typeid_t<Context> Bedrock::type_id<Context, Type>() \
{ \
return {Value}; \
}
} \
[[maybe_unused]] void __##Context##__##Value()

DEFINE_BEDROCK_TYPE_ID(CommandRegistry, std::string, 1);
DEFINE_BEDROCK_TYPE_ID(CommandRegistry, bool, 2);
Expand Down
4 changes: 2 additions & 2 deletions src/endstone_runtime/windows/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ const std::error_category &hook_error_category() noexcept
return "Unknown error.";
}
}
} CATEGORY;
return CATEGORY;
} category;
return category;
}
} // namespace endstone::hook

Expand Down
12 changes: 0 additions & 12 deletions tests/test_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,14 @@ TEST_F(LoggerFactoryTest, GetLogger)
TEST_F(LoggerFactoryTest, SetLevel)
{
auto &logger = LoggerFactory::getLogger("TestLogger");
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Trace));
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Debug));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Info));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Warning));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Error));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Critical));

logger.setLevel(Logger::Level::Debug);
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Trace));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Debug));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Info));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Warning));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Error));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Critical));

logger.setLevel(Logger::Level::Error);
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Trace));
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Debug));
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Info));
ASSERT_FALSE(logger.isEnabledFor(Logger::Level::Warning));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Error));
ASSERT_TRUE(logger.isEnabledFor(Logger::Level::Critical));
}

0 comments on commit 0317e13

Please sign in to comment.