|
|
@ -34,74 +34,116 @@ namespace srslte {
|
|
|
|
|
|
|
|
|
|
|
|
class log_filter;
|
|
|
|
class log_filter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class log_ref
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using ptr_type = std::unique_ptr<log>*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
log_ref() = default;
|
|
|
|
|
|
|
|
explicit log_ref(ptr_type p) : ptr_(p) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// works like a log*
|
|
|
|
|
|
|
|
log* operator->() { return ptr_->get(); }
|
|
|
|
|
|
|
|
log* operator->() const { return ptr_->get(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in case we want to obtain log*
|
|
|
|
|
|
|
|
log* operator*() { return ptr_->get(); }
|
|
|
|
|
|
|
|
log* get() { return ptr_->get(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// identity defined by ref address
|
|
|
|
|
|
|
|
bool operator==(const log_ref& l) { return ptr_ == l.ptr_; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// to do checks like if(log_ref)
|
|
|
|
|
|
|
|
operator bool() { return ptr_ != nullptr; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ptr_type ptr_ = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class logmap : public singleton_t<logmap>
|
|
|
|
class logmap : public singleton_t<logmap>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
static const size_t servicename_minimum_length = 4;
|
|
|
|
|
|
|
|
using it_t = std::unordered_map<std::string, std::unique_ptr<log> >::iterator;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// Access to log map by servicename. If servicename does not exist, create a new log_filter with default cfg
|
|
|
|
// Access to log map by servicename. If servicename does not exist, create a new log_filter with default cfg
|
|
|
|
// Access to the map is protected by a mutex
|
|
|
|
// Access to the map is protected by a mutex
|
|
|
|
static log* get(const std::string& servicename)
|
|
|
|
static log_ref get(std::string servicename)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
|
|
|
|
if (servicename.size() < servicename_minimum_length) {
|
|
|
|
|
|
|
|
// pad right of string with spaces
|
|
|
|
|
|
|
|
servicename.insert(servicename.end(), servicename_minimum_length - servicename.length(), ' ');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool->get_impl(std::move(servicename));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// register manually created log
|
|
|
|
|
|
|
|
static void register_log(std::unique_ptr<log> log_ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(pool->mutex);
|
|
|
|
|
|
|
|
if (log_ptr != nullptr) {
|
|
|
|
|
|
|
|
pool->log_map[log_ptr->get_service_name()] = std::move(log_ptr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static std::unique_ptr<srslte::log> deregister_log(const std::string& servicename)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
|
|
|
|
std::unique_ptr<srslte::log> ret;
|
|
|
|
std::lock_guard<std::mutex> lock(pool->mutex);
|
|
|
|
std::lock_guard<std::mutex> lock(pool->mutex);
|
|
|
|
auto it = pool->log_map.find(servicename);
|
|
|
|
auto it = pool->log_map.find(servicename);
|
|
|
|
if (it == pool->log_map.end()) {
|
|
|
|
if (it != pool->log_map.end()) {
|
|
|
|
// create a new logger with default cfg
|
|
|
|
ret = std::move(it->second);
|
|
|
|
std::unique_ptr<log_filter> filter(new log_filter{servicename, pool->default_logger});
|
|
|
|
pool->log_map.erase(it);
|
|
|
|
filter->set_level(pool->default_log_level);
|
|
|
|
|
|
|
|
filter->set_hex_limit(pool->default_hex_limit);
|
|
|
|
|
|
|
|
auto ret = pool->log_map.insert(std::make_pair(servicename, std::move(filter)));
|
|
|
|
|
|
|
|
std::unique_ptr<log>& inserted = ret.first->second;
|
|
|
|
|
|
|
|
return inserted.get();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return it->second.get();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set default logger
|
|
|
|
// set default logger
|
|
|
|
void set_default_logger(logger* logger_)
|
|
|
|
static void set_default_logger(logger* logger_)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
default_logger = logger_;
|
|
|
|
std::lock_guard<std::mutex> lock(pool->mutex);
|
|
|
|
|
|
|
|
pool->default_logger = logger_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set default log level
|
|
|
|
// set default log level
|
|
|
|
void set_default_log_level(LOG_LEVEL_ENUM l)
|
|
|
|
static void set_default_log_level(LOG_LEVEL_ENUM l)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
default_log_level = l;
|
|
|
|
std::lock_guard<std::mutex> lock(pool->mutex);
|
|
|
|
|
|
|
|
pool->default_log_level = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set default hex limit
|
|
|
|
// set default hex limit
|
|
|
|
void set_default_hex_limit(int hex_limit)
|
|
|
|
static void set_default_hex_limit(int hex_limit)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
logmap* pool = get_instance();
|
|
|
|
default_hex_limit = hex_limit;
|
|
|
|
std::lock_guard<std::mutex> lock(pool->mutex);
|
|
|
|
|
|
|
|
pool->default_hex_limit = hex_limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// register manually created log
|
|
|
|
protected:
|
|
|
|
void register_log(std::unique_ptr<log> log_ptr)
|
|
|
|
logmap() : default_logger(&logger_stdout_val) {}
|
|
|
|
{
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
if (log_ptr != nullptr) {
|
|
|
|
|
|
|
|
log_map[log_ptr->get_service_name()] = std::move(log_ptr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<srslte::log> deregister_log(const std::string& servicename)
|
|
|
|
private:
|
|
|
|
|
|
|
|
log_ref get_impl(std::string servicename)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_ptr<srslte::log> ret;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
auto it = log_map.find(servicename);
|
|
|
|
auto it = log_map.find(servicename);
|
|
|
|
if (it != log_map.end()) {
|
|
|
|
if (it == log_map.end()) {
|
|
|
|
ret = std::move(it->second);
|
|
|
|
// create a new logger with default cfg
|
|
|
|
log_map.erase(it);
|
|
|
|
std::unique_ptr<log_filter> filter(new log_filter{std::move(servicename), default_logger});
|
|
|
|
|
|
|
|
filter->set_level(default_log_level);
|
|
|
|
|
|
|
|
filter->set_hex_limit(default_hex_limit);
|
|
|
|
|
|
|
|
auto ret = log_map.insert(std::make_pair(filter->get_service_name(), std::move(filter)));
|
|
|
|
|
|
|
|
return log_ref{&ret.first->second};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
return log_ref{&it->second};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
logmap() : default_logger(&logger_stdout_val) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// consts
|
|
|
|
// consts
|
|
|
|
logger_stdout logger_stdout_val;
|
|
|
|
logger_stdout logger_stdout_val;
|
|
|
|
|
|
|
|
|
|
|
|