<commit_before>/*************************************************************************
 * Copyright (c) 2014 eProsima. All rights reserved.
 *
 * This copy of eProsima Fast RTPS is licensed to you under the terms described in the
 * FASTRTPS_LIBRARY_LICENSE file included in this distribution.
 *
 *************************************************************************/

#ifndef DOXYGEN_SHOULD_SKIP_THIS

/**
 * @file Log.cpp
 *
 */



#include <fastrtps/log/Log.h>

#define START_N_MESSAGES 2000
#define MIN_N_MESSAGES 500

#include <boost/thread/mutex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
namespace eprosima {

Log* Log::m_instance = NULL;
bool Log::m_instanceFlag = false;

static const int MAXWIDTH = 43;


Log::Log(): mp_logFile(NULL),
		m_logFileDefined(false),
#if defined(__DEBUG) || defined(_DEBUG)
		m_defaultVerbosityLevel(VERB_INFO),
#else
		m_defaultVerbosityLevel(VERB_ERROR),
#endif
		mp_logMesgMutex(NULL),
		mp_printMutex(NULL)

{

	mp_logMesgMutex = new boost::mutex();
	mp_printMutex = new boost::mutex();


	for(uint32_t i = 0;i< START_N_MESSAGES;++i)
	{
		m_logMessages.push(new LogMessage());
	}

}

void Log::removeLog()
{
	delete(Log::getInstance());
}

Log::~Log() {
	// TODO Auto-generated destructor stub
	if(mp_logFile !=NULL)
	{
		mp_logFile->close();
		delete(mp_logFile);
	}

	delete(mp_logMesgMutex);
	delete(mp_printMutex);

	m_instanceFlag = false;
	m_instance = nullptr;
	while(m_logMessages.size()>0)
	{
		delete(m_logMessages.front());
		m_logMessages.pop();
	}
}

Log* Log::getInstance()
{
	if(!m_instanceFlag)
	{
		m_instance = new Log();
		m_instanceFlag = true;
	}
	return m_instance;
}

void Log::setVerbosity(LOG_VERBOSITY_LVL level)
{
	Log* ELOG = Log::getInstance();
	ELOG->m_defaultVerbosityLevel = level;
	for(CategoryVerbosity::iterator it = ELOG->m_categories.begin();
			it!= ELOG->m_categories.end();++it)
	{
		it->second = level;
	}
}

void Log::setCategoryVerbosity(LOG_CATEGORY cat, LOG_VERBOSITY_LVL level)
{
	Log* ELOG = Log::getInstance();
	ELOG->getCategory(cat)->second = level;
}

void Log::logFileName(const char* filename,bool add_date_to_name)
{
	Log* ELOG = Log::getInstance();
	std::stringstream ss;
	if(add_date_to_name)
	{
		boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
		facet->format("%Y%m%d_%H%M%S");
		ss.imbue(std::locale(std::locale::classic(), facet));
		ss << boost::posix_time::ptime(boost::posix_time::second_clock::local_time())<<"_";
	}
	ss << filename;
	ELOG->mp_logFile = new std::ofstream();
	ELOG->mp_logFile->open(ss.str().c_str(),std::ios::app);
	ELOG->m_logFileDefined = true;
	(*ELOG->mp_logFile) << std::endl;
	(*ELOG->mp_logFile) << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
	(*ELOG->mp_logFile) << "+                                EPROSIMA LOG                                  +"<<std::endl;
	(*ELOG->mp_logFile) << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
	(*ELOG->mp_logFile) << "+                            "<< boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time());
	(*ELOG->mp_logFile) << "                              +"<<std::endl;
	(*ELOG->mp_logFile) << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
	(*ELOG->mp_logFile) << std::endl;
}

LogMessage* Log::getLogMessage()
{
	mp_logMesgMutex->lock();
	if(m_logMessages.size()<MIN_N_MESSAGES)
	{
		for(uint32_t i = 0;i<MIN_N_MESSAGES;++i)
			m_logMessages.push(new LogMessage());
	}
	LogMessage* msg = m_logMessages.front();
	m_logMessages.pop();
	mp_logMesgMutex->unlock();
	return msg;
}

