////////////////////////////////////////////////////////////////////////////////
// 
// Statistics.cc
//
//    produced: 17 Sep 2021 jr
//
////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <thread>
#include <cstdlib>

#include "Statistics.hh"

namespace topcom {

  std::fstream               Statistics::_stats_stream;
  Statistics::table_type     Statistics::_data_table;

  // initialization:
  void Statistics::init() {
    if (CommandlineOptions::output_stats()) {
      MessageStreams::verbose() << message::lock
				<< "\t initializing statistics output ..." << '\n'
				<< "\t ... output file: " << CommandlineOptions::stats_file() << " ..." << std::endl
				<< message::unlock;
      Statistics::stats_stream().open(CommandlineOptions::stats_file(), std::ios::out | std::ios::trunc);
      Statistics::stats_stream() << "//////////////////////////////////////////////////////////////////////////////" << std::endl;
      Statistics::stats_stream() << "// main thread statistics file (automatically generated by TOPCOM)" << std::endl;
      Statistics::stats_stream() << "//////////////////////////////////////////////////////////////////////////////" << std::endl;
      Statistics::stats_stream() << std::endl;
      MessageStreams::verbose() << message::lock
				<< "\t ... done." << std::endl
				<< message::unlock;
    }
  }

  // termination:
  void Statistics::term() {
    if (CommandlineOptions::output_stats()) {
      MessageStreams::verbose() << message::lock
				<< "\t terminating statistics output ..." << std::endl
				<< message::unlock;
      Statistics::stats_stream().close();
      MessageStreams::verbose() << message::lock
				<< "\t ... done." << std::endl
				<< message::unlock;
    }
  }

  // report:
  void Statistics::report() {
    if (CommandlineOptions::output_stats()) {
      MessageStreams::verbose() << message::lock
				<< "\t reporting statistic values ..." << std::endl
				<< message::unlock;
      Statistics::stats_stream() << "report statistics:" << std::endl;
      for (Statistics::table_type::const_iterator iter = _data_table.begin();
	   iter != _data_table.end();
	   ++iter) {
	Statistics::stats_stream() << iter->first << ": \t" << iter->second << '\n';
      }
      MessageStreams::verbose() << message::lock
				<< "\t ... done." << std::endl
				<< message::unlock;
    }
  }
  
  // reset:
  void Statistics::reset() {
    if (CommandlineOptions::output_stats()) {
      MessageStreams::verbose() << message::lock
				<< "\t reset statistic values ..." << std::endl
				<< message::unlock;
      Statistics::stats_stream() << "reset statistics:" << std::endl;
      for (Statistics::table_type::iterator iter = _data_table.begin();
	   iter != _data_table.end();
	   ++iter) {
	iter->second = 0;
      }
      MessageStreams::verbose() << message::lock
				<< "\t ... done." << std::endl
				<< message::unlock;
    }
  }

  
}; // namespace topcom

// eof CommandlineOptions.cc
