Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / math / include_private / boost / math / tools / iteration_logger.hpp
1 //  Copyright John Maddock 2014.
2 //  Use, modification and distribution are subject to the
3 //  Boost Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_MATH_TOOLS_ITERATION_LOGGER
7 #define BOOST_MATH_TOOLS_ITERATION_LOGGER
8
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <iostream>
13 #include <iomanip>
14 #include <fstream>
15 #include <boost/current_function.hpp>
16
17 namespace boost{ namespace math{ namespace detail{
18
19 struct logger
20 {
21    std::map<std::pair<std::string, std::string>, unsigned long long> data;
22
23    ~logger()
24    {
25       for(std::map<std::pair<std::string, std::string>, unsigned long long>::const_iterator i = data.begin(); i != data.end(); ++i)
26       {
27          std::cout << "Logging iteration data:\n  file: " << i->first.first
28             << "\n  function: " << i->first.second
29             << "\n  count: " << i->second << std::endl;
30          //
31          // Read in existing data:
32          //
33          std::map<std::string, unsigned long long> file_data;
34          std::string filename = i->first.first + ".logfile";
35          std::ifstream is(filename.c_str());
36          while(is.good())
37          {
38             std::string f;
39             unsigned long long c;
40             is >> std::ws;
41             std::getline(is, f);
42             is >> c;
43             if(f.size())
44                file_data[f] = c;
45          }
46          is.close();
47          if(file_data.find(i->first.second) != file_data.end())
48             file_data[i->first.second] += i->second;
49          else
50             file_data[i->first.second] = i->second;
51          //
52          // Write it out again:
53          //
54          std::ofstream os(filename.c_str());
55          for(std::map<std::string, unsigned long long>::const_iterator j = file_data.begin(); j != file_data.end(); ++j)
56             os << j->first << "\n    " << j->second << std::endl;
57          os.close();
58       }
59    }
60 };
61
62 inline void log_iterations(const char* file, const char* function, unsigned long long count)
63 {
64    static logger l;
65    std::pair<std::string, std::string> key(file, function);
66    if(l.data.find(key) == l.data.end())
67       l.data[key] = 0;
68    l.data[key] += count;
69 }
70
71 #define BOOST_MATH_LOG_COUNT(count) boost::math::detail::log_iterations(__FILE__, BOOST_CURRENT_FUNCTION, count);
72
73
74 }}} // namespaces
75
76 #endif // BOOST_MATH_TOOLS_ITERATION_LOGGER
77