<commit_before>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, development version     *
*                (c) 2006-2016 INRIA, USTL, UJF, CNRS, MGH                    *
*                                                                             *
* This library 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.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* This component is open-source                                               *
*                                                                             *
* Authors: <NAME>                                                     *
*          <NAME>                                                       *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
/*****************************************************************************
* User of this library should read the documentation
* in the messaging.h file.
******************************************************************************/
#include <sstream>
using std::ostringstream ;

#include <iostream>
using std::endl ;

#include <vector>
using std::vector ;

#include <string>
using std::string ;

#include <algorithm>
using std::remove ;

#include "MessageDispatcher.h"

#include "MessageHandler.h"
#include "ConsoleMessageHandler.h"

#include <sofa/core/objectmodel/Base.h>

#include <sofa/helper/logging/RichConsoleStyleMessageFormatter.h>
using sofa::helper::logging::RichConsoleStyleMessageFormatter;

#include <mutex>
using std::lock_guard ;
using std::mutex;

namespace sofa
{

namespace helper
{

namespace logging
{

mutex s_dispatchermutex ;

static std::vector<MessageHandler*> setDefaultMessageHandler()
{
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    std::vector<MessageHandler*> messageHandlers;
    static ConsoleMessageHandler s_consoleMessageHandler(new RichConsoleStyleMessageFormatter());
    messageHandlers.push_back(&s_consoleMessageHandler);
    return messageHandlers;
}


std::vector<MessageHandler*>& MessageDispatcher::getHandlers()
{
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    static std::vector<MessageHandler*> s_handlers = setDefaultMessageHandler();
    return s_handlers;
}

int MessageDispatcher::addHandler(MessageHandler* o){
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    std::vector<MessageHandler*>& handlers = getHandlers();
    if( std::find(handlers.begin(), handlers.end(), o) == handlers.end()){
        handlers.push_back(o) ;
        return (int)(handlers.size()-1);
    }
    return -1;
}

int MessageDispatcher::rmHandler(MessageHandler* o){
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    std::vector<MessageHandler*>& handlers = getHandlers();
    handlers.erase(remove(handlers.begin(), handlers.end(), o), handlers.end());
    return (int)(handlers.size()-1);
}

void MessageDispatcher::clearHandlers(){
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    std::vector<MessageHandler*>& handlers = getHandlers();
    handlers.clear() ;
}

void MessageDispatcher::process(sofa::helper::logging::Message& m){
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    std::vector<MessageHandler*>& handlers = getHandlers();
    for( size_t i=0 ; i<handlers.size() ; i++ )
        handlers[i]->process(m) ;
}


MessageDispatcher::LoggerStream MessageDispatcher::log(Message::Class mclass, Message::Type type, const std::string& sender, const FileInfo::SPtr& fileInfo) {
    return MessageDispatcher::LoggerStream( mclass, type, sender, fileInfo);
}

MessageDispatcher::LoggerStream MessageDispatcher::log(Message::Class mclass, Message::Type type, const sofa::core::objectmodel::Base* sender, const  FileInfo::SPtr& fileInfo) {
    return MessageDispatcher::LoggerStream(mclass, type, sender, fileInfo);
}

MessageDispatcher::LoggerStream MessageDispatcher::info(Message::Class mclass, const std::string& sender, const FileInfo::SPtr& fileInfo) {
    return log(mclass, Message::Info, sender, fileInfo);
}<commit_msg>[SofaKernel] REFACTOR MessageDispatcher to go toward thread-safetiness.<commit_after>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, development version     *
*                (c) 2006-2016 INRIA, USTL, UJF, CNRS, MGH                    *
*                                                                             *
* This library 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.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* This component is open-source                                               *
*                                                                             *
* Authors: <NAME>                                                     *
*          <NAME>                                                       *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
/*****************************************************************************
* User of this library should read the documentation
* in the messaging.h file.
******************************************************************************/
#include <sstream>
using std::ostringstream ;

#include <iostream>
using std::endl ;

#include <vector>
using std::vector ;

#include <string>
using std::string ;

#include <algorithm>
using std::remove ;

#include "MessageDispatcher.h"

#include "MessageHandler.h"
#include "ConsoleMessageHandler.h"

#include <sofa/core/objectmodel/Base.h>

#include <sofa/helper/logging/RichConsoleStyleMessageFormatter.h>
using sofa::helper::logging::RichConsoleStyleMessageFormatter;

#include <mutex>
using std::lock_guard ;
using std::mutex;

namespace sofa
{

namespace helper
{

namespace logging
{

static ConsoleMessageHandler s_consoleMessageHandler(new RichConsoleStyleMessageFormatter());

class MessageDispatcherImpl
{
public:
    std::vector<MessageHandler*> m_messageHandlers { &s_consoleMessageHandler } ;

    std::vector<MessageHandler*>& getHandlers()
    {
        return m_messageHandlers ;
    }

    int addHandler(MessageHandler* o)
    {
        if( std::find(m_messageHandlers.begin(), m_messageHandlers.end(), o) == m_messageHandlers.end())
        {
            m_messageHandlers.push_back(o) ;
            return (int)(m_messageHandlers.size()-1);
        }
        return -1;
    }

    int rmHandler(MessageHandler* o)
    {
        m_messageHandlers.erase(remove(m_messageHandlers.begin(), m_messageHandlers.end(), o), m_messageHandlers.end());
        return (int)(m_messageHandlers.size()-1);
    }

    void clearHandlers()
    {
        m_messageHandlers.clear() ;
    }

    void process(sofa::helper::logging::Message& m)
    {
        for( size_t i=0 ; i<m_messageHandlers.size() ; i++ )
            m_messageHandlers[i]->process(m) ;
    }
};


mutex s_dispatchermutex ;
MessageDispatcherImpl s_messagedispatcher ;

/*
static std::vector<MessageHandler*> setDefaultMessageHandler()
{
    /// This static function of the dispatcher has to be protected against concurent access
    /// This is done by a mutex and a scoped_guard as in Use the http://en.cppreference.com/w/cpp/thread/lock_guard
    //lock_guard<mutex> guard(s_dispatchermutex) ;

    std::vector<MessageHandler*> messageHandlers;
    messageHandlers.push_back(&s_consoleMessageHandler);
    return messageHandlers;
}*/

std::vector<MessageHandler*>& MessageDispatcher::getHandlers()
{
    lock_guard<mutex> guard(s_dispatchermutex) ;

    return s_messagedispatcher.getHandlers();
}

int MessageDispatcher::addHandler(MessageHandler* o){
    lock_guard<mutex> guard(s_dispatchermutex) ;

    return s_messagedispatcher.addHandler(o);
}

int MessageDispatcher::rmHandler(MessageHandler* o){
    lock_guard<mutex> guard(s_dispatchermutex) ;

    return s_messagedispatcher.rmHandler(o);
}

void MessageDispatcher::clearHandlers(){
    lock_guard<mutex> guard(s_dispatchermutex) ;

    s_messagedispatcher.clearHandlers();
}

void MessageDispatcher::process(sofa::helper::logging::Message& m){
    lock_guard<mutex> guard(s_dispatchermutex) ;

    s_messagedispatcher.process(m);
}

MessageDispatcher::LoggerStream MessageDispatcher::log(Message::Class mclass, Message::Type type, const std::string& sender, const FileInfo::SPtr& fileInfo) {
    return MessageDispatcher::LoggerStream( mclass, type, sender, fileInfo);
}

MessageDispatcher::LoggerStream MessageDispatcher::log(Message::Class mclass, Message::Type type, const sofa::core::objectmodel::Base* sender, const  FileInfo::SPtr& fileInfo) {
    return MessageDispatcher::LoggerStream(mclass, type, sender, fileInfo);
}

MessageDispatcher::LoggerStream MessageDispatcher::info(Message::Class mclass, const std::string& sender, const FileInfo::SPtr& fileInfo) {
    return log(mclass, Message::Info, sender, fileInfo);
}
{'fraction_non_alphanumeric': 0.15186787259142745, 'fraction_numerical': 0.003932363350373574, 'mean_word_length': 2.577940348902645, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136825', 'n_tokens_mistral': 3179, 'n_tokens_neox': 3022, 'n_words': 858}