Skip to content

Commit

Permalink
latest from coda-oss
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Jan 5, 2022
1 parent 41ba677 commit aff92f4
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 71 deletions.
24 changes: 16 additions & 8 deletions externals/coda-oss/modules/c++/logging/include/logging/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
// Filter.h
///////////////////////////////////////////////////////////

#ifndef __LOGGING_FILTER_H__
#define __LOGGING_FILTER_H__
#ifndef CODA_OSS_logging_Filter_h_INCLUDED_
#define CODA_OSS_logging_Filter_h_INCLUDED_
#pragma once

#include <string>
#include "logging/LogRecord.h"
Expand All @@ -38,18 +39,25 @@ namespace logging
*
* \brief Filter instances are used to perform arbitrary filtering of LogRecords.
*/
class Filter
struct Filter
{
public:
Filter(std::string name = "") : mName(name){}
virtual ~Filter(){}
virtual ~Filter() = default;

bool filter(const LogRecord* record) const;
std::string getName() const { return mName; }
virtual bool filter(const LogRecord* record) const;
virtual bool filter(const LogRecord& record) const
{
return filter(&record);
}

std::string getName() const
{
return mName;
}

protected:
std::string mName;
};

}
#endif
#endif // CODA_OSS_logging_Filter_h_INCLUDED_
24 changes: 15 additions & 9 deletions externals/coda-oss/modules/c++/logging/include/logging/Filterer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
// Filterer.h
///////////////////////////////////////////////////////////

#ifndef __LOGGING_FILTERER_H__
#define __LOGGING_FILTERER_H__
#ifndef CODA_OSS_logging_Filterer_h_INCLUDED_
#define CODA_OSS_logging_Filterer_h_INCLUDED_
#pragma once

#include <string>
#include <map>
Expand All @@ -41,27 +42,32 @@ namespace logging
* A base class for loggers and handlers which allows them to share
* common code.
*/
class Filterer
struct Filterer
{
public:
Filterer(){}
virtual ~Filterer(){}
Filterer() = default;
virtual ~Filterer() = default;

/*!
* Adds a Filter to the managed map of Filters. We do NOT take control of
* the pointer
*/
void addFilter(Filter* filter);
void addFilter(Filter&);

bool filter(const LogRecord* record) const;
virtual bool filter(const LogRecord* record) const;
virtual bool filter(const LogRecord& record) const
{
return filter(&record);
}

//! Removes the specified Filter
void removeFilter(Filter* filter);
void removeFilter(Filter&);

protected:
std::map<std::string, Filter*> filters;

};

}
#endif
#endif // CODA_OSS_logging_Filterer_h_INCLUDED_

20 changes: 13 additions & 7 deletions externals/coda-oss/modules/c++/logging/include/logging/Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
// Handler.h
///////////////////////////////////////////////////////////

#ifndef __LOGGING_HANDLER_H__
#define __LOGGING_HANDLER_H__
#ifndef CODA_OSS_logging_Handler_h_INCLUDED_
#define CODA_OSS_logging_Handler_h_INCLUDED_

#include <string>
#include "logging/LogRecord.h"
Expand All @@ -51,16 +51,15 @@ struct Handler : public Filterer
* Construct a Handler at the specified LogLevel (LogLevel::LOG_NOTSET is default)
*/
Handler(LogLevel level = LogLevel::LOG_NOTSET);
virtual ~Handler()
{
}
virtual ~Handler() = default;
Handler& operator=(const Handler&) = delete;

/*!
* Sets the Formatter to use when formatting LogRecords
* Not Threads Safe!
*/
virtual void setFormatter(Formatter* formatter);
virtual void setFormatter(std::unique_ptr<Formatter>&&);

//! Sets the minimum LogLevel required to emit LogRecords
void setLevel(LogLevel level);
Expand All @@ -74,6 +73,10 @@ struct Handler : public Filterer
* and emitted.
*/
virtual bool handle(const LogRecord* record);
virtual bool handle(const LogRecord& record)
{
return handle(&record);
}

virtual void close();

