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/>.
38 enum MESSAGE_TYPE
{ MSG_NONE
, MSG_MESSAGE
, MSG_WARNING
, MSG_ERROR
, MSG_DEBUG
};
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
50 const char *prefix
= " ";
52 case MSG_WARNING
: prefix
= "[W] "; break;
53 case MSG_ERROR
: prefix
= "[E] "; break;
54 case MSG_DEBUG
: prefix
= "[D] "; break;
58 ss
<< "[" << time
<< " s]" << prefix
<< msg
<< std::endl
;
64 MESSAGE_TYPE loglevel_
= MSG_ERROR
;
65 vector
<LogEntry
> log_
;
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_
));
80 if ( file_
.is_open() ){
83 file_
<< "Logger closed at " << ctime(&t
) << std::endl
;
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_
; };
95 * Logs one message. Writes message into log file if it is set.
96 * Messages with t > loglevel will be not logged.
98 * \param t Type of the message.
99 * \sa LogEntry, MESSAGE_TYPE
101 void msg ( const string
&s
, MESSAGE_TYPE t
=MSG_MESSAGE
){
107 LogEntry entry
= { timer
-start_
, s
, t
};
108 log_
.push_back( entry
);
110 if ( entry
.type
> loglevel_
)
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_
; }
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
) );
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
;
145 else if ( s
== "message" )
147 else if ( s
== "warning" )
149 else if ( s
== "error" )
151 else if ( s
== "debug" )
154 msg( "Unknown loglevel: "+s
, MSG_WARNING
);
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
) {
172 if ( !filename_
.empty() ){
173 if ( !file_
.is_open() )
174 file_
.open( filename_
, std::ofstream::out
|std::ofstream::app
);
175 for ( auto &rec
: log_
)
181 inline void msg ( const std::stringstream
&ss
, MESSAGE_TYPE t
=MSG_MESSAGE
)
186 #endif //__LOGGER_HXX