CategoryVerbosity::iterator Log::getCategory(LOG_CATEGORY cat)
{
	CategoryVerbosity::iterator it = m_categories.find(cat);
	if(it != m_categories.end())
		return it;
	m_categories[cat] = m_defaultVerbosityLevel;
	return m_categories.find(cat);
}


LogMessage& Log::logMessage(LOG_TYPE type, LOG_CATEGORY cat, const char* CLASS_NAME,
		const char* METHOD_NAME,const char * COLOR)
{
	Log* ELOG = Log::getInstance();
	LogMessage* log = ELOG->getLogMessage();
	log->m_type = type;
	log->m_cat = cat;
	log->m_color.str("");
	if (COLOR != nullptr)
		log->m_color << COLOR;
	else
		log->m_color << C_DEF;
	log->m_msg.str("");
	log->m_msg <<  "["<<CLASS_NAME << "::"<<METHOD_NAME;
	log->m_msg << std::left << std::setw(MAXWIDTH -log->m_msg.str().size())<< "] ";
	log->m_date.str("");
	log->m_date << "["<< boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::local_time())<<"]";
	return *log;
}

LogMessage& Log::logMessage(LOG_TYPE type,const char* COLOR)
{
	Log* ELOG = Log::getInstance();
	LogMessage* log = ELOG->getLogMessage();
	log->m_type = type;
	log->m_msg.str("");
	if (COLOR != nullptr)
			log->m_color << COLOR;
		else
			log->m_color << C_DEF;
	log->m_date.str("");
	log->m_date << "["<< boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::local_time())<<"]";
	return *log;
}