Expand All @@ -85,7 +88,10 @@ struct Handler : public Filterer
// for writing directly to stream,
// used for the bulk of the logging for speed
virtual void emitRecord(const LogRecord* record) = 0;

virtual void emitRecord(const LogRecord& record)
{
return emitRecord(&record);
}

LogLevel mLevel = LogLevel::LOG_NOTSET;
sys::Mutex mHandlerLock;
Expand All @@ -94,4 +100,4 @@ struct Handler : public Filterer
};

}
#endif
#endif // CODA_OSS_logging_Handler_h_INCLUDED_
13 changes: 9 additions & 4 deletions externals/coda-oss/modules/c++/logging/include/logging/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
// Logger.h
///////////////////////////////////////////////////////////

#ifndef __LOGGING_LOGGER_H__
#define __LOGGING_LOGGER_H__
#ifndef CODA_OSS_logging_Logger_h_INCLUDED_
#define CODA_OSS_logging_Logger_h_INCLUDED_

#include <string>
#include <vector>
#include <memory>

#include "logging/Filterer.h"
#include "logging/LogRecord.h"
#include "logging/Handler.h"
Expand Down Expand Up @@ -104,6 +105,7 @@ struct Logger : public Filterer
* This Logger does not own the passed-in Handler.
*/
void addHandler(Handler* handler, bool own = false);
void addHandler(std::unique_ptr<Handler>&&); // own = true

/*!
* Removes the specified Handler from the list of Handlers.
Expand Down Expand Up @@ -139,14 +141,17 @@ struct Logger : public Filterer

protected:
void handle(const LogRecord* record);
void handle(const LogRecord& record)
{
handle(&record);
}

typedef std::pair<Handler*, bool> Handler_T;
typedef std::vector<Handler_T> Handlers_T;

std::string mName;
Handlers_T mHandlers;

};
typedef std::shared_ptr<Logger> LoggerPtr;
}
#endif
#endif // CODA_OSS_logging_Logger_h_INCLUDED_
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct StreamHandler : public Handler
//! adds the need to write epilogue before deleting formatter
// and then writing the prologue with the new formatter
virtual void setFormatter(Formatter* formatter);
virtual void setFormatter(std::unique_ptr<Formatter>&&);

virtual void close();

Expand Down
15 changes: 10 additions & 5 deletions externals/coda-oss/modules/c++/logging/source/Filterer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ void logging::Filterer::addFilter(logging::Filter* filter)
filters[filter->getName()] = filter;
}
}
void logging::Filterer::addFilter(logging::Filter& filter)
{
addFilter(&filter);
}

bool logging::Filterer::filter(const logging::LogRecord* record) const
{
for (std::map<std::string, logging::Filter*>::const_iterator p = filters.begin();
p != filters.end(); ++p)
for (const auto& p : filters)
{
if (!p->second->filter(record))
if (!p.second->filter(record))
return false;
}
return true;
Expand All @@ -49,6 +52,8 @@ bool logging::Filterer::filter(const logging::LogRecord* record) const
void logging::Filterer::removeFilter(logging::Filter* filter)
{
filters.erase(filter->getName());

}

void logging::Filterer::removeFilter(logging::Filter& filter)
{
removeFilter(&filter);
}
9 changes: 6 additions & 3 deletions externals/coda-oss/modules/c++/logging/source/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ Handler::Handler(LogLevel level)
void Handler::close()
{
// delete if necessary
if (mFormatter != &mDefaultFormatter &&
mFormatter != NULL)
if (mFormatter != &mDefaultFormatter) // "delete NULL" is ok
{
delete mFormatter;
mFormatter = NULL;
mFormatter = nullptr;
}
}

Expand Down Expand Up @@ -83,4 +82,8 @@ void Handler::setFormatter(Formatter* formatter)
mFormatter = formatter;
}
}
void Handler::setFormatter(std::unique_ptr<Formatter>&& formatter)
{
setFormatter(formatter.release());
}
}
27 changes: 14 additions & 13 deletions externals/coda-oss/modules/c++/logging/source/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ logging::Logger::~Logger()

void logging::Logger::log(logging::LogLevel level, const std::string& msg)
{
logging::LogRecord *rec = new logging::LogRecord(mName, msg, level);
const logging::LogRecord rec(mName, msg, level);
handle(rec);
delete rec;
}

void logging::Logger::log(LogLevel level, const except::Context& ctxt)
{
logging::LogRecord *rec = new logging::LogRecord(mName, ctxt.getMessage(),
const logging::LogRecord rec(mName, ctxt.getMessage(),
level, ctxt.getFile(),
ctxt.getFunction(),
ctxt.getLine(),
ctxt.getTime());
handle(rec);
delete rec;
}

void logging::Logger::log(LogLevel level, const except::Throwable& t)
Expand Down Expand Up @@ -155,12 +153,12 @@ void logging::Logger::handle(const logging::LogRecord* record)
{
if (filter(record))
{
for (Handlers_T::iterator p = mHandlers.begin(); p != mHandlers.end(); ++p)
for (const auto& p : mHandlers)
{
//std::cout << (int)(*p)->getLevel() << std::endl;
//only handle if it is above/equal to threshold
if (p->first->getLevel() <= record->getLevel())
p->first->handle(record);
if (p.first->getLevel() <= record->getLevel())
p.first->handle(record);
}
}
}
Expand All @@ -181,6 +179,10 @@ void logging::Logger::addHandler(logging::Handler* handler, bool own)
if (!found)
mHandlers.push_back(Handler_T(handler, own));
}
void logging::Logger::addHandler(std::unique_ptr<logging::Handler>&& handler)
{
addHandler(handler.release(), true /*own*/);
}

