hide logmap implementation in source file. Export symbols so they can be used by srsASN1 project

master
Francisco Paisana 5 years ago
parent e859d622c7
commit 32c347fbb1

@ -19,17 +19,16 @@
* *
*/ */
#include "log_filter.h"
#include "logger_stdout.h"
#include "singleton.h"
#include "srslte/common/log_filter.h"
#include "srslte/common/logger_stdout.h"
#include <mutex>
#include <unordered_map>
#ifndef SRSLTE_LOGMAP_H #ifndef SRSLTE_LOGMAP_H
#define SRSLTE_LOGMAP_H #define SRSLTE_LOGMAP_H
#include "srslte/common/log.h"
#include "srslte/common/logger.h"
#include "srslte/common/singleton.h"
#include <mutex>
#include <unordered_map>
namespace srslte { namespace srslte {
class log_filter; class log_filter;
@ -63,89 +62,34 @@ private:
class logmap : public singleton_t<logmap> class logmap : public singleton_t<logmap>
{ {
static const size_t servicename_minimum_length = 4; 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_ref get(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 // register manually created log
static void register_log(std::unique_ptr<log> log_ptr) static void register_log(std::unique_ptr<log> log_ptr);
{
logmap* pool = get_instance(); static std::unique_ptr<srslte::log> deregister_log(const std::string& servicename);
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();
std::unique_ptr<srslte::log> ret;
std::lock_guard<std::mutex> lock(pool->mutex);
auto it = pool->log_map.find(servicename);
if (it != pool->log_map.end()) {
ret = std::move(it->second);
pool->log_map.erase(it);
}
return ret;
}
// set default logger // set default logger
static void set_default_logger(logger* logger_) static void set_default_logger(logger* logger_);
{
logmap* pool = get_instance();
std::lock_guard<std::mutex> lock(pool->mutex);
pool->default_logger = logger_;
}
// set default log level // set default log level
static void set_default_log_level(LOG_LEVEL_ENUM l) static void set_default_log_level(LOG_LEVEL_ENUM l);
{
logmap* pool = get_instance();
std::lock_guard<std::mutex> lock(pool->mutex);
pool->default_log_level = l;
}
// set default hex limit // set default hex limit
static void set_default_hex_limit(int hex_limit) static void set_default_hex_limit(int hex_limit);
{
logmap* pool = get_instance();
std::lock_guard<std::mutex> lock(pool->mutex);
pool->default_hex_limit = hex_limit;
}
protected: protected:
logmap() : default_logger(&logger_stdout_val) {} logmap();
private: private:
log_ref get_impl(std::string servicename) log_ref get_impl(std::string servicename);
{
std::lock_guard<std::mutex> lock(mutex);
auto it = log_map.find(servicename);
if (it == log_map.end()) {
// create a new logger with default cfg
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 log_ref{&it->second};
}
// consts // consts
logger_stdout logger_stdout_val; std::unique_ptr<logger> logger_stdout_val;
// default cfg // default cfg
logger* default_logger = nullptr; logger* default_logger = nullptr;

@ -22,6 +22,8 @@
#ifndef SRSLTE_SINGLETON_H #ifndef SRSLTE_SINGLETON_H
#define SRSLTE_SINGLETON_H #define SRSLTE_SINGLETON_H
#include <memory>
// CRTP pattern to create class singletons // CRTP pattern to create class singletons
template <typename T> template <typename T>
class singleton_t class singleton_t

@ -26,6 +26,7 @@ set(SOURCES arch_select.cc
gen_mch_tables.c gen_mch_tables.c
liblte_security.cc liblte_security.cc
log_filter.cc log_filter.cc
logmap.cc
logger_file.cc logger_file.cc
mac_pcap.cc mac_pcap.cc
nas_pcap.cc nas_pcap.cc

@ -0,0 +1,105 @@
/*
* Copyright 2013-2019 Software Radio Systems Limited
*
* This file is part of srsLTE.
*
* srsLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include "srslte/common/logmap.h"
#include "srslte/common/log_filter.h"
#include "srslte/common/logger_stdout.h"
using namespace srslte;
logmap::logmap() : logger_stdout_val(new logger_stdout{})
{
default_logger = logger_stdout_val.get();
}
// 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
log_ref SRSLTE_EXPORT logmap::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
void SRSLTE_EXPORT logmap::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);
}
}
std::unique_ptr<srslte::log> SRSLTE_EXPORT logmap::deregister_log(const std::string& servicename)
{
logmap* pool = get_instance();
std::unique_ptr<srslte::log> ret;
std::lock_guard<std::mutex> lock(pool->mutex);
auto it = pool->log_map.find(servicename);
if (it != pool->log_map.end()) {
ret = std::move(it->second);
pool->log_map.erase(it);
}
return ret;
}
// set default logger
void SRSLTE_EXPORT logmap::set_default_logger(logger* logger_)
{
logmap* pool = get_instance();
std::lock_guard<std::mutex> lock(pool->mutex);
pool->default_logger = logger_;
}
// set default log level
void SRSLTE_EXPORT logmap::set_default_log_level(LOG_LEVEL_ENUM l)
{
logmap* pool = get_instance();
std::lock_guard<std::mutex> lock(pool->mutex);
pool->default_log_level = l;
}
// set default hex limit
void SRSLTE_EXPORT logmap::set_default_hex_limit(int hex_limit)
{
logmap* pool = get_instance();
std::lock_guard<std::mutex> lock(pool->mutex);
pool->default_hex_limit = hex_limit;
}
log_ref logmap::get_impl(std::string servicename)
{
std::lock_guard<std::mutex> lock(mutex);
auto it = log_map.find(servicename);
if (it == log_map.end()) {
// create a new logger with default cfg
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 log_ref{&it->second};
}
Loading…
Cancel
Save