[ox/logconn] Change some C strings to StringViews, add macro for initializing logger

This commit is contained in:
Gary Talent 2023-06-26 23:58:10 -05:00
parent cf5786a4ed
commit 7755168004
3 changed files with 29 additions and 2 deletions

27
deps/ox/src/ox/logconn/def.hpp vendored Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 - 2023 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#if defined(DEBUG) && !defined(OX_BARE_METAL)
#define OX_INIT_DEBUG_LOGGER(loggerName, appName) \
ox::LoggerConn loggerName; \
{ \
const auto loggerErr = (loggerName).initConn(appName); \
if (loggerErr) { \
oxErrf("Could not connect to logger: {} ({}:{})\n", toStr(loggerErr), loggerErr.file, loggerErr.line); \
} else { \
ox::trace::setLogger(&(loggerName)); \
} \
ox::trace::init(); \
}
#else
#define OX_INIT_DEBUG_LOGGER(loggerName, appName)
#endif

View File

@ -34,7 +34,7 @@ LoggerConn::~LoggerConn() noexcept {
} }
} }
ox::Error LoggerConn::initConn(const char *appName) noexcept { ox::Error LoggerConn::initConn(ox::CRStringView appName) noexcept {
sockaddr_in addr{}; sockaddr_in addr{};
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

View File

@ -38,7 +38,7 @@ class LoggerConn: public trace::Logger {
LoggerConn &operator=(const LoggerConn&) noexcept = delete; LoggerConn &operator=(const LoggerConn&) noexcept = delete;
ox::Error send(const trace::TraceMsg&) noexcept final; ox::Error send(const trace::TraceMsg&) noexcept final;
ox::Error sendInit(const trace::InitTraceMsg&) noexcept final; ox::Error sendInit(const trace::InitTraceMsg&) noexcept final;
ox::Error initConn(const char *appName = "") noexcept; ox::Error initConn(ox::CRStringView appName) noexcept;
ox::Error send(const char *buff, std::size_t len) const noexcept; ox::Error send(const char *buff, std::size_t len) const noexcept;
private: private:
void msgSend() noexcept; void msgSend() noexcept;