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/>.
37 enum MESSAGE_TYPE
{ MSG_NONE
, MSG_MESSAGE
, MSG_WARNING
, MSG_ERROR
, MSG_DEBUG
};
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
49 const char *prefix
= " ";
51 case MSG_WARNING
: prefix
= "[W] "; break;
52 case MSG_ERROR
: prefix
= "[E] "; break;
53 case MSG_DEBUG
: prefix
= "[D] "; break;
57 ss
<< "[" << time
<< " s]" << prefix
<< msg
<< std::endl
;
63 MESSAGE_TYPE loglevel_
= MSG_ERROR
;
64 vector
<LogEntry
> log_
;
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_
));
79 if ( file_
.is_open() ){
82 file_
<< "Logger closed at " << ctime(&t
) << std::endl
;
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_
; };
94 * Logs one message. Writes message into log file if it is set.
95 * Messages with t > loglevel will be not logged.
97 * \param t Type of the message.
98 * \sa LogEntry, MESSAGE_TYPE
100 void msg ( const string
&s
, MESSAGE_TYPE t
=MSG_MESSAGE
){
106 LogEntry entry
= { timer
-start_
, s
, t
};
107 log_
.push_back( entry
);
109 if ( entry
.type
> loglevel_
)
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_
; }
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
) );
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
;
144 else if ( s
== "message" )
146 else if ( s
== "warning" )
148 else if ( s
== "error" )
150 else if ( s
== "debug" )
153 msg( "Unknown loglevel: "+s
, MSG_WARNING
);
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
) {
171 if ( !filename_
.empty() ){
172 if ( !file_
.is_open() )
173 file_
.open( filename_
, std::ofstream::out
|std::ofstream::app
);
174 for ( auto &rec
: log_
)
180 inline void msg ( const std::stringstream
&ss
, MESSAGE_TYPE t
=MSG_MESSAGE
)
185 #endif //__LOGGER_HXX