1 //
2 //  Copyright (C) 2009  Nick Gasson
3 //
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.
8 //
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.
13 //
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/>.
16 //
17 
18 #ifndef INC_ILOGGER_HPP
19 #define INC_ILOGGER_HPP
20 
21 #include "Platform.hpp"
22 
23 #include <ostream>
24 
25 // Stream surrogate for writing log data to
26 struct PrintLine {
27    PrintLine(std::ostream& a_stream);
28    ~PrintLine();
29    std::ostream& stream;
30 };
31 
32 typedef shared_ptr<PrintLine> PrintLinePtr;
33 
34 template <typename T>
operator <<(PrintLinePtr a_print_line,const T & a_thing)35 inline PrintLinePtr operator<<(PrintLinePtr a_print_line, const T& a_thing)
36 {
37    using namespace std;
38    a_print_line->stream << a_thing;
39    return a_print_line;
40 }
41 
42 // Types of log levels
43 enum LogMsgType {
44    LOG_NORMAL, LOG_DEBUG, LOG_WARN, LOG_ERROR
45 };
46 
47 // Interface to logger class
48 struct ILogger {
~ILoggerILogger49    virtual ~ILogger() {}
50 
51    virtual PrintLinePtr write_msg(LogMsgType type = LOG_NORMAL) = 0;
52 };
53 
54 typedef shared_ptr<ILogger> ILoggerPtr;
55 
56 ILoggerPtr get_logger();
57 
log(LogMsgType type=LOG_NORMAL)58 inline PrintLinePtr log(LogMsgType type = LOG_NORMAL)
59 {
60    return get_logger()->write_msg(type);
61 }
62 
warn()63 inline PrintLinePtr warn() { return log(LOG_WARN); }
debug()64 inline PrintLinePtr debug() { return log(LOG_DEBUG); }
error()65 inline PrintLinePtr error() { return log(LOG_ERROR); }
66 
67 #endif
68