OPT added to MAkefile; KanjiView::Cell constructor: fg,bg changed to int (Fl_Color...
[aoi.git] / src / logger.hxx
blobc7d13e1d6c3514eb7553b0e9fec7a6b4bb48eb66
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
21 #ifndef __LOGGER_HXX
22 #define __LOGGER_HXX
24 #include <ctime>
25 #include <string>
26 #include <vector>
27 #include <sstream>
28 #include <cstdio>
29 #include <fstream>
31 using std::string;
32 using std::vector;
34 class Logger
36 public:
37 enum MESSAGE_TYPE { MSG_NONE, MSG_MESSAGE, MSG_WARNING, MSG_ERROR, MSG_DEBUG };
38 struct LogEntry {
39 time_t time;
40 string msg;
41 MESSAGE_TYPE type;
42 /*!
43 * Returns formatted message. Format is:
44 * <b><pre>[ time from the start ] prefix message</pre></b>,
45 * where prefix is [W] - warning, [E] - error, [D] - debug or none.
46 * \return formatted message
47 */
48 string str() {
49 const char *prefix = " ";
50 switch (type){
51 case MSG_WARNING: prefix = "[W] "; break;
52 case MSG_ERROR: prefix = "[E] "; break;
53 case MSG_DEBUG: prefix = "[D] "; break;
54 default: break;
56 std::stringstream ss;
57 ss << "[" << time << " s]" << prefix << msg << std::endl;
58 return ss.str();
62 private:
63 MESSAGE_TYPE loglevel_ = MSG_ERROR;
64 vector<LogEntry> log_;
65 time_t start_;
66 string filename_;
67 std::ofstream file_;
70 public:
71 //! Constructor.
72 Logger(){
73 time(&start_); // XXX: string(ctime()) ends with '\n'
74 msg(string("Logger started at ")+string(ctime(&start_)));
75 msg(string("Logger::loglevel = ")+std::to_string(loglevel_));
77 //! Destructor
78 ~Logger(){
79 if ( file_.is_open() ){
80 time_t t;
81 time(&t);
82 file_ << "Logger closed at " << ctime(&t) << std::endl;
83 file_.close();
87 /*!
88 * Returns current log (all log entries from the start).
89 * \return All log entries from start of the logger (sorted by time).
91 inline vector<LogEntry> get_log () const { return log_; };
93 /*!
94 * Logs one message. Writes message into log file if it is set.
95 * Messages with t > loglevel will be not logged.
96 * \param s Message.
97 * \param t Type of the message.
98 * \sa LogEntry, MESSAGE_TYPE
100 void msg ( const string &s, MESSAGE_TYPE t=MSG_MESSAGE ){
101 if ( s.empty() )
102 return;
104 time_t timer;
105 time( &timer );
106 LogEntry entry = { timer-start_, s, t };
107 log_.push_back( entry );
109 if ( entry.type > loglevel_ )
110 return;
112 printf( "%s", entry.str().c_str());
114 if ( !filename_.empty() ){
115 if ( !file_.is_open() )
116 file_.open( filename_, std::ofstream::out|std::ofstream::app );
117 file_ << entry.str();
123 * Returns current loglevel.
124 * \return Current loglevel.
126 inline MESSAGE_TYPE loglevel () const { return loglevel_; }
128 * Sets loglevel.
129 * \param level new loglevel
131 inline void loglevel ( MESSAGE_TYPE level=MSG_ERROR ){
132 msg(string("Logger::loglevel set to ") + std::to_string(level) );
133 loglevel_ = level;
136 * Sets loglevel from string.
137 * If s is not recognized shows warning and keep current loglevel.
138 * \param s none, message, warning, error or debug
140 inline void loglevel ( const string &s ){
141 MESSAGE_TYPE level = MSG_MESSAGE;
142 if ( s == "none" )
143 level = MSG_NONE;
144 else if ( s == "message" )
145 level = MSG_MESSAGE;
146 else if ( s == "warning" )
147 level = MSG_WARNING;
148 else if ( s == "error" )
149 level = MSG_ERROR;
150 else if ( s == "debug" )
151 level = MSG_DEBUG;
152 else {
153 msg( "Unknown loglevel: "+s, MSG_WARNING );
154 return;
156 loglevel( level );
160 * Returns name of the log file.
161 * \return name of the log file
163 inline string filename () const { return filename_; };
165 * Sets name (path) of the log file. The file is rewritten every start of Logger.
166 * Writes whole current log to file.
167 * \param path to log file
169 inline void filename ( const string &s ) {
170 filename_=s;
171 if ( !filename_.empty() ){
172 if ( !file_.is_open() )
173 file_.open( filename_, std::ofstream::out|std::ofstream::app );
174 for ( auto &rec: log_ )
175 file_ << rec.str();
180 inline void msg ( const std::stringstream &ss, MESSAGE_TYPE t=MSG_MESSAGE )
181 { msg(ss.str(),t); }
185 #endif //__LOGGER_HXX