Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syslog_sink should be a bit more open to modifications #3123

Closed
Silex opened this issue Jul 3, 2024 · 3 comments
Closed

syslog_sink should be a bit more open to modifications #3123

Silex opened this issue Jul 3, 2024 · 3 comments

Comments

@Silex
Copy link
Contributor

Silex commented Jul 3, 2024

Hello,

I'm using spdlog on a device where syslog is weirdly configured: no LOG_TRACE/LOG_DEBUG support. I cannot change this.

So my idea was to reuse https://github.com/gabime/spdlog/blob/v1.x/include/spdlog/sinks/syslog_sink.h like so

template <typename Mutex>
class axis_syslog_sink : public spdlog::sinks::syslog_sink<Mutex>
{

public:

  using spdlog::sinks::syslog_sink<Mutex>::syslog_sink; // Inherit constructors

protected:

  int syslog_prio_from_level(const spdlog::details::log_msg& msg) const {
    if (msg.level == spdlog::level::trace || msg.level == spdlog::level::debug)
      return LOG_INFO;
    return spdlog::sinks::syslog_sink<Mutex>::syslog_prio_from_level(msg);
  }

};

But it does not work because syslog_prio_from_level is not virtual. If syslog_levels_ was protected and/or modifiable from the public interface I could also make it work.

So, being stuck I had to copy-paste-adapt the whole class.

I'd understand if you don't really want to support this weird use case but with a tiny bit of modifications I could drop the custom class entirely.

@gabime
Copy link
Owner

gabime commented Jul 3, 2024

PR to mark syslog_prio_from_level as virtual is welcome

@Silex
Copy link
Contributor Author

Silex commented Jul 3, 2024

Continuing in #3124

@Silex Silex closed this as completed Jul 3, 2024
@Silex
Copy link
Contributor Author

Silex commented Jul 4, 2024

For those which it might help, here's the final class after the PR was accepted:

#pragma once

#include <mutex>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/syslog_sink.h>

// Custom syslog sink that logs debug and trace messages as LOG_INFO
template <typename Mutex>
struct axis_syslog_sink : public spdlog::sinks::syslog_sink<Mutex>
{
  axis_syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting);
};

template <typename Mutex>
inline axis_syslog_sink<Mutex>::axis_syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
: spdlog::sinks::syslog_sink<Mutex>(ident, syslog_option, syslog_facility, enable_formatting)
{
  auto& levels = spdlog::sinks::syslog_sink<Mutex>::syslog_levels_;
  levels[spdlog::level::trace] = LOG_INFO;
  levels[spdlog::level::debug] = LOG_INFO;
}

using axis_syslog_sink_mt = axis_syslog_sink<std::mutex>;
using axis_syslog_sink_st = axis_syslog_sink<spdlog::details::null_mutex>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants