From 847bf3a0786384ff420359cf19999d805f9bf8af Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Sun, 18 Jan 2026 18:24:26 -0600 Subject: [PATCH] Dropping smartstreams (not fully cleared) and added get_pid() --- engine/NBCore/CMakeLists.txt | 1 + engine/NBCore/Logger.hpp | 27 +- engine/NBCore/Processes.hpp | 14 + engine/NBCore/SmartStreams.hpp | 292 +++--------------- engine/NBCore/src/Processes.cpp | 25 ++ engine/NBCore/tests/CMakeLists.txt | 2 +- .../CMakeLists.txt | 4 +- engine/NBCore/tests/ProcessTest/main.cpp | 17 + engine/NBCore/tests/SmartStreamTest/main.cpp | 28 -- 9 files changed, 114 insertions(+), 296 deletions(-) create mode 100644 engine/NBCore/Processes.hpp create mode 100644 engine/NBCore/src/Processes.cpp rename engine/NBCore/tests/{SmartStreamTest => ProcessTest}/CMakeLists.txt (52%) create mode 100644 engine/NBCore/tests/ProcessTest/main.cpp delete mode 100644 engine/NBCore/tests/SmartStreamTest/main.cpp diff --git a/engine/NBCore/CMakeLists.txt b/engine/NBCore/CMakeLists.txt index b65e891..5d329b2 100644 --- a/engine/NBCore/CMakeLists.txt +++ b/engine/NBCore/CMakeLists.txt @@ -2,6 +2,7 @@ include_directories(./.) add_library(NBCore ./src/Errors.cpp + ./src/Processes.cpp ) if (NB_BUILD_TESTS) diff --git a/engine/NBCore/Logger.hpp b/engine/NBCore/Logger.hpp index 09608b3..95ae814 100644 --- a/engine/NBCore/Logger.hpp +++ b/engine/NBCore/Logger.hpp @@ -8,8 +8,10 @@ #include #include +#include "Processes.hpp" #include "ThreadSafeQueue.hpp" + namespace nb { typedef std::chrono::time_point< @@ -21,6 +23,8 @@ struct LogEvent{ const LoggerTimePoint time; const unsigned char lvl; const std::string msg; + const std::thread::id tid; + const uint64_t pid; }; typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&); @@ -46,7 +50,13 @@ public: _queue.empty(); } void msg(const std::string& _msg, uint8_t lvl=0) { - _queue.push(LogEvent{std::chrono::system_clock::now(), lvl, _msg}); + _queue.push(LogEvent{ + std::chrono::system_clock::now(), + lvl, + _msg, + std::this_thread::get_id(), + get_pid() + }); } void warn(const std::string& _msg, uint8_t lvl=1) { msg(_msg, lvl); @@ -64,30 +74,15 @@ public: protected: virtual void process(const LogEvent& event) { uint8_t lvl = event.lvl; - try { - *_ostream << _function_map.at(lvl)(event.time, event.msg).c_str(); - } catch (std::out_of_range e) { - *_ostream << _function_map.at(0)(event.time, event.msg).c_str(); - //*_ostream << event.msg; - } *_ostream << "\n"; } - static const LogProcessFunctionMap _function_map; - ThreadsafeQueue _queue; StreamType* const _ostream; std::atomic _running; std::shared_ptr _runningThread = nullptr; }; -template -const LogProcessFunctionMap Logger::_function_map = { - {0, [](const LoggerTimePoint& tp, const std::string& msg){ - return msg; - }} -}; - template bool Logger::isRunning() const { return _running && bool(_runningThread); diff --git a/engine/NBCore/Processes.hpp b/engine/NBCore/Processes.hpp new file mode 100644 index 0000000..69b6e38 --- /dev/null +++ b/engine/NBCore/Processes.hpp @@ -0,0 +1,14 @@ +#pragma once +#ifndef _NB_PROCESSES +#define _NB_PROCESSES + +#include + +namespace nb { + +uint64_t get_pid(); + +} // namespace nb + + +#endif // _NB_PROCESSES \ No newline at end of file diff --git a/engine/NBCore/SmartStreams.hpp b/engine/NBCore/SmartStreams.hpp index 3fe0802..2496a44 100644 --- a/engine/NBCore/SmartStreams.hpp +++ b/engine/NBCore/SmartStreams.hpp @@ -16,12 +16,12 @@ namespace nb { template class SmartStream; -template +template struct MixinHasProcess { protected: template static auto test(int)->decltype( - M::process(std::declval(), std::declval()), + std::declval().template process(std::declval()), std::true_type{} ); @@ -32,12 +32,12 @@ public: static const bool value = decltype(test(0))::value; }; -template +template struct PackHasProcess; -template -struct PackHasProcess { - static const bool value = MixinHasProcess::value; +template +struct PackHasProcess { + static const bool value = MixinHasProcess::value; using type = typename std::conditional< value, Mix, @@ -45,53 +45,59 @@ struct PackHasProcess { >::type; }; -template -struct PackHasProcess { +template +struct PackHasProcess { protected: - static const bool base_value = MixinHasProcess::value; + static const bool base_value = MixinHasProcess::value; public: static const bool value = std::conditional< base_value, std::true_type, - typename PackHasProcess::value + typename PackHasProcess::value >::type::value; using type = typename std::conditional< base_value, Mix1, - typename PackHasProcess::type + typename PackHasProcess::type >::type; }; +/* template +struct HasStreamType : std::false_type {}; + +template +struct HasStreamType : std::false_type {}; */ + template -class SmartStream : public virtual Mixins... { +class SmartStream : public Mixins... { using StreamType = typename std::conditional< - std::is_base_of::value + decltype(std::declval(), std::true_type{}), + ST::StreamType, + ST >::type; protected: StreamType* const _stream; - + public: - SmartStream(StreamType& stream) : _stream(&stream) {} - - StreamType* getStream() const { return _stream; } - + SmartStream(StreamType& stream) : _stream(&stream), Mixins(&stream)... {} + using Mixins::process...; template > - static typename std::enable_if::value, void>::type - toStream(SmartStream& stream, const U& val, const StreamFmt* fmtas=nullptr) { - PackHasProcess::type::process(stream, val); + typename std::enable_if::value, void>::type + toStream(const U& val, const StreamFmt* fmtas=nullptr) { + PackHasProcess::type::template process(val); } template > - static typename std::enable_if::value, void>::type - toStream(SmartStream& stream, const U& val, const StreamFmt* fmtas=nullptr) { - *(stream._stream) << val; + typename std::enable_if::value, void>::type + toStream(const U& val, const StreamFmt* fmtas=nullptr) { + *_stream << val; } - template - friend SmartStream& operator<<(SmartStream& stream, const U& val) { - SmartStream::toStream(stream, val); + template + friend SmartStream& operator<<(SmartStream& stream, const U& val) { + stream.toStream(val); return stream; } }; @@ -100,231 +106,19 @@ struct SmartText { int x; }; -struct SmartStreamFormatter { - template - static void process(SmartStream& stream, const SmartText& val, StreamType* fmtas = nullptr) { - *(stream.getStream()) << val.x; +template +struct ANSIFormatter { + template + void process(const SmartText& val, StreamType* fmtas = nullptr) { + static_cast(this)->toStream(val.x); } + +protected: + ANSIFormatter(StreamType* stream) : _stream(stream) {} + + StreamType* const _stream; }; } -/*namespace nb { - -using RGB = std::array; - -class TerminalBufferController { - - template - friend void switchCoutBuf(T); -private: - static auto terminal_buffer = std::cout.rdbuf(); - TerminalBufferController(); -}; - -std::string toANSI(const RGB& color) { - return "\x1b[38;2;"+std::to_string(color[0])+";"+std::to_string(color[1])+ - ";"+std::to_string(color[2])+"m"; -} - -std::string toANSI(const unsigned int& color) { - return "\x1b[38;5;"+std::to_string(color)+"m"; -} - -template -void switchCoutBuf(T val) { - -} - -/* class TextStream { - virtual void write(std::) -}; - -struct SmartTextBase { - virtual std::string toString(std::ostream&) const = 0; - virtual ~SmartTextBase() = default; -}; - -template -struct SmartText; - -template -struct SmartText : SmartTextBase { - explicit SmartText(const U& val) : msg(val) {} - - SmartText(const SmartText&) = default; - - template - std::string toString(std::basic_ostream& stream) const { - return static_cast(*this).toString(stream); - } - std::string toString(std::ostream& stream) const override { - return static_cast(*this).toString(stream); - } - - U msg; -}; - -template -struct SmartText : SmartTextBase { - explicit SmartText(const U& val) : msg(val) {} - - SmartText(const SmartText&) = default; - - template - std::string toString(std::basic_ostream& stream) const { - std::stringstream sstr; - auto stream_buf = stream.rdbuf(sstr.rdbuf()); - stream << msg; - stream.rdbuf(stream_buf); - return sstr.str(); - } - std::string toString(std::ostream& stream) const override { - std::stringstream sstr; - auto stream_buf = stream.rdbuf(sstr.rdbuf()); - stream << msg; - stream.rdbuf(stream_buf); - return sstr.str(); - } - - U msg; -}; - -template -struct SmartText : SmartTextBase { - template - SmartText(char const(&val) [N]) : msg(val) {} - SmartText(const std::string& val) : msg(val) {} - SmartText(const char* val) : msg(val) {} - - SmartText(const SmartText&) = default; - - template - std::string toString(std::basic_ostream& stream) const { - return static_cast(*this).toString(stream); - } - std::string toString(std::ostream& stream) const override { - return static_cast(*this).toString(stream); - } - - std::string msg; -}; - -template <> -struct SmartText : SmartTextBase { - template - SmartText(char const(&val) [N]) : msg(val) {} - SmartText(const std::string& val) : msg(val) {} - SmartText(const char* val) : msg(val) {} - - SmartText(const SmartText&) = default; - - template - std::string toString(std::basic_ostream& stream) const { - std::stringstream sstr; - auto stream_buf = stream.rdbuf(sstr.rdbuf()); - stream << msg; - stream.rdbuf(stream_buf); - return sstr.str(); - } - std::string toString(std::ostream& stream) const override { - std::stringstream sstr; - auto stream_buf = stream.rdbuf(sstr.rdbuf()); - stream << msg; - stream.rdbuf(stream_buf); - return sstr.str(); - } - - std::string msg; -}; - -template -std::basic_ostream& operator<<(std::basic_ostream& stream, const SmartTextBase& smt) { - stream << smt.toString(stream); - return stream; -} - -template -struct TerminalColor : public SmartText> { - typedef SmartText> Base; - using Base::msg; - - explicit TerminalColor(unsigned int color, const U& val) - : Base(val), colorSequence(toANSI(color)) { } - explicit TerminalColor(const RGB& color, const U& val) - : Base(val), colorSequence(toANSI(color)) { } - - TerminalColor(const TerminalColor&) = default; - - const std::string colorSequence; - - template - std::string toString(std::basic_ostream& stream) const { - std::stringstream sstr; - auto stream_buf = stream.rdbuf(sstr.rdbuf()); - if (&stream == &std::cout) { - stream << colorSequence << msg << "\x1b[0m"; - } else { - stream << msg; - } - stream.rdbuf(stream_buf); - return sstr.str(); - } -}; - -template -std::vector> variadicToBaseVec(std::vector> vec, const U& val) { - static_assert(std::is_base_of::value, "Push value type must be base of vector base type."); - vec.push(static_cast>(std::make_shared(val))); - return vec; -} - -template -std::vector> variadicToBaseVec(std::vector> vec, const U&... val) { - vec.push(static_cast>(std::make_shared(val...))); - return variadicToBaseVec(vec, val...); -} - -struct SmartGroup : public SmartText>, SmartGroup> { - using SmartVec = std::vector>; - typedef SmartText Base; - using Base::msg; - - SmartGroup(const SmartGroup&) = default; - - template - explicit SmartGroup(const Types&... vals) - : Base(variadicToBaseVec({}, vals...)) {} - - template - void push(const T& val) { - msg = variadicToBaseVec(msg, val); - } - - template - SmartGroup operator+(const T& val) { - SmartGroup ret(*this); - ret.push(val); - return ret; - } - - template - std::string toString(std::basic_ostream& stream) const { - std::stringstream sstr; - auto stream_buf = stream.rdbuf(sstr.rdbuf()); - for (const std::shared_ptr& st : msg) { - stream << st; - } - stream.rdbuf(stream_buf); - return sstr.str(); - } - -}; - -template -SmartGroup operator+(const SmartText& lhs, const SmartText& rhs) { - return SmartGroup(lhs, rhs); -} - -} */ #endif // _NB_SMARTSTREAM \ No newline at end of file diff --git a/engine/NBCore/src/Processes.cpp b/engine/NBCore/src/Processes.cpp new file mode 100644 index 0000000..3eeabf3 --- /dev/null +++ b/engine/NBCore/src/Processes.cpp @@ -0,0 +1,25 @@ +#ifdef _TARGET_WINDOWS +#include +#endif // _TARGET_WINDOWS + +#ifdef _TARGET_LINUX +#include +#endif // _TARGET_LINUX + +#include "Processes.hpp" + +namespace nb { + +#ifdef _TARGET_WINDOWS +uint64_t get_pid() { + const auto pid_w = GetCurrentProcessId(); + uint64_t ret; + memcpy(&ret, &pid_w, sizeof(pid_w)); + return ret; +} +#endif // _TARGET_WINDOWS + +#ifdef _TARGET_LINUX +#endif // _TARGET_LINUX + +} // namespace nb \ No newline at end of file diff --git a/engine/NBCore/tests/CMakeLists.txt b/engine/NBCore/tests/CMakeLists.txt index 6536cab..54832c1 100644 --- a/engine/NBCore/tests/CMakeLists.txt +++ b/engine/NBCore/tests/CMakeLists.txt @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.26.0) project(gtest_Graphics VERSION 0.1.0 LANGUAGES C CXX) add_subdirectory(./ErrorTest) -add_subdirectory(./SmartStreamTest) \ No newline at end of file +add_subdirectory(./ProcessTest) \ No newline at end of file diff --git a/engine/NBCore/tests/SmartStreamTest/CMakeLists.txt b/engine/NBCore/tests/ProcessTest/CMakeLists.txt similarity index 52% rename from engine/NBCore/tests/SmartStreamTest/CMakeLists.txt rename to engine/NBCore/tests/ProcessTest/CMakeLists.txt index 5aca423..32ab7d8 100644 --- a/engine/NBCore/tests/SmartStreamTest/CMakeLists.txt +++ b/engine/NBCore/tests/ProcessTest/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.26.0) project(gtest_Graphics VERSION 0.1.0 LANGUAGES C CXX) -add_executable(SmartStreamTest main.cpp) -target_link_libraries(SmartStreamTest NBCore) \ No newline at end of file +add_executable(ProcessTest main.cpp) +target_link_libraries(ProcessTest NBCore) \ No newline at end of file diff --git a/engine/NBCore/tests/ProcessTest/main.cpp b/engine/NBCore/tests/ProcessTest/main.cpp new file mode 100644 index 0000000..f7ca564 --- /dev/null +++ b/engine/NBCore/tests/ProcessTest/main.cpp @@ -0,0 +1,17 @@ +#define CODE_ERROR_LOCATIONS + + +#include +#include "Processes.hpp" +#include + +int main() { + + std::cout << "nb::get_pid() -> " << nb::get_pid() << std::endl; + + #ifdef _TARGET_WINDOWS + std::cout << "GetCurrentProcessId() -> " << GetCurrentProcessId() << std::endl; + #endif // _TARGET_WINDOWS + + return 0; +} \ No newline at end of file diff --git a/engine/NBCore/tests/SmartStreamTest/main.cpp b/engine/NBCore/tests/SmartStreamTest/main.cpp deleted file mode 100644 index 7b67419..0000000 --- a/engine/NBCore/tests/SmartStreamTest/main.cpp +++ /dev/null @@ -1,28 +0,0 @@ - -#include -#include -#include - - -#include "SmartStreams.hpp" - - - -int main() { - - nb::SmartStream stout(std::cout); - - stout << nb::SmartText{5} << "\ngoop\n"; -/* nb::SmartText test(100); - - std::cout << test; - - std::ofstream file; - file.open("test.txt"); - if (file.is_open()) { - file << test; - } - file.close(); */ - - return 0; -} \ No newline at end of file