void logging::Logger::removeHandler(logging::Handler* handler)
{
Expand All @@ -197,20 +199,19 @@ void logging::Logger::removeHandler(logging::Handler* handler)

void logging::Logger::setLevel(LogLevel level)
{
for (Handlers_T::iterator p = mHandlers.begin(); p != mHandlers.end(); ++p)
for (const auto& p : mHandlers)
{
//set the level
p->first->setLevel(level);
p.first->setLevel(level);
}
}

void logging::Logger::reset()
{
for (Handlers_T::iterator p = mHandlers.begin(); p
!= mHandlers.end(); ++p)
for (const auto& p : mHandlers)
{
if (p->second && p->first)
delete p->first;
if (p.second && p.first)
delete p.first;
}
mHandlers.clear();
}
11 changes: 11 additions & 0 deletions externals/coda-oss/modules/c++/logging/source/StreamHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ void StreamHandler::setFormatter(Formatter* formatter)
// start log with formatter injection
write(mFormatter->getPrologue());
}
void StreamHandler::setFormatter(std::unique_ptr<Formatter>&& formatter)
{
// end log with formatter injection
write(mFormatter->getEpilogue());

// delete old and reset to new
Handler::setFormatter(std::move(formatter));

// start log with formatter injection
write(mFormatter->getPrologue());
}

void StreamHandler::close()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ sys::Mutex RunNothing::counterLock;

TEST_CASE(testExceptionLogger)
{
std::unique_ptr<logging::Logger> log(new logging::Logger("test"));
logging::Logger log("test");

mem::SharedPtr<logging::ExceptionLogger> exLog(new logging::ExceptionLogger(log.get()));
logging::ExceptionLogger exLog(&log);

size_t counter(0);
uint16_t numThreads(2);
Expand All @@ -73,11 +73,11 @@ TEST_CASE(testExceptionLogger)
mt::GenerationThreadPool pool(numThreads);
pool.start();

runs.push_back(new RunNothing(counter, exLog.get()));
runs.push_back(new RunNothing(counter, &exLog));
pool.addAndWaitGroup(runs);
runs.clear();

runs.push_back(new RunNothing(counter, exLog.get()));
runs.push_back(new RunNothing(counter, &exLog));
pool.addAndWaitGroup(runs);
runs.clear();

Expand Down
Loading

0 comments on commit aff92f4

Please sign in to comment.