Use correct package prefix for local macros in configure.ac
[centerim5.git] / src / Log.h
blobc456a69a93623e53911f66819339d1bb52587c94
1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 //
4 // This file is part of CenterIM.
5 //
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef LOG_H
20 #define LOG_H
22 #include "CenterIM.h"
24 #include <cppconsui/TextView.h>
25 #include <cppconsui/Window.h>
26 #include <deque>
27 #include <libpurple/purple.h>
28 #include <string>
30 #define LOG (Log::instance())
32 class Log {
33 public:
34 // Levels are 1:1 mapped to glib levels.
35 enum Level {
36 LEVEL_NONE, //
37 LEVEL_ERROR, // = Fatal in libpurple.
38 LEVEL_CRITICAL, // = Error in libpurple.
39 LEVEL_WARNING, //
40 LEVEL_MESSAGE, // No such level in libpurple.
41 LEVEL_INFO, //
42 LEVEL_DEBUG, // = Misc in libpurple.
45 static Log *instance();
47 void error(const char *fmt, ...)
48 CPPCONSUI_GNUC_ATTRIBUTE((format(printf, 2, 3)));
49 void critical(const char *fmt, ...)
50 CPPCONSUI_GNUC_ATTRIBUTE((format(printf, 2, 3)));
51 void warning(const char *fmt, ...)
52 CPPCONSUI_GNUC_ATTRIBUTE((format(printf, 2, 3)));
53 void message(const char *fmt, ...)
54 CPPCONSUI_GNUC_ATTRIBUTE((format(printf, 2, 3)));
55 void info(const char *fmt, ...)
56 CPPCONSUI_GNUC_ATTRIBUTE((format(printf, 2, 3)));
57 void debug(const char *fmt, ...)
58 CPPCONSUI_GNUC_ATTRIBUTE((format(printf, 2, 3)));
60 void clearAllBufferedMessages();
62 private:
63 enum Type {
64 TYPE_CIM,
65 TYPE_GLIB,
66 TYPE_PURPLE,
69 enum Phase {
70 PHASE_INITIALIZATION,
71 PHASE_NORMAL,
72 PHASE_FINALIZATION,
75 class LogWindow : public CppConsUI::Window {
76 public:
77 LogWindow();
78 virtual ~LogWindow() override {}
80 // FreeWindow
81 virtual void onScreenResized() override;
83 void append(const char *text);
85 protected:
86 CppConsUI::TextView *textview_;
88 private:
89 CONSUI_DISABLE_COPY(LogWindow);
92 class LogBufferItem {
93 public:
94 LogBufferItem(Type type, Level level, const char *text);
95 ~LogBufferItem();
97 Type getType() const { return type_; }
98 Level getLevel() const { return level_; }
99 const char *getText() const { return text_; }
101 protected:
102 Type type_;
103 Level level_;
104 char *text_;
106 private:
107 CONSUI_DISABLE_COPY(LogBufferItem);
110 typedef std::deque<LogBufferItem *> LogBufferItems;
111 LogBufferItems init_log_items_;
112 LogBufferItems log_items_;
114 guint default_handler_;
115 guint glib_handler_;
116 guint gmodule_handler_;
117 guint glib_gobject_handler_;
118 guint gthread_handler_;
119 guint cppconsui_handler_;
121 static Log *my_instance_;
123 Phase phase_;
124 LogWindow *log_window_;
125 GIOChannel *logfile_;
127 Level log_level_cim_;
128 Level log_level_glib_;
129 Level log_level_purple_;
131 Log();
132 ~Log();
133 CONSUI_DISABLE_COPY(Log);
135 static void init();
136 static void finalize();
137 void initNormalPhase();
138 void finalizeNormalPhase();
139 friend class CenterIM;
141 // To catch libpurple's debug messages.
142 void purple_print(
143 PurpleDebugLevel level, const char *category, const char *arg_s);
144 gboolean purple_is_enabled(PurpleDebugLevel level, const char *category);
146 // To catch default messages.
147 static void default_log_handler_(const char *domain, GLogLevelFlags flags,
148 const char *msg, gpointer user_data)
150 reinterpret_cast<Log *>(user_data)->default_log_handler(domain, flags, msg);
152 void default_log_handler(
153 const char *domain, GLogLevelFlags flags, const char *msg);
155 // To catch glib's messages.
156 static void glib_log_handler_(const char *domain, GLogLevelFlags flags,
157 const char *msg, gpointer user_data)
159 reinterpret_cast<Log *>(user_data)->glib_log_handler(domain, flags, msg);
161 void glib_log_handler(
162 const char *domain, GLogLevelFlags flags, const char *msg);
164 // Called when log preferences change.
165 static void log_pref_change_(
166 const char *name, PurplePrefType type, gconstpointer val, gpointer data)
168 reinterpret_cast<Log *>(data)->log_pref_change(name, type, val);
170 void log_pref_change(
171 const char *name, PurplePrefType type, gconstpointer val);
173 void updateCachedPreference(const char *name);
174 void write(Type type, Level level, const char *text, bool buffer = true);
175 void writeErrorToWindow(const char *fmt, ...);
176 void writeToFile(const char *text);
177 void bufferMessage(Type type, Level level, const char *text);
178 void clearBufferedMessages(LogBufferItems &items);
179 void outputBufferedMessages(LogBufferItems &items);
180 void outputAllBufferedMessages();
181 Level convertPurpleDebugLevel(PurpleDebugLevel purplelevel);
182 Level convertGLibDebugLevel(GLogLevelFlags gliblevel);
183 Level stringToLevel(const char *slevel);
184 Level getLogLevel(Type type);
187 #endif // LOG_H
189 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: