LCOV - code coverage report
Current view: top level - dbe/src/internal - messenger.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 0.0 % 54 0
Test Date: 2025-12-21 13:07:08 Functions: 0.0 % 13 0

            Line data    Source code
       1              : /*
       2              :  * messenger.cpp
       3              :  *
       4              :  *  Created on: Nov 3, 2015
       5              :  *      Author: Leonidas Georgopoulos
       6              :  */
       7              : 
       8              : #include "logging/Logging.hpp"
       9              : 
      10              : #include "dbe/messenger.hpp"
      11              : #include "dbe/messenger_proxy.hpp"
      12              : 
      13              : #include <map>
      14              : #include <cassert>
      15              : 
      16              : namespace dbe
      17              : {
      18              : namespace interface
      19              : {
      20              : namespace messenger
      21              : {
      22              : 
      23              : //------------------------------------------------------------------------------------------
      24              : msglevels::t_str const msglevels::debug = "DEBUG";
      25              : msglevels::t_str const msglevels::info = "INFO";
      26              : msglevels::t_str const msglevels::note = "NOTE";
      27              : msglevels::t_str const msglevels::warn = "WARN";
      28              : msglevels::t_str const msglevels::error = "ERROR";
      29              : msglevels::t_str const msglevels::fail = "FAIL";
      30              : 
      31              : msglevels::t_levels const msglevels::all_levels = {debug, info, note, warn, error, fail};
      32              : // provides codes from message level type ( level -> code )
      33              : static std::map<qt::t_str const, messages const> codes =
      34              : {
      35              :   { msglevels::info, messages::DEBUG},
      36              :   { msglevels::info, messages::INFO },
      37              :   { msglevels::note, messages::NOTE },
      38              :   { msglevels::warn, messages::WARN },
      39              :   { msglevels::error, messages::ERROR },
      40              :   { msglevels::fail, messages::FAIL }
      41              : };
      42              : 
      43              : // provides level types from code ( code->level)
      44              : static std::map<messages const, qt::t_str const> levels =
      45              : {
      46              :   { messages::DEBUG, msglevels::debug },
      47              :   { messages::INFO, msglevels::info },
      48              :   { messages::NOTE, msglevels::note },
      49              :   { messages::WARN, msglevels::warn },
      50              :   { messages::ERROR, msglevels::error },
      51              :   { messages::FAIL, msglevels::fail }
      52              : };
      53              : 
      54              : // provides titles from level types
      55              : static std::map<qt::t_str const, qt::t_str const> titles =
      56              : {
      57              :   { msglevels::debug, "Debug info" },
      58              :   { msglevels::info, "Information" },
      59              :   { msglevels::note, "Notice" },
      60              :   { msglevels::warn, "Warning" },
      61              :   { msglevels::error, "Error" },
      62              :   { msglevels::fail, "Failure" }
      63              : };
      64              : 
      65              : std::mutex qt::m_block;
      66              : std::atomic<bool> qt::m_batch_mode ( false );
      67              : qt::t_batches qt::m_batches;
      68              : //------------------------------------------------------------------------------------------
      69              : 
      70              : //------------------------------------------------------------------------------------------
      71            0 : qt::post_ret_type qt::post ( t_str const & m, t_str const & l )
      72              : {
      73            0 :   std::lock_guard<std::mutex> lock ( m_block );
      74              : 
      75            0 :   if ( m_batch_mode )
      76              :   {
      77            0 :     merge_post ( m, l );
      78              :   }
      79              :   else
      80              :   {
      81            0 :     direct_post ( m, l );
      82              :   }
      83            0 : }
      84              : //------------------------------------------------------------------------------------------
      85              : 
      86              : //------------------------------------------------------------------------------------------
      87            0 : qt::post_ret_type qt::direct_post ( t_str const & m, t_str const & l )
      88              : {
      89            0 :   if ( warn == l )
      90              :   {
      91            0 :     TLOG_DEBUG(0) <<  static_cast<int> ( messages::WARN ) <<  m ;
      92            0 :     messenger_proxy::ref().warn ( titles[warn], m );
      93              :   }
      94            0 :   else if ( error == l )
      95              :   {
      96            0 :     TLOG_DEBUG(0) << static_cast<int> ( messages::ERROR ) << m ;
      97            0 :     messenger_proxy::ref().error ( titles[error], m );
      98              :   }
      99            0 :   else if ( info == l )
     100              :   {
     101            0 :     TLOG_DEBUG(0) << static_cast<int> ( messages::INFO ) << m ;
     102            0 :     messenger_proxy::ref().info ( titles[info], m );
     103              :   }
     104            0 :   else if ( fail == l )
     105              :   {
     106            0 :     TLOG_DEBUG(0) << static_cast<int> ( messages::FAIL ) << m ;
     107            0 :     messenger_proxy::ref().fail ( titles[fail], m );
     108              :   }
     109            0 :   else if ( note == l )
     110              :   {
     111            0 :     TLOG_DEBUG(0) << static_cast<int> ( messages::NOTE ) << m ;
     112            0 :     messenger_proxy::ref().fail ( titles[note], m );
     113              :   }
     114            0 :   else if ( debug == l )
     115              :   {
     116            0 :     TLOG_DEBUG(0) << static_cast<int> ( messages::DEBUG ) << m ;
     117            0 :     messenger_proxy::ref().fail ( titles[debug], m );
     118              :   }
     119            0 : }
     120              : //------------------------------------------------------------------------------------------
     121              : 
     122              : //------------------------------------------------------------------------------------------
     123            0 : qt::post_ret_type qt::merge_post ( t_str const & m, t_str const & l )
     124              : {
     125            0 :   auto codeit = codes.find ( l );
     126            0 :   assert ( codeit != codes.end() );
     127            0 :   m_batches[static_cast<int> ( codeit->second )].insert ( m );
     128            0 : }
     129              : //------------------------------------------------------------------------------------------
     130              : 
     131              : //------------------------------------------------------------------------------------------
     132            0 : void qt::purge()
     133              : {
     134            0 :   for ( auto clevel = m_batches.begin(); clevel != m_batches.end(); ++clevel )
     135              :   {
     136            0 :     if ( not clevel->empty() )
     137              :     {
     138            0 :         t_str merged ( "Summary of messages during batch mode" );
     139              : 
     140            0 :         for(auto it = clevel->begin(); it != clevel->end(); ) {
     141            0 :             auto cnt = clevel->count(*it);
     142              : 
     143            0 :             t_str cmessage = *it;
     144            0 :             merged += t_str ( "\n\n" ) + cmessage + t_str ( "\n\n #Occurances=" ) + std::to_string(cnt);
     145              : 
     146            0 :             std::advance(it, cnt);
     147            0 :         }
     148              : 
     149            0 :         direct_post ( merged, levels[static_cast<messages> ( clevel - m_batches.begin() )] );
     150            0 :         clevel->clear();
     151            0 :     }
     152              :   }
     153            0 : }
     154              : //------------------------------------------------------------------------------------------
     155              : 
     156              : //------------------------------------------------------------------------------------------
     157            0 : std::unique_ptr<batch_guard> qt::batchmode()
     158              : {
     159            0 :   return std::unique_ptr<batch_guard> ( new batch_guard() );
     160              : }
     161              : //------------------------------------------------------------------------------------------
     162              : 
     163              : //------------------------------------------------------------------------------------------
     164            0 : batch_guard::~batch_guard()
     165              : {
     166            0 :   qt::m_batch_mode = false;
     167            0 :   qt::purge();
     168            0 : }
     169              : //------------------------------------------------------------------------------------------
     170              : 
     171              : //------------------------------------------------------------------------------------------
     172            0 : batch_guard::batch_guard()
     173              : {
     174            0 :   qt::m_batch_mode = true;
     175            0 : }
     176              : //------------------------------------------------------------------------------------------
     177              : 
     178              : }// namespace messenger
     179              : } /* namespace interface */
     180              : } /* namespace dbe */
        

Generated by: LCOV version 2.0-1