SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
parallel_log.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
25 
26 #ifndef SHORE_PARALLEL_PARALLEL_LOG_HPP__
27 #define SHORE_PARALLEL_PARALLEL_LOG_HPP__
28 
29 #include <iostream>
30 #include <deque>
31 #include <stdexcept>
32 #include <map>
33 #include <vector>
34 
35 #include <boost/thread/thread.hpp>
36 #include <boost/thread/recursive_mutex.hpp>
37 #include <boost/thread/condition.hpp>
38 
39 #ifdef WITH_MPI
40 #include <boost/thread.hpp>
41 #include <boost/serialization/string.hpp>
42 #include <boost/serialization/vector.hpp>
43 #include <boost/archive/text_oarchive.hpp>
44 #include <boost/archive/text_iarchive.hpp>
45 #endif // WITH_MPI
46 
48 #include "shore/stream/streams.hpp"
49 #include "shore/base/memops.hpp"
50 
51 namespace shore {
52 
55 {
56  private:
57 
58  typedef boost::recursive_mutex mutex_type;
59  typedef boost::recursive_mutex::scoped_lock lock_type;
60  typedef std::map<std::string,mutex_type *> mutexmap_t;
61 
62  std::string m_filename;
63 
64  #ifdef WITH_MPI
65  // serialization for passing around as MPI message
66  friend class boost::serialization::access;
67 
68  template<class Archive>
69  void serialize(Archive& ar,const unsigned int version)
70  {
71  ar&m_filename;
72  }
73  #endif // WITH_MPI
74 
75  public:
76 
77  parallel_log(const std::string &fn="stderr");
78  parallel_log(const parallel_log &other);
79  parallel_log &operator=(const parallel_log &other);
80 
81  void set_file(const std::string &fn);
82  const std::string &get_file() const;
83  std::string get_dir() const;
84 
85  class log_lock;
86 
87  log_lock lock() const;
88 
89  class log_lock
90  {
91  private:
92 
93  friend class parallel_log;
94 
95  static mutex_type m_mastermutex;
96  static mutexmap_t m_mutexmap;
97  static shore::ptrkeeper m_cleaner;
98 
99  std::string m_filename;
100  int m_threadid;
101  mutex_type *m_mutex;
102  lock_type *m_lock;
103  shore::ostreams m_os;
104 
105 
106  void init();
107 
108  void activate();
109 
110  log_lock(const std::string &logfile);
111 
112  log_lock(const log_lock &other);
113 
114  public:
115 
116  ~log_lock();
117 
118  std::ostream &out;
119  };
120 };
121 
122 } // namespace shore
123 
124 #endif // SHORE_PARALLEL_PARALLEL_LOG_HPP__
125