55
66#include < vsomeip/runtime.hpp>
77
8+ #ifdef __QNX__
9+ #include < sys/slog2.h>
10+ extern char * __progname;
11+ #elif __linux__
12+ extern char * __progname;
13+ #endif
14+
815#include " ../include/logger_impl.hpp"
916#include " ../../configuration/include/configuration.hpp"
1017
1118namespace vsomeip_v3 {
1219namespace logger {
1320
14- logger_impl::logger_impl () : config_{{false , false , false , level_e::LL_NONE}} { }
21+ logger_impl::logger_impl () : config_{{false , false , false , false , level_e::LL_NONE}} { }
22+
23+ #ifdef __QNX__
24+ slog2_buffer_set_config_t logger_impl::buffer_config = {0 , " main" , SLOG2_INFO, {" main" , 4 }, 1 };
25+ slog2_buffer_t logger_impl::buffer_handle[1 ] = {0 };
26+ #endif
1527
1628void logger_impl::init (const std::shared_ptr<configuration>& _configuration) {
1729 logger_impl::get ()->set_configuration (_configuration);
30+
31+ logger_impl::get ()->init_slog2 (_configuration);
1832}
1933
2034logger_impl::config logger_impl::get_configuration () const {
@@ -27,6 +41,7 @@ void logger_impl::set_configuration(const std::shared_ptr<configuration>& _confi
2741 config cfg; // NOLINT(cppcoreguidelines-pro-type-member-init)
2842 cfg.loglevel = _configuration->get_loglevel ();
2943 cfg.console_enabled = _configuration->has_console_log ();
44+ cfg.slog2_enabled = _configuration->has_slog2_log ();
3045 cfg.dlt_enabled = _configuration->has_dlt_log ();
3146 {
3247 std::scoped_lock its_lock{log_file_mutex_};
@@ -46,6 +61,62 @@ void logger_impl::log_to_file(std::string_view _msg) {
4661 }
4762}
4863
64+ auto logger_impl::init_slog2 (const std::shared_ptr<configuration>& _configuration) -> void {
65+ #ifdef __QNX__
66+ if (slog2_is_initialized_ || !_configuration)
67+ return ;
68+
69+ logger_impl::buffer_config.buffer_set_name = __progname;
70+ logger_impl::buffer_config.num_buffers = 1 ;
71+ logger_impl::buffer_config.verbosity_level = log_level_as_slog2 (_configuration->get_loglevel ());
72+
73+ // Use a 16kB log buffer by default
74+ // Override with a size specified by environment variable
75+ int num_pages = 4 ;
76+ auto s = getenv (" VSOMEIP_SLOG2_NUM_PAGES" );
77+ if (s != nullptr ) {
78+ char * endptr = nullptr ;
79+ errno = 0 ;
80+ auto const tmp_num_pages = strtoul (s, &endptr, 0 );
81+
82+ // Safety checks
83+ auto const at_least_one_digit_matched = endptr != s;
84+ auto const input_is_terminated = *endptr == ' \0 ' ;
85+ auto const no_error = errno == 0 ;
86+ auto const within_range = tmp_num_pages > 0 && tmp_num_pages < 1024 ; // 1024 pages = 4MB, hard to imagine this not being enough
87+
88+ if (at_least_one_digit_matched && input_is_terminated && no_error && within_range) {
89+ num_pages = static_cast <decltype (num_pages)>(tmp_num_pages);
90+ }
91+ }
92+
93+ logger_impl::buffer_config.buffer_config [0 ].buffer_name = " vsomeip" ;
94+ logger_impl::buffer_config.buffer_config [0 ].num_pages = num_pages;
95+
96+ // Register the buffer set.
97+ if (-1 == slog2_register (&logger_impl::buffer_config, logger_impl::buffer_handle, 0 )) {
98+ std::fprintf (stderr, " Error registering slogger2 buffer!\n " );
99+ return ;
100+ } else {
101+ slog2_is_initialized_ = true ;
102+ }
103+ #else
104+ static_cast <void >(_configuration);
105+ #endif
106+ }
107+
108+ void logger_impl::log_to_slog2 (level_e _level, std::string_view _msg) {
109+ #ifdef __QNX__
110+ auto const handle = logger_impl::buffer_handle[0 ];
111+ auto const user_code = 0 ;
112+
113+ slog2c (handle, user_code, log_level_as_slog2 (_level), _msg.data ());
114+ #else
115+ static_cast <void >(_level);
116+ static_cast <void >(_msg);
117+ #endif
118+ }
119+
49120#ifdef USE_DLT
50121#ifndef ANDROID
51122
0 commit comments