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 logv( enum Level level
, const char *fmt
, va_list args
);
62 void clearAllBufferedMessages();
77 class LogWindow
: public CppConsUI::Window
{
80 virtual ~LogWindow() override
{}
83 virtual void onScreenResized() override
;
85 void append(const char *text
);
88 CppConsUI::TextView
*textview_
;
91 CONSUI_DISABLE_COPY(LogWindow
);
96 LogBufferItem(Type type
, Level level
, const char *text
);
99 Type
getType() const { return type_
; }
100 Level
getLevel() const { return level_
; }
101 const char *getText() const { return text_
; }
109 CONSUI_DISABLE_COPY(LogBufferItem
);
112 typedef std::deque
<LogBufferItem
*> LogBufferItems
;
113 LogBufferItems init_log_items_
;
114 LogBufferItems log_items_
;
116 guint default_handler_
;
118 guint gmodule_handler_
;
119 guint glib_gobject_handler_
;
120 guint gthread_handler_
;
121 guint cppconsui_handler_
;
123 static Log
*my_instance_
;
126 LogWindow
*log_window_
;
127 GIOChannel
*logfile_
;
129 Level log_level_cim_
;
130 Level log_level_glib_
;
131 Level log_level_purple_
;
135 CONSUI_DISABLE_COPY(Log
);
138 static void finalize();
139 void initNormalPhase();
140 void finalizeNormalPhase();
141 friend class CenterIM
;
143 // To catch libpurple's debug messages.
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
);
191 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: