Update list of wide characters
[centerim5.git] / src / Log.h
blob551fa471cc5c932bc9c360208d9a12afc9b3fb85
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 logv( enum Level level, const char *fmt, va_list args );
62 void clearAllBufferedMessages();
64 private:
65 enum Type {
66 TYPE_CIM,
67 TYPE_GLIB,
68 TYPE_PURPLE,
71 enum Phase {
72 PHASE_INITIALIZATION,
73 PHASE_NORMAL,
74 PHASE_FINALIZATION,
77 class LogWindow : public CppConsUI::Window {
78 public:
79 LogWindow();
80 virtual ~LogWindow() override {}
82 // FreeWindow
83 virtual void onScreenResized() override;
85 void append(const char *text);
87 protected:
88 CppConsUI::TextView *textview_;
90 private:
91 CONSUI_DISABLE_COPY(LogWindow);
94 class LogBufferItem {
95 public:
96 LogBufferItem(Type type, Level level, const char *text);
97 ~LogBufferItem();
99 Type getType() const { return type_; }
100 Level getLevel() const { return level_; }
101 const char *getText() const { return text_; }
103 protected:
104 Type type_;
105 Level level_;
106 char *text_;
108 private:
109 CONSUI_DISABLE_COPY(LogBufferItem);
112 typedef std::deque<LogBufferItem *> LogBufferItems;
113 LogBufferItems init_log_items_;
114 LogBufferItems log_items_;
116 guint default_handler_;
117 guint glib_handler_;
118 guint gmodule_handler_;
119 guint glib_gobject_handler_;
120 guint gthread_handler_;
121 guint cppconsui_handler_;
123 static Log *my_instance_;
125 Phase phase_;
126 LogWindow *log_window_;
127 GIOChannel *logfile_;
129 Level log_level_cim_;
130 Level log_level_glib_;
131 Level log_level_purple_;
133 Log();
134 ~Log();
135 CONSUI_DISABLE_COPY(Log);
137 static void init();
138 static void finalize();
139 void initNormalPhase();
140 void finalizeNormalPhase();
141 friend class CenterIM;
143 // To catch libpurple's debug messages.
144 void purple_print(
145 PurpleDebugLevel level, const char *category, const char *arg_s);
146 gboolean purple_is_enabled(PurpleDebugLevel level, const char *category);
148 // To catch default messages.
149 static void default_log_handler_(const char *domain, GLogLevelFlags flags,
150 const char *msg, gpointer user_data)
152 reinterpret_cast<Log *>(user_data)->default_log_handler(domain, flags, msg);
154 void default_log_handler(
155 const char *domain, GLogLevelFlags flags, const char *msg);
157 // To catch glib's messages.
158 static void glib_log_handler_(const char *domain, GLogLevelFlags flags,
159 const char *msg, gpointer user_data)
161 reinterpret_cast<Log *>(user_data)->glib_log_handler(domain, flags, msg);
163 void glib_log_handler(
164 const char *domain, GLogLevelFlags flags, const char *msg);
166 // Called when log preferences change.
167 static void log_pref_change_(
168 const char *name, PurplePrefType type, gconstpointer val, gpointer data)
170 reinterpret_cast<Log *>(data)->log_pref_change(name, type, val);
172 void log_pref_change(
173 const char *name, PurplePrefType type, gconstpointer val);
175 void updateCachedPreference(const char *name);
176 void write(Type type, Level level, const char *text, bool buffer = true);
177 void writeErrorToWindow(const char *fmt, ...);
178 void writeToFile(const char *text);
179 void bufferMessage(Type type, Level level, const char *text);
180 void clearBufferedMessages(LogBufferItems &items);
181 void outputBufferedMessages(LogBufferItems &items);
182 void outputAllBufferedMessages();
183 Level convertPurpleDebugLevel(PurpleDebugLevel purplelevel);
184 Level convertGLibDebugLevel(GLogLevelFlags gliblevel);
185 Level stringToLevel(const char *slevel);
186 Level getLogLevel(Type type);
189 #endif // LOG_H
191 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: