updated uuid libs

This commit is contained in:
Proddy
2023-01-05 15:05:30 +01:00
parent 6370296c53
commit 3b196fc90d
27 changed files with 2013 additions and 1411 deletions

View File

@@ -1,6 +1,6 @@
/*
* uuid-log - Microcontroller logging framework
* Copyright 2019 Simon Arlott
* Copyright 2019,2021-2022 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -20,11 +20,15 @@
#include <Arduino.h>
#include <atomic>
#include <cstdarg>
#include <cstdint>
#include <list>
#include <map>
#include <memory>
#if UUID_LOG_THREAD_SAFE
#include <mutex>
#endif
#include <string>
#include <utility>
#include <vector>
@@ -33,8 +37,21 @@ namespace uuid {
namespace log {
std::map<Handler *, Level> Logger::handlers_;
Level Logger::level_ = Level::OFF;
std::atomic<Level> Logger::global_level_{Level::OFF};
#if UUID_LOG_THREAD_SAFE
std::mutex Logger::mutex_;
#endif
//! @cond false
static Level constrain_level(Level level) {
if (level < Level::EMERG) {
level = Level::EMERG;
} else if (level > Level::TRACE) {
level = Level::TRACE;
}
return level;
}
//! @endcond
Message::Message(uint64_t uptime_ms, Level level, Facility facility, const char * name, const std::string && text)
: uptime_ms(uptime_ms)
@@ -50,20 +67,46 @@ Logger::Logger(const char * name, Facility facility)
};
std::shared_ptr<std::map<Handler *, Level>> & Logger::registered_handlers() {
static std::shared_ptr<std::map<Handler *, Level>> handlers = std::make_shared<std::map<Handler *, Level>>();
return handlers;
}
void Logger::register_handler(Handler * handler, Level level) {
handlers_[handler] = level;
#if UUID_LOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
auto & handlers = registered_handlers();
handler->handlers_ = handlers;
(*handlers)[handler] = level;
refresh_log_level();
};
void Logger::unregister_handler(Handler * handler) {
handlers_.erase(handler);
refresh_log_level();
auto handlers = handler->handlers_.lock();
if (handlers) {
#if UUID_LOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
if (handlers->erase(handler)) {
refresh_log_level();
}
}
};
Level Logger::get_log_level(const Handler * handler) {
const auto level = handlers_.find(const_cast<Handler *>(handler));
#if UUID_LOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
auto & handlers = registered_handlers();
if (level != handlers_.end()) {
const auto level = handlers->find(const_cast<Handler *>(handler));
if (level != handlers->end()) {
return level->second;
}
@@ -80,16 +123,6 @@ void Logger::emerg(const char * format, ...) const {
}
};
void Logger::emerg(const __FlashStringHelper * format, ...) const {
if (enabled(Level::EMERG)) {
va_list ap;
va_start(ap, format);
vlog(Level::EMERG, format, ap);
va_end(ap);
}
};
void Logger::crit(const char * format, ...) const {
if (enabled(Level::CRIT)) {
va_list ap;
@@ -100,16 +133,6 @@ void Logger::crit(const char * format, ...) const {
}
};
void Logger::crit(const __FlashStringHelper * format, ...) const {
if (enabled(Level::CRIT)) {
va_list ap;
va_start(ap, format);
vlog(Level::CRIT, format, ap);
va_end(ap);
}
};
void Logger::alert(const char * format, ...) const {
if (enabled(Level::ALERT)) {
va_list ap;
@@ -120,15 +143,6 @@ void Logger::alert(const char * format, ...) const {
}
};
void Logger::alert(const __FlashStringHelper * format, ...) const {
if (enabled(Level::ALERT)) {
va_list ap;
va_start(ap, format);
vlog(Level::ALERT, format, ap);
va_end(ap);
}
};
void Logger::err(const char * format, ...) const {
if (enabled(Level::ERR)) {
va_list ap;
@@ -139,16 +153,6 @@ void Logger::err(const char * format, ...) const {
}
};
void Logger::err(const __FlashStringHelper * format, ...) const {
if (enabled(Level::ERR)) {
va_list ap;
va_start(ap, format);
vlog(Level::ERR, format, ap);
va_end(ap);
}
};
void Logger::warning(const char * format, ...) const {
if (enabled(Level::WARNING)) {
va_list ap;
@@ -159,16 +163,6 @@ void Logger::warning(const char * format, ...) const {
}
};
void Logger::warning(const __FlashStringHelper * format, ...) const {
if (enabled(Level::WARNING)) {
va_list ap;
va_start(ap, format);
vlog(Level::WARNING, format, ap);
va_end(ap);
}
};
void Logger::notice(const char * format, ...) const {
if (enabled(Level::NOTICE)) {
va_list ap;
@@ -179,16 +173,6 @@ void Logger::notice(const char * format, ...) const {
}
};
void Logger::notice(const __FlashStringHelper * format, ...) const {
if (enabled(Level::NOTICE)) {
va_list ap;
va_start(ap, format);
vlog(Level::NOTICE, format, ap);
va_end(ap);
}
};
void Logger::info(const char * format, ...) const {
if (enabled(Level::INFO)) {
va_list ap;
@@ -199,16 +183,6 @@ void Logger::info(const char * format, ...) const {
}
};
void Logger::info(const __FlashStringHelper * format, ...) const {
if (enabled(Level::INFO)) {
va_list ap;
va_start(ap, format);
vlog(Level::INFO, format, ap);
va_end(ap);
}
};
void Logger::debug(const char * format, ...) const {
if (enabled(Level::DEBUG)) {
va_list ap;
@@ -219,16 +193,6 @@ void Logger::debug(const char * format, ...) const {
}
};
void Logger::debug(const __FlashStringHelper * format, ...) const {
if (enabled(Level::DEBUG)) {
va_list ap;
va_start(ap, format);
vlog(Level::DEBUG, format, ap);
va_end(ap);
}
};
void Logger::trace(const char * format, ...) const {
if (enabled(Level::TRACE)) {
va_list ap;
@@ -239,38 +203,20 @@ void Logger::trace(const char * format, ...) const {
}
};
void Logger::trace(const __FlashStringHelper * format, ...) const {
if (enabled(Level::TRACE)) {
va_list ap;
va_start(ap, format);
vlog(Level::TRACE, format, ap);
va_end(ap);
}
};
void Logger::log(Level level, Facility facility, const char * format, ...) const {
if (level < Level::EMERG) {
level = Level::EMERG;
} else if (level > Level::TRACE) {
level = Level::TRACE;
}
void Logger::log(Level level, const char * format, ...) const {
level = constrain_level(level);
if (enabled(level)) {
va_list ap;
va_start(ap, format);
vlog(level, facility, format, ap);
vlog(level, format, ap);
va_end(ap);
}
};
void Logger::log(Level level, Facility facility, const __FlashStringHelper * format, ...) const {
if (level < Level::EMERG) {
level = Level::EMERG;
} else if (level > Level::TRACE) {
level = Level::TRACE;
}
void Logger::log(Level level, Facility facility, const char * format, ...) const {
level = constrain_level(level);
if (enabled(level)) {
va_list ap;
@@ -295,41 +241,34 @@ void Logger::vlog(Level level, Facility facility, const char * format, va_list a
dispatch(level, facility, text);
}
void Logger::vlog(Level level, const __FlashStringHelper * format, va_list ap) const {
vlog(level, facility_, format, ap);
}
void Logger::vlog(Level level, Facility facility, const __FlashStringHelper * format, va_list ap) const {
std::vector<char> text(MAX_LOG_LENGTH + 1);
if (vsnprintf_P(text.data(), text.size(), reinterpret_cast<PGM_P>(format), ap) <= 0) {
return;
}
dispatch(level, facility, text);
}
void Logger::dispatch(Level level, Facility facility, std::vector<char> & text) const {
std::shared_ptr<Message> message = std::make_shared<Message>(get_uptime_ms(), level, facility, name_, text.data());
text.resize(0);
for (auto & handler : handlers_) {
#if UUID_LOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
for (auto & handler : *registered_handlers()) {
if (level <= handler.second) {
*handler.first << message;
}
}
}
/* Mutex already locked by caller. */
void Logger::refresh_log_level() {
level_ = Level::OFF;
Level level = Level::OFF;
for (auto & handler : handlers_) {
if (level_ < handler.second) {
level_ = handler.second;
for (auto & handler : *registered_handlers()) {
if (level < handler.second) {
level = handler.second;
}
}
global_level_ = level;
}
} // namespace log
} // namespace uuid
} // namespace uuid