doxygen comments
[aoi.git] / src / logger.hxx
blob8691cc95add2fef7b067f8abbea48a3fae62a69c
1 /*
2 Copyright 2013 Karel Matas
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /*! \file logger.hxx
19 * \date 2013/06/18
22 #ifndef __LOGGER_HXX
23 #define __LOGGER_HXX
25 #include <ctime>
26 #include <string>
27 #include <vector>
28 #include <sstream>
29 #include <cstdio>
30 #include <fstream>
32 using std::string;
33 using std::vector;
35 class Logger
37 public:
38 enum MESSAGE_TYPE { MSG_NONE, MSG_MESSAGE, MSG_WARNING, MSG_ERROR, MSG_DEBUG };
39 struct LogEntry {
40 time_t time;
41 string msg;
42 MESSAGE_TYPE type;
43 /*!
44 * Returns formatted message. Format is:
45 * <b><pre>[ time from the start ] prefix message</pre></b>,
46 * where prefix is [W] - warning, [E] - error, [D] - debug or none.
47 * \return formatted message
48 */
49 string str() {
50 const char *prefix = " ";
51 switch (type){
52 case MSG_WARNING: prefix = "[W] "; break;
53 case MSG_ERROR: prefix = "[E] "; break;
54 case MSG_DEBUG: prefix = "[D] "; break;
55 default: break;
57 std::stringstream ss;
58 ss << "[" << time << " s]" << prefix << msg << std::endl;
59 return ss.str();
63 private:
64 MESSAGE_TYPE loglevel_ = MSG_ERROR;
65 vector<LogEntry> log_;
66 time_t start_;
67 string filename_;
68 std::ofstream file_;
71 public:
72 //! Constructor.
73 Logger(){
74 time(&start_); // XXX: string(ctime()) ends with '\n'
75 msg(string("Logger started at ")+string(ctime(&start_)));
76 msg(string("Logger::loglevel = ")+std::to_string(loglevel_));
78 //! Destructor
79 ~Logger(){
80 if ( file_.is_open() ){
81 time_t t;
82 time(&t);
83 file_ << "Logger closed at " << ctime(&t) << std::endl;
84 file_.close();
88 /*!
89 * Returns current log (all log entries from the start).
90 * \return All log entries from start of the logger (sorted by time).
92 inline vector<LogEntry> get_log () const { return log_; };
94 /*!
95 * Logs one message. Writes message into log file if it is set.
96 * Messages with t > loglevel will be not logged.
97 * \param s Message.
98 * \param t Type of the message.
99 * \sa LogEntry, MESSAGE_TYPE
101 void msg ( const string &s, MESSAGE_TYPE t=MSG_MESSAGE ){
102 if ( s.empty() )
103 return;
105 time_t timer;
106 time( &timer );
107 LogEntry entry = { timer-start_, s, t };
108 log_.push_back( entry );
110 if ( entry.type > loglevel_ )
111 return;
113 printf( "%s", entry.str().c_str());
115 if ( !filename_.empty() ){
116 if ( !file_.is_open() )
117 file_.open( filename_, std::ofstream::out|std::ofstream::app );
118 file_ << entry.str();
124 * Returns current loglevel.
125 * \return Current loglevel.
127 inline MESSAGE_TYPE loglevel () const { return loglevel_; }
129 * Sets loglevel.
130 * \param level new loglevel
132 inline void loglevel ( MESSAGE_TYPE level=MSG_ERROR ){
133 msg(string("Logger::loglevel set to ") + std::to_string(level) );
134 loglevel_ = level;
137 * Sets loglevel from string.
138 * If s is not recognized shows warning and keep current loglevel.
139 * \param s none, message, warning, error or debug
141 inline void loglevel ( const string &s ){
142 MESSAGE_TYPE level = MSG_MESSAGE;
143 if ( s == "none" )
144 level = MSG_NONE;
145 else if ( s == "message" )
146 level = MSG_MESSAGE;
147 else if ( s == "warning" )
148 level = MSG_WARNING;
149 else if ( s == "error" )
150 level = MSG_ERROR;
151 else if ( s == "debug" )
152 level = MSG_DEBUG;
153 else {
154 msg( "Unknown loglevel: "+s, MSG_WARNING );
155 return;
157 loglevel( level );
161 * Returns name of the log file.
162 * \return name of the log file
164 inline string filename () const { return filename_; };
166 * Sets name (path) of the log file. The file is rewritten every start of Logger.
167 * Writes whole current log to file.
168 * \param path to log file
170 inline void filename ( const string &s ) {
171 filename_=s;
172 if ( !filename_.empty() ){
173 if ( !file_.is_open() )
174 file_.open( filename_, std::ofstream::out|std::ofstream::app );
175 for ( auto &rec: log_ )
176 file_ << rec.str();
181 inline void msg ( const std::stringstream &ss, MESSAGE_TYPE t=MSG_MESSAGE )
182 { msg(ss.str(),t); }
186 #endif //__LOGGER_HXX