1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
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/>.
24 #include <cppconsui/TextView.h>
25 #include <cppconsui/Window.h>
27 #include <libpurple/purple.h>
30 #define LOG (Log::instance())
34 // Levels are 1:1 mapped to glib levels.
37 LEVEL_ERROR
, // = Fatal in libpurple.
38 LEVEL_CRITICAL
, // = Error in libpurple.
40 LEVEL_MESSAGE
, // No such level in libpurple.
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();
75 class LogWindow
: public CppConsUI::Window
{
78 virtual ~LogWindow() override
{}
81 virtual void onScreenResized() override
;
83 void append(const char *text
);
86 CppConsUI::TextView
*textview_
;
89 CONSUI_DISABLE_COPY(LogWindow
);
94 LogBufferItem(Type type
, Level level
, const char *text
);
97 Type
getType() const { return type_
; }
98 Level
getLevel() const { return level_
; }
99 const char *getText() const { return text_
; }
107 CONSUI_DISABLE_COPY(LogBufferItem
);
110 typedef std::deque
<LogBufferItem
*> LogBufferItems
;
111 LogBufferItems init_log_items_
;
112 LogBufferItems log_items_
;
114 guint default_handler_
;
116 guint gmodule_handler_
;
117 guint glib_gobject_handler_
;
118 guint gthread_handler_
;
119 guint cppconsui_handler_
;
121 static Log
*my_instance_
;
124 LogWindow
*log_window_
;
125 GIOChannel
*logfile_
;
127 Level log_level_cim_
;
128 Level log_level_glib_
;
129 Level log_level_purple_
;
133 CONSUI_DISABLE_COPY(Log
);
136 static void finalize();
137 void initNormalPhase();
138 void finalizeNormalPhase();
139 friend class CenterIM
;
141 // To catch libpurple's debug messages.
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
);
189 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: