RepastHPC  2.3.1
logger.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * logger.h
36  *
37  * Created on:
38  * Author: nick
39  */
40 
41 #ifndef LOGGER_H_
42 #define LOGGER_H_
43 
44 #include <string>
45 #include <vector>
46 #include <map>
47 
48 #define MAX_CONFIG_FILE_SIZE 16384
49 
50 namespace repast {
51 
52 typedef enum _LogLevel {DEBUG, INFO, WARN, ERROR, FATAL} LOG_LEVEL;
53 
54 class Appender {
55 
56 public:
57  Appender(const std::string name);
58  virtual void write(const std::string& line) = 0;
59  virtual void close() {}
60 
61  const std::string& name() const {
62  return _name;
63  }
64 
65  virtual ~Appender() = 0;
66 
67 protected:
68  const std::string _name;
69 };
70 
71 class Logger {
72 
73 public:
74  Logger(const std::string, LOG_LEVEL, int proc_id);
75 
76  void log(LOG_LEVEL, const std::string msg);
77  void close();
78  void add_appender(Appender *appender);
79 
80 private:
81  const std::string name;
82  const LOG_LEVEL level;
83  int proc_id;
84  std::vector<Appender*> appenders;
85 
86  void format_msg(LOG_LEVEL level, const std::string& msg, std::string& to_format);
87 };
88 
90 
91 public:
92  AppenderBuilder(const std::string name);
93 
94  std::string name;
95  std::string file_name;
96  long max_size;
97  int max_idx;
98 
99  Appender* build();
100 };
101 
102 class Log4CL;
103 
105 
106 private:
107  void error_warn();
108 
109  std::string error;
110  int line, proc_id;
111 
112  std::map<std::string, AppenderBuilder*> app_map;
113  std::map<std::string, Logger*> logger_map;
114  // key: logger name, value: vector of appenders names for
115  // that logger
116  std::map<std::string, std::vector<std::string>*> logger_app_map;
117 
118  int parse_level(const std::string& str) const;
119 
120  void create_root_logger(const std::string& value);
121  void create_logger(const std::string& key, const std::string& value);
122  void create_named_logger(const std::string& name, const std::string& value);
123 
124  void create_appender(const std::string& key, const std::string& value);
125  void create_appender_file(const std::string& key, const std::string& value);
126  void create_appender_size(const std::string& key, const std::string& value);
127  void create_appender_bidx(const std::string& key, const std::string& value);
128 
129  Log4CL* create_log4cl();
130 
131  AppenderBuilder* get_appender_builder(const std::string& key);
132 
133 public:
135  Log4CL* configure(const std::string& config_file, int proc_id, boost::mpi::communicator* comm = 0, int maxConfigFileSize = MAX_CONFIG_FILE_SIZE);
136 };
137 
138 class Log4CL {
139  friend class Log4CLConfigurator;
140 
141 public:
142  ~Log4CL();
143  static Log4CL* instance();
144  static void configure(int, const std::string&, boost::mpi::communicator* comm = 0, int maxConfigFileSize = MAX_CONFIG_FILE_SIZE);
145  static void configure(int);
146 
147  Logger& get_logger(std::string logger_name);
148  void close();
149 
150 protected:
151  Log4CL();
152 
153 private:
154  static Log4CL *_instance;
155 
156  std::map<std::string, Logger*> logger_map;
157  std::vector<Appender *> appenders;
158 };
159 }
160 
161 
162 #endif /* LOGGER_H_ */
repast::Log4CLConfigurator
Definition: logger.h:104
repast::AppenderBuilder
Definition: logger.h:89
repast::Logger
Definition: logger.h:71
repast::Log4CL
Definition: logger.h:138
repast::Appender
Definition: logger.h:54