Merge remote branch 'origin/master'
[oweals/minetest.git] / src / log.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef LOG_HEADER
21 #define LOG_HEADER
22
23 #include <string>
24
25 /*
26         Use this for logging everything.
27         
28         If you need to explicitly print something, use dstream or cout or cerr.
29 */
30
31 enum LogMessageLevel {
32         LMT_ERROR, /* Something failed ("invalid map data on disk, block (2,2,1)") */
33         LMT_ACTION, /* In-game actions ("celeron55 placed block at (12,4,-5)") */
34         LMT_INFO, /* More deep info ("saving map on disk (only_modified=true)") */
35         LMT_VERBOSE, /* Flood-style ("loaded block (2,2,2) from disk") */
36         LMT_NUM_VALUES,
37 };
38
39 class ILogOutput
40 {
41 public:
42         /* line: Full line with timestamp, level and thread */
43         virtual void printLog(const std::string &line){};
44         /* line: Full line with timestamp, level and thread */
45         virtual void printLog(const std::string &line, enum LogMessageLevel lev){};
46         /* line: Only actual printed text */
47         virtual void printLog(enum LogMessageLevel lev, const std::string &line){};
48 };
49
50 void log_add_output(ILogOutput *out, enum LogMessageLevel lev);
51 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev);
52 void log_add_output_all_levs(ILogOutput *out);
53 void log_remove_output(ILogOutput *out);
54
55 void log_register_thread(const std::string &name);
56 void log_deregister_thread();
57
58 void log_printline(enum LogMessageLevel lev, const std::string &text);
59
60 #define LOGLINEF(lev, ...)\
61 {\
62         char buf[10000];\
63         snprintf(buf, 10000, __VA_ARGS__);\
64         log_printline(lev, buf);\
65 }
66
67 extern std::ostream errorstream;
68 extern std::ostream actionstream;
69 extern std::ostream infostream;
70 extern std::ostream verbosestream;
71
72 extern bool log_trace_level_enabled;
73
74 #define TRACESTREAM(x){ if(log_trace_level_enabled) verbosestream x; }
75 #define TRACEDO(x){ if(log_trace_level_enabled){ x ;} }
76
77 #endif
78