From 9a25563a04180b4accfc59ebf0a5206f4161ce3a Mon Sep 17 00:00:00 2001 From: kai-ma Date: Fri, 11 Jul 2025 14:30:48 +0800 Subject: [PATCH] add csrc/utils --- msprobe/msprobe/csrc/CMakeLists.txt | 48 +++++++ msprobe/msprobe/csrc/utils/Constant.h | 28 ++++ msprobe/msprobe/csrc/utils/Exception.h | 30 +++++ msprobe/msprobe/csrc/utils/Log.h | 169 +++++++++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 msprobe/msprobe/csrc/CMakeLists.txt create mode 100644 msprobe/msprobe/csrc/utils/Constant.h create mode 100644 msprobe/msprobe/csrc/utils/Exception.h create mode 100644 msprobe/msprobe/csrc/utils/Log.h diff --git a/msprobe/msprobe/csrc/CMakeLists.txt b/msprobe/msprobe/csrc/CMakeLists.txt new file mode 100644 index 00000000000..03d7686ca95 --- /dev/null +++ b/msprobe/msprobe/csrc/CMakeLists.txt @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.14) +cmake_policy(SET CMP0048 NEW) +project(msprobe VERSION 1.0.0 LANGUAGES CXX C) + +set(CMAKE_VERBOSE_MAKEFILE ON) + +find_package(cpython MODULE REQUIRED) + +add_library(msprobe_c SHARED) + +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +target_compile_definitions(msprobe_c PRIVATE _GLIBCXX_USE_CXX11_ABI=0) + +# 安全编译选项 +target_compile_options(msprobe_c PRIVATE "-Wall") +target_compile_options(msprobe_c PRIVATE "-fPIC") +target_compile_options(msprobe_c PRIVATE "-fstack-protector-all") +target_compile_options(msprobe_c PRIVATE "-ftrapv") +target_compile_options(msprobe_c PRIVATE "-D_FORTIFY_SOURCE=2") + +target_link_options(msprobe_c PRIVATE "-Wl,-z,relor") +target_link_options(msprobe_c PRIVATE "-Wl,-z,now") +target_link_options(msprobe_c PRIVATE "-Wl,-z,noexecstack") +target_link_options(msprobe_c PRIVATE "-Wl,--disable-new-dtags") +target_link_options(msprobe_c PRIVATE "-s") + +target_link_libraries(msprobe_c PUBLIC dl) +target_link_libraries(msprobe_c PUBLIC pthread) +target_link_libraries(msprobe_c PUBLIC ${cpython_LIBRARIES}) + +if(DEFINED BUILD_TYPE AND "${BUILD_TYPE}" STREQUAL "debug") + target_compile_options(msprobe_c PRIVATE "-O0") + target_compile_options(msprobe_c PRIVATE "-g") + target_compile_definitions(msprobe_c PRIVATE __DEBUG__) +else() + target_compile_options(msprobe_c PRIVATE "-O2") +endif() + +target_include_directories(msprobe_c BEFORE PRIVATE /usr/include) +target_include_directories(msprobe_c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + +get_target_property(INCLUDE_DIRS msprobe_c INCLUDE_DIRECTORIES) +message(STATUS "msprobe_c include dirs: ${INCLUDE_DIRS}") + +file(GLOB_RECURSE SOURCES "*.cpp") +target_sources(msprobe_c PRIVATE ${SOURCES}) + +install(TARGETS msprobe_c LIBRARY DESTINATION lib) diff --git a/msprobe/msprobe/csrc/utils/Constant.h b/msprobe/msprobe/csrc/utils/Constant.h new file mode 100644 index 00000000000..f0a6190f156 --- /dev/null +++ b/msprobe/msprobe/csrc/utils/Constant.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONST_H +#define CONST_H + +#include + +namespace Cst { + constexpr uint8_t ARGS_LEN_2 = 2; + constexpr uint8_t ARGS_LEN_3 = 3; + constexpr uint8_t ARGS_LEN_5 = 5; +} // namespace Cst + +#endif diff --git a/msprobe/msprobe/csrc/utils/Exception.h b/msprobe/msprobe/csrc/utils/Exception.h new file mode 100644 index 00000000000..4eb65006b6d --- /dev/null +++ b/msprobe/msprobe/csrc/utils/Exception.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef EXCEPTION_H +#define EXCEPTION_H + +#include + +namespace Utility { + class MsprobeException : public std::runtime_error { + public: + explicit MsprobeException(const std::string &msg) : std::runtime_error("msprobe [ERROR]: " + msg) { + } + }; +} // namespace Utility + +#endif diff --git a/msprobe/msprobe/csrc/utils/Log.h b/msprobe/msprobe/csrc/utils/Log.h new file mode 100644 index 00000000000..6af72244588 --- /dev/null +++ b/msprobe/msprobe/csrc/utils/Log.h @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Utility { + class Log { + public: + static constexpr uint8_t BUF_SIZE = 32; + + enum class LogLevel { DEBUG = 0, INFO, WARNING, ERROR }; + + class LogStream { + public: + LogStream(LogLevel level, Log &logger) : level_(level), logger_(logger) { + } + + LogStream(const LogStream &) = delete; + LogStream &operator=(const LogStream &) = delete; + + LogStream(LogStream &&) = default; + LogStream &operator=(LogStream &&) = default; + + ~LogStream() { + if (logger_.CheckLogLevel(level_)) { + logger_.PrintLog(buffer_.str(), level_); + } + } + + template LogStream &operator<<(T &&arg) { + buffer_ << (std::forward(arg)); + return *this; + } + + private: + std::ostringstream buffer_; + LogLevel level_; + Log &logger_; + }; + + static Log &GetInstance() { + static Log instance; + return instance; + } + + LogStream DebugStream() { + return LogStream(LogLevel::DEBUG, *this); + } + + LogStream InfoStream() { + return LogStream(LogLevel::INFO, *this); + } + + LogStream WarningStream() { + return LogStream(LogLevel::WARNING, *this); + } + + LogStream ErrorStream() { + return LogStream(LogLevel::ERROR, *this); + } + + void SetLogLevel(int level) { + logLv_ = level; + } + + int GetLogLevel() const { + return logLv_; + } + + bool CheckLogLevel(LogLevel level) const { + return static_cast(level) >= logLv_; + } + + void PrintLog(const std::string &msg, LogLevel lv) { + std::lock_guard lock(logMutex_); + std::string filteredMsg = msg; + filterSpecialChar(filteredMsg); + char buf[BUF_SIZE]; + auto now = std::chrono::system_clock::now(); + std::time_t time = std::chrono::system_clock::to_time_t(now); + std::tm *tm = std::localtime(&time); + std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); + printf("%s (msprobe) (PID %ld) [%s] %s\n", buf, GetPid(), LogLevelString.at(lv), filteredMsg.c_str()); + fflush(stdout); + } + + private: + Log() = default; + ~Log() = default; + + static uint64_t GetPid() { + return static_cast(getpid()); + } + + void filterSpecialChar(std::string &msg) { + for (const auto &s : SpecialChar) { + size_t pos = 0; + while ((pos = msg.find(s, pos)) != std::string::npos) { + msg.replace(pos, s.length(), "_"); + pos += 1; + } + } + } + + std::mutex logMutex_; + int logLv_ = 1; + + const std::map LogLevelString = { + {LogLevel::DEBUG, "DEBUG"}, + {LogLevel::INFO, "INFO"}, + {LogLevel::WARNING, "WARNING"}, + {LogLevel::ERROR, "ERROR"}, + }; + + const std::unordered_set SpecialChar = { + "\n", + "\r", + "\u007f", + "\b", + "\f", + "\t", + "\v", + "\u000b", + "%08", + "%09", + "%0a", + "%0b", + "%0c", + "%0d", + "%7f", + "//", + "\\", + "&", + }; + }; + +#define LOG_DEBUG Utility::Log::GetInstance().DebugStream() +#define LOG_INFO Utility::Log::GetInstance().InfoStream() +#define LOG_WARNING Utility::Log::GetInstance().WarningStream() +#define LOG_ERROR Utility::Log::GetInstance().ErrorStream() +} // namespace Utility + +#endif // LOG_H -- Gitee