Rev 14 | Blame | Last modification | View Log | RSS feed
/*
* Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __TERROR_H__
#define __TERROR_H__
#include <iostream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#define LOG_NONE 0x0000
#define LOG_INFO 0x0001
#define LOG_WARNING 0x0002
#define LOG_ERROR 0x0004
#define LOG_TRACE 0x0008
#define LOG_DEBUG 0x0010
#define LOG_PROTOCOL LOG_INFO | LOG_ERROR
#define LOG_ALL LOG_INFO | LOG_WARNING | LOG_ERROR | LOG_TRACE | LOG_DEBUG
#define SLOG_NONE "NONE"
#define SLOG_INFO "INFO"
#define SLOG_WARNING "WARNING"
#define SLOG_ERROR "ERROR"
#define SLOG_TRACE "TRACE"
#define SLOG_DEBUG "DEBUG"
#define SLOG_PROTOCOL "PROTOCOL"
#define SLOG_ALL "ALL"
typedef enum terrtype_t
{
TERRNONE,
TERRINFO,
TERRWARNING,
TERRERROR,
TERRTRACE,
TERRDEBUG
}terrtype_t;
std::ostream& indent(std::ostream& os);
class TStreamError
{
public:
TStreamError(const std::string& logFile, const std::string& logLevel);
~TStreamError();
static void setLogFile(const std::string& lf) { mLogfile = lf; }
static std::string& getLogFile() { return mLogfile; }
static void setLogLevel(const std::string& slv);
static unsigned int getLogLevel() { return mLogLevel; }
static void logMsg(std::ostream& str);
static bool checkFilter(terrtype_t err);
static bool checkFilter(int lv);
friend std::ostream& indent(std::ostream& os);
static void incIndent() { mIndent++; }
static void decIndent();
static int getIndent() { return mIndent; }
static std::ostream *getStream() { return mStream; }
static std::string getTime();
private:
static void setLogLevel(unsigned int ll) { mLogLevel = ll; }
static unsigned int _getLevel(const std::string& slv);
static void _init();
const TStreamError& operator=(const TStreamError& ref);
static bool mInitialized;
static std::string mLogfile;
static unsigned int mLogLevel;
static int mIndent;
static std::ostream *mStream;
static std::stringstream mString;
};
class TTracer
{
public:
TTracer(const std::string& msg, int line, char *file);
~TTracer();
private:
std::string mHeadMsg;
int mLine;
std::string mFile;
};
class TError : public std::ostream
{
public:
static void setErrorMsg(const std::string& msg);
static void setErrorMsg(terrtype_t t, const std::string& msg);
static void setError() { mHaveError = true; }
static std::string& getErrorMsg() { return msError; }
static bool isError() { return mHaveError; }
static terrtype_t getErrorType() { return mErrType; }
static std::ostream& append(int lv, std::ostream& os);
static TStreamError* Current();
static void clear() { mHaveError = false; msError.clear(); mErrType = TERRNONE; }
static void logHex(char *str, size_t size);
const TError& operator=(const TError& ref);
static void lock();
static void unlock();
protected:
static std::string strToHex(const char *str, size_t size, int width, bool format, int indent);
private:
static std::string toHex(int num, int width);
TError() {};
~TError();
static std::string msError;
static bool mHaveError;
static terrtype_t mErrType;
static TStreamError *mCurrent;
std::string mHeadMsg;
};
#define MSG_INFO(msg) { TError::lock(); if (TStreamError::checkFilter(LOG_INFO)) { TError::Current()->logMsg(TError::append(LOG_INFO, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
#define MSG_WARNING(msg) { TError::lock(); if (TStreamError::checkFilter(LOG_WARNING)) { TError::Current()->logMsg(TError::append(LOG_WARNING, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
#define MSG_ERROR(msg) { TError::lock(); if (TStreamError::checkFilter(LOG_ERROR)) { TError::Current()->logMsg(TError::append(LOG_ERROR, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
#define MSG_TRACE(msg) { TError::lock(); if (TStreamError::checkFilter(LOG_TRACE)) { TError::Current()->logMsg(TError::append(LOG_TRACE, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
#define MSG_DEBUG(msg) { TError::lock(); if (TStreamError::checkFilter(LOG_DEBUG)) { TError::Current()->logMsg(TError::append(LOG_DEBUG, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
#define DECL_TRACER(msg) TTracer _hidden_tracer(msg, __LINE__, (char *)__FILE__);
#endif