logging.h 2.21 KB
/**
 * \file logging.h
 *
 * \author Wesnydy Lima Ribeiro <wesnydy@lavid.ufpb.br>
 * \date 2015
 */

#ifndef LOGGING_H
#define LOGGING_H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define _ERROR_ "\033[31m"
#define _DEBUG_ "\033[30m"
#define _WARN_  "\033[33m"
#define _INFO_  "\033[32m"
#define _END_   "\033[0m"

#define LOG_FILE "vlibras_user/vlibras-core/log/log"

namespace util {

	enum logLevel { _QUIET = 1 , _ERROR, _WARNING, _INFO, _DEBUG};

	/** \brief Classe responsável pelo log exibido no terminal.
	 *
	 * \headerfile util/src/include/logging.h
	 */
	class Logging {
	public:
		static Logging* instance();

		/** Retorna o nível de log selecionado.
		 *
		 * \return O nível de log.
		 */
		logLevel getLevel();

		/** Seta o nível de log, no terminal. */
		void setLevel(logLevel level);

		/** Escreve o log em um arquivo.
		 *
		 * \param logMsg A mensagem do log.
		 */
		void writeLog(const char* logMsg);

	private:
		Logging(){};
		Logging(Logging const&){};
		Logging& operator=(Logging const&){};

		static Logging* l_instance;
		static logLevel l_level;
		FILE* l_file;

		char* getTime();
	};

	#define PRINTL(level, format, ... ) {																				\
		logLevel llevel;																								\
		llevel = Logging::instance()->getLevel();																		\
		if(level <= llevel){																							\
			switch(level){																								\
			case _DEBUG:																								\
				fprintf(stdout, _DEBUG_"%s::%s<%d>: "_END_ format, __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__);	\
				break;																									\
			case _INFO:																									\
				fprintf(stdout, _INFO_"%s::%s<%d>: "_END_ format, __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__);	\
				break;																									\
			case _WARNING:																								\
				fprintf(stdout, _WARN_"%s::%s<%d>: "_END_ format, __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__);	\
				break;																									\
			case _ERROR:																								\
				fprintf(stderr, _ERROR_"%s::%s<%d>: "_END_ format, __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__);	\
				break;																									\
			}																											\
		}																												\
	}//fim da função PRINTL
}

#endif /* LOGGING_H */