1 static const char version[] = "$Id: test_stream2.c,v 1.5 2012/10/04 22:51:28 valtri Exp $";
2
3 /*
4 * test_category.c
5 *
6 * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
7 *
8 * See the COPYING file for the terms of usage and distribution.
9 */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <log4c/appender.h>
16 #include <log4c/appender_type_stream2.h>
17
18 #include <log4c/layout.h>
19 #include <log4c/category.h>
20 #include <log4c/init.h>
21 #include <sd/test.h>
22 #include <sd/factory.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 static log4c_category_t* root = NULL;
27 static log4c_category_t* sub1 = NULL;
28 static log4c_category_t* sub1sub2 = NULL;
29
30 /******************************************************************************
31 The stream2 appender is used as follows:
32
33 1. Get an appender of type "stream2":
34 stream2_appender = log4c_appender_get(<appender name>);
35 log4c_appender_set_type(stream2_appender,
36 log4c_appender_type_get("stream2"));
37
38 2a.Go straight into logging with this appender by setting it as the appender
39 for a category:
40 log4c_category_set_appender(root, stream2_appender);
41
42 In this case the appender will be configured automatically with default
43 values:
44 . the filename is the same as the <appender name>
45 . the file is opened in "w+" mode
46 . the default system buffer is used (cf; setbuf() )
47
48 OR
49
50 2b. Configure the appender using the stream2 setter functions and then
51 go into logging by setting the appender as the appender for a category:
52
53 If you set a file pointer yourself you are reposnsible for opening,
54 setting any opttions and closing the file pointer. In this case the name of
55 the log file will be the file name of myfp and not the name of the appender:
56
57 log4c_stream2_set_fp(stream2_appender,myfp);
58 log4c_category_set_appender(root, stream2_appender);
59
60 By default the stream2 file pointer is in buffered mode.
61 If you want to set the mode for the stream2 file pointer to unbuffered then
62 do it this way:
63
64 log4c_stream2_set_flags(stream2_appender, LOG4C_STREAM2_UNBUFFERED);
65 log4c_category_set_appender(root, stream2_appender);
66
67 I you are using your own file pointer then you can set the buffer directly
68 and you do not need to use this option.
69
70
71 */
72
73
74 /******************************************************************************/
log4c_print(FILE * a_fp)75 static void log4c_print(FILE* a_fp)
76 {
77 extern sd_factory_t* log4c_category_factory;
78 extern sd_factory_t* log4c_appender_factory;
79 extern sd_factory_t* log4c_layout_factory;
80
81 sd_factory_print(log4c_category_factory, a_fp); fprintf(a_fp, "\n");
82 sd_factory_print(log4c_appender_factory, a_fp); fprintf(a_fp, "\n");
83 sd_factory_print(log4c_layout_factory, a_fp); fprintf(a_fp, "\n");
84 }
85
86 /******************************************************************************/
87 #define foo(cat, level) \
88 { \
89 fprintf(sd_test_out(a_test), "\n# "#cat" "#level" (priority = %s)\n", \
90 log4c_priority_to_string(log4c_category_get_priority(cat))); \
91 log4c_category_##level(cat, #cat" "#level); \
92 }
93
94 /******************************************************************************/
95
test0(sd_test_t * a_test,int argc,char * argv[])96 static int test0(sd_test_t* a_test, int argc, char* argv[])
97 {
98 log4c_print(sd_test_out(a_test));
99 return 1;
100 }
101
102 /******************************************************************************/
103 /*
104 This tests case 2b above (setting our own file pointer)
105 */
test1(sd_test_t * a_test,int argc,char * argv[])106 static int test1(sd_test_t* a_test, int argc, char* argv[])
107 {
108 log4c_appender_t* stream2_appender = NULL;
109
110 log4c_category_set_priority(root, LOG4C_PRIORITY_TRACE);
111
112 stream2_appender =
113 log4c_appender_get("stream2_appender");
114 log4c_appender_set_type(stream2_appender,
115 log4c_appender_type_get("stream2"));
116 log4c_stream2_set_fp(stream2_appender, sd_test_out(a_test));
117 log4c_category_set_appender(root, stream2_appender);
118
119 foo(root, error);
120 foo(root, warn);
121
122 return 1;
123 }
124
125 /******************************************************************************/
126 /*
127 This tests case 2b above (setting the fp mode to unbuffered)
128 */
test2(sd_test_t * a_test,int argc,char * argv[])129 static int test2(sd_test_t* a_test, int argc, char* argv[])
130 {
131 log4c_appender_t* stream2_appender = NULL;
132 int flags;
133
134 log4c_category_set_priority(sub1, LOG4C_PRIORITY_TRACE);
135
136 stream2_appender =
137 log4c_appender_get("stream2_appender");
138 log4c_appender_set_type(stream2_appender, &log4c_appender_type_stream2);
139 log4c_stream2_set_flags(stream2_appender,LOG4C_STREAM2_UNBUFFERED);
140 flags = log4c_stream2_get_flags(stream2_appender);
141 fprintf(sd_test_out(a_test), "stream2 flags '%d'\n",flags);
142 log4c_category_set_appender(sub1, stream2_appender);
143
144 foo(sub1, error);
145 foo(sub1, warn);
146
147 return 1;
148 }
149
150 /******************************************************************************/
151 /*
152 This tests case 2a above (use stream2 default config values)
153 */
test3(sd_test_t * a_test,int argc,char * argv[])154 static int test3(sd_test_t* a_test, int argc, char* argv[])
155 {
156 log4c_appender_t* stream2_appender = NULL;
157 char testlogname[1024];
158
159 log4c_category_set_priority(root, LOG4C_PRIORITY_TRACE);
160
161 sprintf(testlogname, "%s.log", sd_test_get_name(a_test));
162 stream2_appender =
163 log4c_appender_get(testlogname);
164 log4c_appender_set_type(stream2_appender,
165 log4c_appender_type_get("stream2"));
166 log4c_category_set_appender(root, stream2_appender);
167
168 foo(root, error);
169 fprintf(sd_test_out(a_test),"# check %s for this message\n",
170 testlogname);
171 foo(root, warn);
172 fprintf(sd_test_out(a_test),"# check %s for this message\n",
173 testlogname);
174 return 1;
175 }
176
177 /******************************************************************************/
178 /*
179 This tests case tests opening and closing the stream2
180 several times....to test for mem leaks or corruption
181 */
test4(sd_test_t * a_test,int argc,char * argv[])182 static int test4(sd_test_t* a_test, int argc, char* argv[])
183 {
184 log4c_appender_t* stream2_appender = NULL;
185 char testlogname[1024];
186
187 log4c_category_set_priority(root, LOG4C_PRIORITY_TRACE);
188
189 sprintf(testlogname, "%s.1", sd_test_get_name(a_test));
190 stream2_appender =
191 log4c_appender_get(testlogname);
192 log4c_appender_set_type(stream2_appender,
193 log4c_appender_type_get("stream2"));
194 log4c_category_set_appender(root, stream2_appender);
195
196 foo(root, error);
197 fprintf(sd_test_out(a_test),"# check %s for this message\n",
198 testlogname);
199 foo(root, warn);
200 fprintf(sd_test_out(a_test),"# check %s for this message\n",
201 testlogname);
202
203 /* Now close it....*/
204 log4c_appender_close(stream2_appender );
205
206 /* Now open it....*/
207 foo(root, warn);
208 fprintf(sd_test_out(a_test),"# check %s for this message\n",
209 testlogname);
210 foo(root, warn);
211 fprintf(sd_test_out(a_test),"# check %s for this message\n",
212 testlogname);
213
214 /* Now close it....*/
215 log4c_appender_close(stream2_appender );
216
217 /* Now open it....*/
218 foo(root, info);
219 fprintf(sd_test_out(a_test),"# check %s for this message\n",
220 testlogname);
221 foo(root, warn);
222 fprintf(sd_test_out(a_test),"# check %s for this message\n",
223 testlogname);
224 /* Now close it....*/
225
226 return 1;
227 }
228
229 /******************************************************************************/
main(int argc,char * argv[])230 int main(int argc, char* argv[])
231 {
232 int ret;
233 sd_test_t* t = sd_test_new(argc, argv);
234
235 if ( log4c_init() > 0 ) {
236 fprintf(stderr, "Failed to init log4c...exiting\n");
237 exit(1);
238 }
239
240 root = log4c_category_get("root");
241 sub1 = log4c_category_get("sub1");
242 sub1sub2 = log4c_category_get("sub1.sub2");
243
244 sd_test_add(t, test0);
245 sd_test_add(t, test1);
246 sd_test_add(t, test2);
247 sd_test_add(t, test3);
248 sd_test_add(t, test4);
249
250 ret = sd_test_run(t, argc, argv);
251
252 sd_test_delete(t);
253
254 log4c_fini();
255
256 return ! ret;
257 }
258