1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  *  Copyright (C) 2012-2013  Kouhei Sutou <kou@clear-code.com>
4  *
5  *  This library 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 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This library 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
16  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include <gcutter.h>
21 
22 #include <cutter/cut-logger.h>
23 #include <cutter/cut-enum-types.h>
24 
25 #define CUT_LOG_DOMAIN "logger-test"
26 
27 void data_level_from_string (void);
28 void test_level_from_string (gconstpointer data);
29 void test_error (void);
30 void test_critical (void);
31 void test_message (void);
32 void test_warning (void);
33 void test_debug (void);
34 void test_info (void);
35 void test_console_output (void);
36 void test_target_level (void);
37 void test_interesting_level (void);
38 
39 static CutLogger *logger;
40 
41 static const gchar *logged_domain;
42 static CutLogLevelFlags logged_level;
43 static const gchar *logged_message;
44 static const gchar *logged_file;
45 static guint logged_line;
46 static const gchar *logged_function;
47 static GString *stdout_string;
48 static gboolean end_of_log;
49 
50 static CutLogLevelFlags original_log_level;
51 static GPrintFunc original_print_hander;
52 
53 static gboolean default_handler_disconnected;
54 
55 static void
cb_log(CutLogger * logger,const gchar * domain,CutLogLevelFlags level,const gchar * file,guint line,const gchar * function,GTimeVal * time_value,const gchar * message,gpointer user_data)56 cb_log (CutLogger *logger,
57         const gchar *domain, CutLogLevelFlags level,
58         const gchar *file, guint line, const gchar *function,
59         GTimeVal *time_value, const gchar *message,
60         gpointer user_data)
61 {
62     logged_domain = cut_take_strdup(domain);
63     logged_level = level;
64     logged_file = cut_take_strdup(file);
65     logged_line = line;
66     logged_function = cut_take_strdup(function);
67 
68     logged_message = cut_take_strdup(message);
69 }
70 
71 
72 void
setup(void)73 setup (void)
74 {
75     logger = NULL;
76 
77     logged_domain = NULL;
78     logged_level = 0;
79     logged_message = NULL;
80 
81     stdout_string = g_string_new(NULL);
82     end_of_log = FALSE;
83 
84     original_print_hander = NULL;
85 
86     original_log_level = cut_get_log_level();
87     cut_set_log_level(CUT_LOG_LEVEL_ALL);
88     default_handler_disconnected = FALSE;
89 
90     g_signal_connect(cut_logger(), "log", G_CALLBACK(cb_log), NULL);
91 }
92 
93 void
teardown(void)94 teardown (void)
95 {
96     if (logger)
97         g_object_unref(logger);
98 
99     cut_set_log_level(original_log_level);
100     if (default_handler_disconnected)
101         cut_logger_connect_default_handler(cut_logger());
102 
103     if (original_print_hander)
104         g_set_print_handler(original_print_hander);
105 
106     g_signal_handlers_disconnect_by_func(cut_logger(),
107                                          G_CALLBACK(cb_log), NULL);
108     g_string_free(stdout_string, TRUE);
109 }
110 
111 void
data_level_from_string(void)112 data_level_from_string (void)
113 {
114 #define ADD(label, expected, level_string)                              \
115     gcut_add_datum(label,                                               \
116                    "/expected", CUT_TYPE_LOG_LEVEL_FLAGS, expected,  \
117                    "/level-string", G_TYPE_STRING, level_string,        \
118                    NULL)
119 
120     ADD("all", CUT_LOG_LEVEL_ALL, "all");
121     ADD("one", CUT_LOG_LEVEL_INFO, "info");
122     ADD("multi",
123         CUT_LOG_LEVEL_ERROR | CUT_LOG_LEVEL_CRITICAL,
124         "error|critical");
125     ADD("append",
126         CUT_LOG_LEVEL_CRITICAL |
127         CUT_LOG_LEVEL_ERROR |
128         CUT_LOG_LEVEL_WARNING |
129         CUT_LOG_LEVEL_MESSAGE |
130         CUT_LOG_LEVEL_INFO |
131         CUT_LOG_LEVEL_DEBUG,
132         "+info|debug");
133     ADD("remove",
134         CUT_LOG_LEVEL_CRITICAL |
135         CUT_LOG_LEVEL_MESSAGE,
136         "-error|warning");
137 
138 #undef ADD
139 }
140 
141 void
test_level_from_string(gconstpointer data)142 test_level_from_string (gconstpointer data)
143 {
144     const gchar *level_string;
145     CutLogLevelFlags expected, actual;
146     GError *error = NULL;
147 
148     expected = gcut_data_get_flags(data, "/expected");
149     level_string = gcut_data_get_string(data, "/level-string");
150     actual = cut_log_level_flags_from_string(level_string,
151                                              CUT_LOG_LEVEL_DEFAULT,
152                                              &error);
153     gcut_assert_error(error);
154     gcut_assert_equal_flags(CUT_TYPE_LOG_LEVEL_FLAGS,
155                             expected,
156                             actual);
157 }
158 
159 #define cut_assert_equal_log(level, message)                        \
160     cut_assert_equal_string(CUT_LOG_DOMAIN, logged_domain);         \
161     gcut_assert_equal_flags(CUT_TYPE_LOG_LEVEL_FLAGS,               \
162                             level,                                  \
163                             logged_level);                          \
164     cut_assert_equal_string(__FILE__, logged_file);                 \
165     cut_assert_equal_string(__PRETTY_FUNCTION__, logged_function);  \
166     cut_assert_equal_uint(line, logged_line);                       \
167     cut_assert_equal_string(message, logged_message);
168 
169 static void
disconnect_default_handler(void)170 disconnect_default_handler (void)
171 {
172     cut_logger_disconnect_default_handler(cut_logger());
173     default_handler_disconnected = TRUE;
174 }
175 
176 void
test_error(void)177 test_error (void)
178 {
179     guint line;
180 
181     disconnect_default_handler();
182     cut_log_error("error"); line = __LINE__;
183     cut_assert_equal_log(CUT_LOG_LEVEL_ERROR, "error");
184 }
185 
186 void
test_critical(void)187 test_critical (void)
188 {
189     guint line;
190 
191     disconnect_default_handler();
192     cut_log_critical("critical"); line = __LINE__;
193     cut_assert_equal_log(CUT_LOG_LEVEL_CRITICAL, "critical");
194 }
195 
196 void
test_message(void)197 test_message (void)
198 {
199     guint line;
200 
201     disconnect_default_handler();
202     cut_log_message("message"); line = __LINE__;
203     cut_assert_equal_log(CUT_LOG_LEVEL_MESSAGE, "message");
204 }
205 
206 void
test_warning(void)207 test_warning (void)
208 {
209     guint line;
210 
211     disconnect_default_handler();
212     cut_log_warning("warning"); line = __LINE__;
213     cut_assert_equal_log(CUT_LOG_LEVEL_WARNING, "warning");
214 }
215 
216 void
test_debug(void)217 test_debug (void)
218 {
219     guint line;
220 
221     disconnect_default_handler();
222     cut_log_debug("debug"); line = __LINE__;
223     cut_assert_equal_log(CUT_LOG_LEVEL_DEBUG, "debug");
224 }
225 
226 void
test_info(void)227 test_info (void)
228 {
229     guint line;
230 
231     disconnect_default_handler();
232     cut_log_info("info"); line = __LINE__;
233     cut_assert_equal_log(CUT_LOG_LEVEL_INFO, "info");
234 }
235 
236 static void
print_handler(const gchar * string)237 print_handler (const gchar *string)
238 {
239     if (g_strrstr(string, "end-of-log"))
240         end_of_log = TRUE;
241     g_string_append(stdout_string, string);
242 }
243 
244 void
test_console_output(void)245 test_console_output (void)
246 {
247     original_print_hander = g_set_print_handler(print_handler);
248     cut_set_log_level(CUT_LOG_LEVEL_INFO);
249     cut_log_info("info");
250     cut_log_info("end-of-log");
251     g_set_print_handler(original_print_hander);
252     original_print_hander = NULL;
253 
254     cut_assert_match("info", stdout_string->str);
255 }
256 
257 void
test_target_level(void)258 test_target_level (void)
259 {
260     cut_set_log_level(CUT_LOG_LEVEL_DEFAULT);
261 
262     logger = cut_logger_new();
263     g_signal_connect(logger, "log",
264                      G_CALLBACK(cut_logger_default_log_handler), NULL);
265 
266     original_print_hander = g_set_print_handler(print_handler);
267     cut_logger_log(logger, "domain", CUT_LOG_LEVEL_INFO,
268                    "file", 29, "function",
269                    "message");
270     g_set_print_handler(original_print_hander);
271     original_print_hander = NULL;
272     cut_assert_equal_string("", stdout_string->str);
273 
274     cut_logger_set_target_level(logger, CUT_LOG_LEVEL_INFO);
275     original_print_hander = g_set_print_handler(print_handler);
276     cut_logger_log(logger, "domain", CUT_LOG_LEVEL_INFO,
277                    "file", 29, "function",
278                    "message");
279     g_set_print_handler(original_print_hander);
280     original_print_hander = NULL;
281     cut_assert_match("message", stdout_string->str);
282 }
283 
284 void
test_interesting_level(void)285 test_interesting_level (void)
286 {
287     logger = cut_logger_new();
288     gcut_assert_equal_flags(CUT_TYPE_LOG_LEVEL_FLAGS,
289                             CUT_LOG_LEVEL_MESSAGE |
290                             CUT_LOG_LEVEL_WARNING |
291                             CUT_LOG_LEVEL_ERROR |
292                             CUT_LOG_LEVEL_CRITICAL,
293                             cut_logger_get_interesting_level(logger));
294 
295     cut_logger_set_target_level(logger,
296                                 CUT_LOG_LEVEL_INFO | CUT_LOG_LEVEL_TRACE);
297     gcut_assert_equal_flags(CUT_TYPE_LOG_LEVEL_FLAGS,
298                             CUT_LOG_LEVEL_INFO | CUT_LOG_LEVEL_TRACE,
299                             cut_logger_get_interesting_level(logger));
300 
301     cut_logger_set_interesting_level(logger,
302                                      "syslog",
303                                      CUT_LOG_LEVEL_DEBUG);
304     gcut_assert_equal_flags(CUT_TYPE_LOG_LEVEL_FLAGS,
305                             CUT_LOG_LEVEL_INFO |
306                             CUT_LOG_LEVEL_DEBUG |
307                             CUT_LOG_LEVEL_TRACE,
308                             cut_logger_get_interesting_level(logger));
309 }
310 
311 /*
312 vi:ts=4:nowrap:ai:expandtab:sw=4
313 */
314