diff options
| author | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 16:49:52 -0500 |
|---|---|---|
| committer | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 16:49:52 -0500 |
| commit | 17b117e73df71925d73ee026b4f54aa1867ce0a5 (patch) | |
| tree | 382610c8e598a77a961c5ceb32b9b614ed00e757 /include/mts | |
| download | libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.tar.gz libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.tar.bz2 libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.zip | |
initial commit
Diffstat (limited to 'include/mts')
25 files changed, 2292 insertions, 0 deletions
diff --git a/include/mts/MTS_AutoLock.h b/include/mts/MTS_AutoLock.h new file mode 100644 index 0000000..e7a05bd --- /dev/null +++ b/include/mts/MTS_AutoLock.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_AutoLock.h + \brief Auto lock + \date 08OCT14 + \author Pavlo Samko + + Auto lock. + */ + + +#ifndef _MTS_AUTOLOCK_H_ +#define _MTS_AUTOLOCK_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_Lock.h> + +namespace MTS { + + //! Auto lock + /*! + Auto lock + + \sa Lock + */ + + class AutoLock: private NonCopyable { + + public: + AutoLock(Lock &lock): m_lock(lock) { m_lock.lock(); } //!< Constructs the AutoLock + ~AutoLock() { m_lock.unlock(); } //!< Destructs the AutoLock + + private: + Lock &m_lock; //!< Lock + }; +} + +#endif diff --git a/include/mts/MTS_AutoPtr.h b/include/mts/MTS_AutoPtr.h new file mode 100644 index 0000000..68b82cf --- /dev/null +++ b/include/mts/MTS_AutoPtr.h @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_AutoPtr.h + \brief An autopointer + \date 15JUN11 + \author Sean Godinez + + An autopointer. + */ + +#ifndef MTS_AUTOPTR_H +#define MTS_AUTOPTR_H + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_Stdint.h> + +namespace MTS { + + //! An autopointer + /*! + An autopointer cleans up after itself. + */ + template<class T> class AutoPtr: NonCopyable { + + public: + enum MallocType { + SINGLE, ARRAY + }; + + explicit AutoPtr(T* ptr = 0, MallocType type = SINGLE); //!< Explicitly Constructs the AutoPtr to reference ptr + ~AutoPtr(); //!< Destructs the AutoPtr deleting T + + T& operator*() const; //!< Returns T + T* operator->() const; //!< Returns address of T + T& operator[](const uint32_t& index) const; //!< Returns T at index + T* get() const; //!< Returns address of T + T* release(); //!< Removes reference to T (does NOT delete T) and returns address + void reset(T* ptr = 0, MallocType type = SINGLE); //!< Deletes old T and assigns internal reference to ptr + bool isNull() const; //!< Returns true if pointer is NULL + + private: + T* m_pObj; //!< Reference to class T + MallocType m_eType; //!< Describes memory allocated as a single T or block of Ts + + }; + + template<class T> AutoPtr<T>::AutoPtr(T* ptr, MallocType type) + : m_pObj(ptr), m_eType(type) { + } + + template<class T> AutoPtr<T>::~AutoPtr() { + if (m_eType == SINGLE) { + delete m_pObj; + } else { + delete[] m_pObj; + } + } + + template<class T> T& AutoPtr<T>::operator*() const { + return *m_pObj; + } + + template<class T> T* AutoPtr<T>::operator->() const { + return m_pObj; + } + + template<class T> T& AutoPtr<T>::operator[](const uint32_t& index) const { + return m_pObj[index]; + } + + template<class T> T* AutoPtr<T>::get() const { + return m_pObj; + } + + template<class T> T* AutoPtr<T>::release() { + T* temp = m_pObj; + m_pObj = 0; + return temp; + } + + template<class T> void AutoPtr<T>::reset(T* ptr, MallocType type) { + T* temp = m_pObj; + m_pObj = ptr; + if (m_eType == SINGLE) { + delete temp; + } else { + delete[] temp; + } + m_eType = type; + } + + template<class T> bool AutoPtr<T>::isNull() const { + return (m_pObj == 0); + } +} + +#endif diff --git a/include/mts/MTS_BasicPublisher.h b/include/mts/MTS_BasicPublisher.h new file mode 100644 index 0000000..d5749ec --- /dev/null +++ b/include/mts/MTS_BasicPublisher.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_BasicPublisher.h + \brief A simple publisher + \date 28MAR12 + \author Sean Godinez + + A basic publisher. + */ + +#ifndef _MTS_BASICPUBLISHER_H_ +#define _MTS_BASICPUBLISHER_H_ + +#include <mts/MTS_Publisher.h> + +namespace MTS { + + //! A basic publisher + /*! + A basic publisher provides simple publishing functionality. + + \sa Publisher, Subscriber, BasicSubscriber + */ + template<class T> class BasicPublisher: public Publisher<T> { + + public: + BasicPublisher(); //!< Constructor + virtual ~BasicPublisher(); //!< Destructor + + virtual void publish(const T& object); //!< Publishes T to Subscribers + }; + + template<class T> BasicPublisher<T>::BasicPublisher() { + } + + template<class T> BasicPublisher<T>::~BasicPublisher() { + } + + template<class T> void BasicPublisher<T>::publish(const T& object) { + Publisher<T>::publish(object); + } + +} + +#endif diff --git a/include/mts/MTS_BasicSubscriber.h b/include/mts/MTS_BasicSubscriber.h new file mode 100644 index 0000000..908dde9 --- /dev/null +++ b/include/mts/MTS_BasicSubscriber.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_BasicSubscriber.h + \brief A simple subscriber + \date 28MAR12 + \author Sean Godinez + + A subscriber. + */ +#ifndef _MTS_BASICSUBSCRIBER_H_ +#define _MTS_BASICSUBSCRIBER_H_ + +#include <mts/MTS_Subscriber.h> +#include <mts/MTS_Callback.h> + +namespace MTS { + + //! A basic subscriber + /*! + A basic subscriber provides simple subscribing functionality. + + \sa Publisher, Subscriber, BasicPublisher + */ + template<class T> class BasicSubscriber: public Subscriber<T> { + + public: + explicit BasicSubscriber(const Callback<T>& callback, const std::string& name = ""); //!< Constructor + virtual ~BasicSubscriber();//!< Destructor + + virtual void update(const T& object); //!< Invokes callback with published data T + + private: + Callback<T>* callback; + + //Prevent Copy and Assignment operators from being called + BasicSubscriber(const BasicSubscriber&); + BasicSubscriber& operator=(const BasicSubscriber&); + }; + + template<class T> BasicSubscriber<T>::BasicSubscriber(const Callback<T>& callback, const std::string& name) + : Subscriber<T>(name), callback(callback.clone()) { + } + + template<class T> BasicSubscriber<T>::~BasicSubscriber() { + delete callback; + } + + template<class T> void BasicSubscriber<T>::update(const T& object) { + callback->invoke(object); + } +} + +#endif diff --git a/include/mts/MTS_Buffer.h b/include/mts/MTS_Buffer.h new file mode 100644 index 0000000..96f25a9 --- /dev/null +++ b/include/mts/MTS_Buffer.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_Buffer.h + \brief A buffer + \date 15JUN11 + \author Sean Godinez + + A buffer. + */ + +#ifndef MTS_BUFFER_H +#define MTS_BUFFER_H + +#include <mts/MTS_Stdint.h> +#include <vector> +#include <string> + +namespace MTS { + + class Buffer { + + public: + static const uint32_t DEFAULT_CAPACITY; + + explicit Buffer(uint32_t capacity = DEFAULT_CAPACITY); + Buffer(const uint8_t* bytes, uint32_t count); + Buffer(const Buffer& other); + virtual ~Buffer(); + + Buffer& operator=(const Buffer& other); + + const uint8_t* getBuffer() const; + std::string str() const; + + uint32_t getSize() const; + void setSize(uint32_t newSize); + + uint32_t getCapacity() const; + void setCapacity(uint32_t newCapacity); + + void clear(); + void compact(); + + uint8_t operator[](uint32_t index) const; + uint8_t operator[](uint32_t index); + + const uint8_t& at(uint32_t index) const; + uint8_t& at(uint32_t index); + + Buffer& append(uint8_t byte); + Buffer& append(const uint8_t* bytes, uint32_t count); + + Buffer& insert(uint32_t index, uint8_t byte); + Buffer& insert(uint32_t index, const uint8_t* bytes, + uint32_t count); + + Buffer& remove(uint32_t index); + Buffer& remove(uint32_t start, uint32_t end); + + Buffer& replace(uint32_t start, uint32_t end, const uint8_t* bytes, uint32_t count); + Buffer* clone() const; + + private: + std::vector<uint8_t> m_vBuffer; + + }; +} + +#endif diff --git a/include/mts/MTS_Component.h b/include/mts/MTS_Component.h new file mode 100644 index 0000000..215add5 --- /dev/null +++ b/include/mts/MTS_Component.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_Component.h + \brief A threaded sub-pub object + \date 28MAR12 + \author Sean Godinez + + A threaded subscriber-publisher object. + */ + +#ifndef _MTS_COMPONENT_H_ +#define _MTS_COMPONENT_H_ + +#include <mts/MTS_Thread.h> +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_SubscriberQueue.h> +#include <mts/MTS_BasicPublisher.h> +#include <mts/MTS_Stdint.h> +#include <string> + +namespace MTS { + + template<class S, class P> class Component: public Thread { + + public: + virtual ~Component(); + + Subscriber<S>& getSubscriber(); + Publisher<P>& getPublisher(); + + virtual void stop(); + + protected: + explicit Component(const std::string& sName, uint32_t iCapacity = UINT32_MAX, bool bManaged = true); + + SubscriberQueue<S> m_oSubscriber; + BasicPublisher<P> m_oPublisher; + + private: + + }; + + template<class S, class P> Component<S, P>::Component( + const std::string& name, uint32_t capacity, bool managed) + : Thread(name, managed), m_oSubscriber(name, capacity) { + } + + template<class S, class P> Component<S, P>::~Component() { + Component::stop(); + } + + template<class S, class P> Subscriber<S>& Component<S, P>::getSubscriber() { + return m_oSubscriber; + } + + template<class S, class P> Publisher<P>& Component<S, P>::getPublisher() { + return m_oPublisher; + } + + template<class S, class P> void Component<S, P>::stop() { + Thread::stop(); + m_oSubscriber.clear(); + } +} + +#endif diff --git a/include/mts/MTS_Condition.h b/include/mts/MTS_Condition.h new file mode 100644 index 0000000..c15acd0 --- /dev/null +++ b/include/mts/MTS_Condition.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_Condition.h + \brief A condition that can be waited on or signaled + \date 15JUN11 + \author Sean Godinez + + A platform independent condition that can be waited on or signaled. + */ + +#ifndef _MTS_CONDITION_H_ +#define _MTS_CONDITION_H_ + +#include <mts/MTS_Stdint.h> +#include <mts/MTS_AutoPtr.h> +#include <mts/MTS_NonCopyable.h> + +#ifdef WIN32 +#include <windows.h> +#else +#include <pthread.h> +#endif + +namespace MTS { + + class Lock; + + //! A condition + /*! + A platform independent condition that can be waited on or signaled. Useful in building semaphores. + + \sa Lock + */ + class Condition: private NonCopyable { + friend class Lock; + + public: + ~Condition(); //!< Destructs the Condition + + void wait(); //!< Waits on *this Condition for signal + void wait(uint32_t millis); //!< Waits on *this Condition for signal or given amount of time + void signal(); //!< Signals *this Condition + + private: + + Lock* m_pLock; //!< Lock that created *this Condition +#ifdef WIN32 + AutoPtr<HANDLE> m_apCondition; //!< Handle to WIN32 Condition +#else + AutoPtr<pthread_cond_t> m_apCondition; //!< Handle to PTHREAD Condition +#endif + + explicit Condition(Lock* lock); //!< Explicit Constructor! Must pass Lock as parent. + }; +} + +#endif diff --git a/include/mts/MTS_Lock.h b/include/mts/MTS_Lock.h new file mode 100644 index 0000000..c35b131 --- /dev/null +++ b/include/mts/MTS_Lock.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_Lock.h + \brief A mutex lock + \date 15JUN11 + \author Sean Godinez + + A platform independent mutex lock. + */ + +#ifndef _MTS_LOCK_H_ +#define _MTS_LOCK_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_Condition.h> +#include <mts/MTS_AutoPtr.h> + +#ifdef WIN32 +#include <windows.h> +#else +#include <pthread.h> +#endif + +namespace MTS { + + //! A mutex lock + /*! + A platform independent mutex lock + + \sa Condition + */ + + class Lock: private NonCopyable { + + public: + Lock(); //!< Constructs the Lock + ~Lock(); //!< Destructs the Lock + + void lock(); //!< Locks the mutex + void unlock(); //!< Unlocks the mutex + + bool isLocked() const; //!< Returns true if locked + + Condition* createCondition(); //!< Creates a Condition object associated with *this Lock + + private: + friend class Condition; + volatile bool m_bLocked; //!< Locks the mutex +#ifdef WIN32 + AutoPtr<HANDLE> m_apMutex; //!< WIN32 Mutex Handle +#else + AutoPtr<pthread_mutex_t> m_apMutex; //!< PTHREAD Mutex Handle + AutoPtr<pthread_mutexattr_t> m_apMutexAttr; //!< PTHREAD Mutex Attributes +#endif + }; +} + +#endif diff --git a/include/mts/MTS_Logger.h b/include/mts/MTS_Logger.h new file mode 100644 index 0000000..a1e88a2 --- /dev/null +++ b/include/mts/MTS_Logger.h @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +/*! \file MTS_Logger.h + \brief A logging utility + \date 15JUN11 + \author Sean Godinez + + A logging utility capable of writing to stdout, syslog, or file + */ +#ifndef MTS_LOGGER_H_ +#define MTS_LOGGER_H_ + +#include <mts/MTS_NonCopyable.h> +#include <mts/MTS_NonConstructable.h> +#include <mts/MTS_Lock.h> +#include <mts/MTS_Stdint.h> +#include <string> + +#ifdef DEBUG +#define printFatal(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::FATAL_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::FATAL_LEVEL, MTS::Logger::PrintLevel::FATAL_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printError(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::ERROR_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::ERROR_LEVEL, MTS::Logger::PrintLevel::ERROR_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printWarning(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::WARNING_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::WARNING_LEVEL, MTS::Logger::PrintLevel::WARNING_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printInfo(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::INFO_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::INFO_LEVEL, MTS::Logger::PrintLevel::INFO_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printConfig(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::CONFIG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::CONFIG_LEVEL, MTS::Logger::PrintLevel::CONFIG_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printDebug(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::DEBUG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::DEBUG_LEVEL, MTS::Logger::PrintLevel::DEBUG_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define printTrace(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::TRACE_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::TRACE_LEVEL, MTS::Logger::PrintLevel::TRACE_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#else +#define printFatal(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::FATAL_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::FATAL_LEVEL, MTS::Logger::PrintLevel::FATAL_LABEL, format, ##__VA_ARGS__) +#define printError(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::ERROR_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::ERROR_LEVEL, MTS::Logger::PrintLevel::ERROR_LABEL, format, ##__VA_ARGS__) +#define printWarning(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::WARNING_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::WARNING_LEVEL, MTS::Logger::PrintLevel::WARNING_LABEL, format, ##__VA_ARGS__) +#define printInfo(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::INFO_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::INFO_LEVEL, MTS::Logger::PrintLevel::INFO_LABEL, format, ##__VA_ARGS__) +#define printConfig(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::CONFIG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::CONFIG_LEVEL, MTS::Logger::PrintLevel::CONFIG_LABEL, format, ##__VA_ARGS__) +#define printDebug(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::DEBUG_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::DEBUG_LEVEL, MTS::Logger::PrintLevel::DEBUG_LABEL, format, ##__VA_ARGS__) +#define printTrace(format, ...) \ + if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::TRACE_LEVEL)) \ + MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::TRACE_LEVEL, MTS::Logger::PrintLevel::TRACE_LABEL, format, ##__VA_ARGS__) +#endif + +namespace MTS { + + class Logger: private NonCopyable { + + public: + + class PrintLevel: private NonConstructable { + public: + static const int32_t OFF_LEVEL; + static const int32_t MINIMUM_LEVEL; + static const int32_t FATAL_LEVEL; + static const int32_t ERROR_LEVEL; + static const int32_t WARNING_LEVEL; + static const int32_t INFO_LEVEL; + static const int32_t CONFIG_LEVEL; + static const int32_t DEBUG_LEVEL; + static const int32_t TRACE_LEVEL; + static const int32_t MAXIMUM_LEVEL; + + static const char* OFF_LABEL; + static const char* FATAL_LABEL; + static const char* ERROR_LABEL; + static const char* WARNING_LABEL; + static const char* INFO_LABEL; + static const char* CONFIG_LABEL; + static const char* DEBUG_LABEL; + static const char* TRACE_LABEL; + static const char* MAXIMUM_LABEL; + }; + + enum PrintMode { + NO_PRINTING, + STDOUT_ONLY, + FILE_ONLY, + SYSLOG_ONLY, + STDOUT_AND_FILE, + STDOUT_AND_SYSLOG + }; + + static int getPrintLevel(); + static const std::string& getPrintLevelString(); + static void setPrintLevel(int32_t level, bool silent = false); + static bool isPrintable(int32_t level); + + static void printfFatal(const char* format, ...); + static void printfError(const char* format, ...); + static void printfWarning(const char* format, ...); + static void printfInfo(const char* format, ...); + static void printfConfig(const char* format, ...); + static void printfDebug(const char* format, ...); + static void printfTrace(const char* format, ...); + static void printfGeneric(int level, const char* label, const char* format, ...); + + static void printf(int level, const char* format, ...); + static void printf(const char* format, ...); + + static bool setup(const PrintMode& mode); + static bool setup(const PrintMode& mode, const std::string& ident, const int& option, const int& facility); + static bool setup(const PrintMode& mode, const std::string& filename); + + private: + + static volatile int m_iPrintLevel; + static std::string m_sPrintLevel; + static Logger::PrintMode m_eMode; + static Lock m_oPrintLock; + static FILE* m_pFile; + static int m_iLogFacility; + static std::string m_sIdent; + static std::string m_sFileName; + + static int32_t syslogPrintLevelConversion(const int32_t& level); + static void printMessage(const int32_t& level, const char* label, const char* format, va_list argptr); + + Logger(); + }; +} + +#endif /* MTS_LOG_H_ */ diff --git a/include/mts/MTS_NonConstructable.h b/include/mts/MTS_NonConstructable.h new file mode 100644 index 0000000..ab69c1d --- /dev/null +++ b/include/mts/MTS_NonConstructable.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * |
