SST/macro
logger.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_COMMON_LOGGER_H_INCLUDED
13 #define SSTMAC_COMMON_LOGGER_H_INCLUDED
14 
17 #include <stdint.h>
18 #include <sprockit/unordered.h>
19 
20 #include <iostream>
21 #include <deque>
22 #include <list>
23 
24 namespace sstmac {
25 
26 
27 
28 /**
29  * Base class of things which track events in the simulation, and output
30  * in different ways.
31  *
32  * Upon construction, loggers are registered in a global variable, and provide
33  * a name signature which has the form "<category> name | name2 | ...". Then
34  * loggers can be selectively turned on and off by calling set_user_param().
35  * Loggers will be turned on using any of their supplied names, and all
36  * loggers of a given category will be turned on by specifying "all" (e.g. "<debug> all")
37  *
38  * Currently supported categories are <debug>, <stats>, and <trace>
39  *
40  * Objects which are created and destroyed often should definitely use a static variable,
41  * because a lot of std::string operations have to happen to check name signatures to see if
42  * the logger is active.
43  *
44  * Also, because the logger constructed adds this to a static collection, static logger variables
45  * should first use the fake constructor as initialization, then check is_real() after the executable
46  * has started to see if they should
47  * reinstantiate themselves.
48  *
49  */
50 class logger {
51  public:
52 
53  /**
54  * Real constructor. Adds this to the static logger collection that is updated
55  * on set_user_param(), and calls update_active() in case set_user_param has already
56  * been called.
57  * @param namesig My name signature (e.g. "<debug> mpi | library | mpiserver"). It's usually a good
58  * idea to provide multiple names so you don't have to remember the exact name, and also so you can turn on
59  * whole groups of objects (like all of mpi, for instance).
60  * @param os the output stream to use.
61  * @param flush If true, the logger will always call flush() on the output stream after it writes something to it.
62  * @param color Set color with escape characters, most useful for debugging in a console.
63  */
64  logger(const std::string& namesig, std::ostream *os, bool flush = false,
65  const char* color = black);
66 
67  /**
68  * Fake constructor for instantiating static loggers before they're sure that the
69  * static logger variable has been instantiated.
70  */
71  logger() :
72  is_real_(false) {
73  }
74 
75  virtual
76  ~logger();
77 
78  /**
79  * Unfortunately, everyone who wants loggers to get turned on/off has
80  * to call this
81  * @param log
82  */
83  static void
85  loggers_.push_back(log);
86  }
87 
88  static void
90 
91  /**
92  * Indicates if this logger is turned on, which is useful
93  * for bypassing std::stringifying everything
94  * @return
95  */
96  inline bool
97  is_active(int lev = 1) const {
98  return (active_ >= lev);
99  }
100 
101  /**
102  * Static function which should be called to change
103  * which loggers are currently turned on. Calls update_all_active().
104  * @param s Signature which indicates which loggers should be turned on.
105  * For example "<debug> mpi | <stats> all | <debug> app | <trace> mpi"
106  */
107  static void
108  set_user_param(const std::string& s);
109 
110  static bool user_params_checked_;
111 
112  static void
114 
115  /**
116  * Clears all saved loggers that are updated on set_user_param.
117  * It's usually a good idea to call this in between simulations
118  * if running multiple in one executable.
119  */
120  static void
121  clear();
122 
123  virtual logger&
124  operator<<(int i);
125 
126  virtual logger&
127  operator<<(long i);
128 
129  virtual logger&
130  operator<<(long long int i);
131 
132  // note: size_t will be one of the unsigned integer types below
133  virtual logger&
134  operator<<(unsigned int i);
135 
136  virtual logger&
137  operator<<(unsigned long int i);
138 
139  virtual logger&
140  operator<<(unsigned long long int i);
141 
142  virtual logger&
143  operator<<(bool i);
144 
145  virtual logger&
146  operator<<(const char* i);
147 
148  virtual logger&
149  operator<<(double i);
150 
151  virtual logger&
152  operator<<(long double i);
153 
154  virtual logger&
155  operator<<(const std::string& i);
156 
157  virtual logger&
159 
160  virtual logger&
161  operator<<(const std::pair<int, int>& p);
162 
163  template <class T>
164  logger&
165  operator<<(const T& t){
166  (*outstream_) << t;
167  return *this;
168  }
169 
170 
171  static const char* black;
172  static const char* red;
173  static const char* green;
174  static const char* yellow;
175  static const char* blue;
176  static const char* magenta;
177  static const char* cyan;
178  static const char* white;
179 
180  /**
181  * Sets whether we should turn text coloring on
182  * (useful for debugging in a console)
183  * @param c
184  */
185  static void
187  debug_coloring_ = c;
188  }
189 
190  /**
191  * Indicates if this instance was constructed correctly.
192  * This is useful for instantiating static logger variables,
193  * and only making the real ones until the simulation has already started,
194  * when the logger static variables have been set up first (which is required
195  * for the whole logger active mechanism to work).
196  * @return true if this instance was constructed properly
197  */
198  inline bool
200  return is_real_;
201  }
202 
203  virtual std::string to_string() const;
204 
206 
207  std::string time() const;
208 
209  protected:
210 
211  static std::deque<std::string> user_params;
212  static spkt_unordered_map<std::string, bool> user_params_matched_;
213  static std::list<logger*> loggers_;
214 
215  int active_;
216 
217  std::string signature_;
218  std::string category_;
219 
220  std::ostream *outstream_;
221 
222  std::string my_color_;
223 
224  /**
225  * updates the active status of this logger based on the current user parameter
226  */
227  void
228  update_active();
229 
230  /**
231  * updates all saved loggers based on the current user parameter
232  */
233  static void
235 
236  static bool debug_coloring_;
237 
239 
240  bool is_real_;
241 
242  static bool timer_on_;
243 
244 };
245 } // end of namespace sstmac
246 #endif
247 
std::string time() const
bool is_real()
Indicates if this instance was constructed correctly.
Definition: logger.h:199
static bool timer_on_
Definition: logger.h:242
virtual ~logger()
static bool user_params_checked_
Definition: logger.h:110
static const char * black
Definition: logger.h:171
void update_active()
updates the active status of this logger based on the current user parameter
bool always_flush_
Definition: logger.h:238
static spkt_unordered_map< std::string, bool > user_params_matched_
Definition: logger.h:212
std::ostream * outstream_
Definition: logger.h:220
std::string category_
Definition: logger.h:218
static std::list< logger * > loggers_
Definition: logger.h:213
Base type for implementations of an engine that is able to schedule events and advance simulation tim...
Definition: event_manager.h:47
static void update_all_active()
updates all saved loggers based on the current user parameter
static event_manager * timer_
Definition: logger.h:205
static void set_user_param(const std::string &s)
Static function which should be called to change which loggers are currently turned on...
static const char * cyan
Definition: logger.h:177
bool is_active(int lev=1) const
Indicates if this logger is turned on, which is useful for bypassing std::stringifying everything...
Definition: logger.h:97
static const char * green
Definition: logger.h:173
Base class of things which track events in the simulation, and output in different ways...
Definition: logger.h:50
bool is_real_
Definition: logger.h:240
static const char * white
Definition: logger.h:178
static void delete_statics()
A basic container for time (subject to future transplant).
Definition: timestamp.h:29
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
virtual std::string to_string() const
static const char * magenta
Definition: logger.h:176
static void register_logger(logger *log)
Unfortunately, everyone who wants loggers to get turned on/off has to call this.
Definition: logger.h:84
static std::deque< std::string > user_params
Definition: logger.h:211
virtual logger & operator<<(int i)
logger()
Fake constructor for instantiating static loggers before they&#39;re sure that the static logger variable...
Definition: logger.h:71
std::string my_color_
Definition: logger.h:222
static bool debug_coloring_
Definition: logger.h:236
static const char * red
Definition: logger.h:172
static void check_user_params()
std::string signature_
Definition: logger.h:217
static const char * yellow
Definition: logger.h:174
static const char * blue
Definition: logger.h:175
static void set_debug_coloring(bool c)
Sets whether we should turn text coloring on (useful for debugging in a console)
Definition: logger.h:186
static void clear()
Clears all saved loggers that are updated on set_user_param.