Compare commits

...

4 Commits

Author SHA1 Message Date
SamareshSingh
cdbd64e230 Fix sign conversion warnings in qt_sinks.h (#3487)
Add static_cast to fix compiler warnings when building with
-Werror=sign-conversion and -Werror=shorten-64-to-32:

1. Cast msg.color_range_start/end to qsizetype when passing to
   QString::fromUtf8(), since QString expects signed qsizetype but
   the message fields are size_t (unsigned).

2. Cast msg.level to size_t when indexing into colors_ array, since
   level_enum is a signed int but array indexing expects size_t.

These casts are safe because:
- qsizetype is guaranteed to be the same size as size_t per Qt docs
- color_range values come from valid string positions
- level values are from a small enum range

Fixes #3321
2025-11-14 01:44:43 +02:00
fab4100
88a0e07ad5 Change access scope for ANSI target_file_ from private to protected (#3486)
This change allows for a custom minimal ANSI color sink implementation
that supports, for example, splitting between `stdout` and `stderr` file
streams depending on the log level.  An example application specific
custom sink that realizes this behavior would look like:

```cpp
template <typename ConsoleMutex>
class SplitSink : public sinks::ansicolor_sink<ConsoleMutex>
{
    using Base = sinks::ansicolor_sink<ConsoleMutex>;

public:
    SplitSink(color_mode mode = color_mode::automatic) : Base(stdout, mode) {}

    void log(const details::log_msg &msg) override
    {
        if (msg.level <= SPDLOG_LEVEL_WARN) {
            this->target_file_ = stdout;
        } else {
            this->target_file_ = stderr;
        }
        Base::log(msg);
    }
};
```

Inspired by https://github.com/gabime/spdlog/issues/345 and
https://github.com/eic/EICrecon/issues/456.  This commit aims at reusing
all of the `ansicolor_sink` code with the exception of dynamic target
file selection that can be implemented in application code based on
example above.

Co-authored-by: Fabian Wermelinger <info@0xfab.ch>
2025-11-09 19:38:28 +02:00
Aleksandr
3f7e502859 fix sign-compare warning (#3479) 2025-11-01 18:03:07 +02:00
Ashish Ahuja
dd3ca04a7a set CMAKE_BUILD_TYPE only in top-level project (#3480) 2025-11-01 17:55:59 +02:00
4 changed files with 12 additions and 6 deletions

View File

@@ -19,7 +19,11 @@ include(GNUInstallDirs)
# Set default build to release
# ---------------------------------------------------------------------------------------
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
# Set CMAKE_BUILD_TYPE only if this project is top-level
if((DEFINED PROJECT_IS_TOP_LEVEL AND PROJECT_IS_TOP_LEVEL)
OR (NOT DEFINED PROJECT_IS_TOP_LEVEL AND CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR))
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
endif()
# ---------------------------------------------------------------------------------------

View File

@@ -489,7 +489,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
result_size =
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size);
if (result_size > 0) {
assert(result_size == target.size());
assert(result_size == static_cast<int>(target.size()));
return;
}
}

View File

@@ -78,8 +78,10 @@ public:
const string_view_t red_bold = "\033[31m\033[1m";
const string_view_t bold_on_red = "\033[1m\033[41m";
private:
protected:
FILE *target_file_;
private:
mutex_t &mutex_;
bool should_do_colors_;
std::unique_ptr<spdlog::formatter> formatter_;

View File

@@ -160,8 +160,8 @@ protected:
payload = QString::fromUtf8(str.data(), static_cast<int>(str.size()));
// convert color ranges from byte index to character index.
if (msg.color_range_start < msg.color_range_end) {
color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size();
color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size();
color_range_start = QString::fromUtf8(str.data(), static_cast<qsizetype>(msg.color_range_start)).size();
color_range_end = QString::fromUtf8(str.data(), static_cast<qsizetype>(msg.color_range_end)).size();
}
} else {
payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
@@ -171,7 +171,7 @@ protected:
qt_text_edit_, // text edit to append to
std::move(payload), // text to append
default_color_, // default color
colors_.at(msg.level), // color to apply
colors_.at(static_cast<size_t>(msg.level)), // color to apply
color_range_start, // color range start
color_range_end}; // color range end