void Log::addMessage(LogMessage& lm)
{
	Log* ELOG = Log::getInstance();
	ELOG->mp_printMutex->lock();
	ELOG->printLogMessage(lm);<commit_msg>Fixed error in log<commit_after>/*************************************************************************
 * Copyright (c) 2014 eProsima. All rights reserved.
 *
 * This copy of eProsima Fast RTPS is licensed to you under the terms described in the
 * FASTRTPS_LIBRARY_LICENSE file included in this distribution.
 *
 *************************************************************************/

#ifndef DOXYGEN_SHOULD_SKIP_THIS

/**
 * @file Log.cpp
 *
 */



#include <fastrtps/log/Log.h>

#define START_N_MESSAGES 2000
#define MIN_N_MESSAGES 500

#include <boost/thread/mutex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
namespace eprosima {

Log* Log::m_instance = NULL;
bool Log::m_instanceFlag = false;

static const int MAXWIDTH = 43;


Log::Log(): mp_logFile(NULL),
		m_logFileDefined(false),
#if defined(__DEBUG) || defined(_DEBUG)
		m_defaultVerbosityLevel(VERB_INFO),
#else
		m_defaultVerbosityLevel(VERB_ERROR),
#endif
		mp_logMesgMutex(NULL),
		mp_printMutex(NULL)

{

	mp_logMesgMutex = new boost::mutex();
	mp_printMutex = new boost::mutex();


	for(uint32_t i = 0;i< START_N_MESSAGES;++i)
	{
		m_logMessages.push(new LogMessage());
	}

}

void Log::removeLog()
{
	delete(Log::getInstance());
}

Log::~Log() {
	// TODO Auto-generated destructor stub
	if(mp_logFile !=NULL)
	{
		mp_logFile->close();
		delete(mp_logFile);
	}

	delete(mp_logMesgMutex);
	delete(mp_printMutex);

	m_instanceFlag = false;
	m_instance = nullptr;
	while(m_logMessages.size()>0)
	{
		delete(m_logMessages.front());
		m_logMessages.pop();
	}
}

Log* Log::getInstance()
{
	if(!m_instanceFlag)
	{
		m_instance = new Log();
		m_instanceFlag = true;
	}
	return m_instance;
}

void Log::setVerbosity(LOG_VERBOSITY_LVL level)
{
	Log* ELOG = Log::getInstance();
	ELOG->m_defaultVerbosityLevel = level;
	for(CategoryVerbosity::iterator it = ELOG->m_categories.begin();
			it!= ELOG->m_categories.end();++it)
	{
		it->second = level;
	}
}

void Log::setCategoryVerbosity(LOG_CATEGORY cat, LOG_VERBOSITY_LVL level)
{
	Log* ELOG = Log::getInstance();
	ELOG->getCategory(cat)->second = level;
}

void Log::logFileName(const char* filename,bool add_date_to_name)
{
	Log* ELOG = Log::getInstance();
	std::stringstream ss;
	if(add_date_to_name)
	{
		boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
		facet->format("%Y%m%d_%H%M%S");
		ss.imbue(std::locale(std::locale::classic(), facet));
		ss << boost::posix_time::ptime(boost::posix_time::second_clock::local_time())<<"_";
	}
	ss << filename;
	ELOG->mp_logFile = new std::ofstream();
	ELOG->mp_logFile->open(ss.str().c_str(),std::ios::app);
	ELOG->m_logFileDefined = true;
	(*ELOG->mp_logFile) << std::endl;
	(*ELOG->mp_logFile) << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
	(*ELOG->mp_logFile) << "+                                EPROSIMA LOG                                  +"<<std::endl;
	(*ELOG->mp_logFile) << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
	(*ELOG->mp_logFile) << "+                            "<< boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time());
	(*ELOG->mp_logFile) << "                              +"<<std::endl;
	(*ELOG->mp_logFile) << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
	(*ELOG->mp_logFile) << std::endl;
}

LogMessage* Log::getLogMessage()
{
	mp_logMesgMutex->lock();
	if(m_logMessages.size()<MIN_N_MESSAGES)
	{
		for(uint32_t i = 0;i<MIN_N_MESSAGES;++i)
			m_logMessages.push(new LogMessage());
	}
	LogMessage* msg = m_logMessages.front();
	m_logMessages.pop();
	mp_logMesgMutex->unlock();
	return msg;
}

CategoryVerbosity::iterator Log::getCategory(LOG_CATEGORY cat)
{
	CategoryVerbosity::iterator it = m_categories.find(cat);
	if(it != m_categories.end())
		return it;
	m_categories[cat] = m_defaultVerbosityLevel;
	return m_categories.find(cat);
}


LogMessage& Log::logMessage(LOG_TYPE type, LOG_CATEGORY cat, const char* CLASS_NAME,
		const char* METHOD_NAME,const char * COLOR)
{
	Log* ELOG = Log::getInstance();
	LogMessage* log = ELOG->getLogMessage();
	log->m_type = type;
	log->m_cat = cat;
	log->m_color.str("");
	if (COLOR != nullptr)
		log->m_color << COLOR;
	else
		log->m_color << C_DEF;
	log->m_msg.str("");
	log->m_msg <<  "["<<CLASS_NAME << "::"<<METHOD_NAME;
    log->m_msg << std::left << std::setw(log->m_msg.str().size() >= MAXWIDTH ? 0 : MAXWIDTH - log->m_msg.str().size()) << "] ";
	log->m_date.str("");
	log->m_date << "["<< boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::local_time())<<"]";
	return *log;
}

LogMessage& Log::logMessage(LOG_TYPE type,const char* COLOR)
{
	Log* ELOG = Log::getInstance();
	LogMessage* log = ELOG->getLogMessage();
	log->m_type = type;
	log->m_msg.str("");
	if (COLOR != nullptr)
			log->m_color << COLOR;
		else
			log->m_color << C_DEF;
	log->m_date.str("");
	log->m_date << "["<< boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::local_time())<<"]";
	return *log;
}


void Log::addMessage(LogMessage& lm)
{
	Log* ELOG = Log::getInstance();
	ELOG->mp_printMutex->lock();
	ELOG->printLogMessage(lm);
{'fraction_non_alphanumeric': 0.239272030651341, 'fraction_numerical': 0.003927203065134099, 'mean_word_length': 5.429187192118227, 'pattern_counts': {'":': 2, '<': 153, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2442821', 'n_tokens_mistral': 4073, 'n_tokens_neox': 3861, 'n_words': 599}