1 /*
2 * Copyright © 2019 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <libweston/weston-log.h>
29 #include "shared/helpers.h"
30 #include <libweston/libweston.h>
31
32 #include "weston-log-internal.h"
33
34 #include <stdio.h>
35
36 /** File type of stream
37 */
38 struct weston_debug_log_file {
39 struct weston_log_subscriber base;
40 FILE *file;
41 };
42
43 static struct weston_debug_log_file *
to_weston_debug_log_file(struct weston_log_subscriber * sub)44 to_weston_debug_log_file(struct weston_log_subscriber *sub)
45 {
46 return container_of(sub, struct weston_debug_log_file, base);
47 }
48
49 static void
weston_log_file_write(struct weston_log_subscriber * sub,const char * data,size_t len)50 weston_log_file_write(struct weston_log_subscriber *sub,
51 const char *data, size_t len)
52 {
53 struct weston_debug_log_file *stream = to_weston_debug_log_file(sub);
54 fwrite(data, len, 1, stream->file);
55 }
56
57 static void
weston_log_subscriber_destroy_log(struct weston_log_subscriber * subscriber)58 weston_log_subscriber_destroy_log(struct weston_log_subscriber *subscriber)
59 {
60 struct weston_debug_log_file *file = to_weston_debug_log_file(subscriber);
61
62 weston_log_subscriber_release(subscriber);
63 free(file);
64 }
65
66 /** Creates a file type of subscriber
67 *
68 * Should be destroyed using weston_log_subscriber_destroy()
69 *
70 * @param dump_to if specified, used for writing data to
71 * @returns a weston_log_subscriber object or NULL in case of failure
72 *
73 * @sa weston_log_subscriber_destroy
74 *
75 */
76 WL_EXPORT struct weston_log_subscriber *
weston_log_subscriber_create_log(FILE * dump_to)77 weston_log_subscriber_create_log(FILE *dump_to)
78 {
79 struct weston_debug_log_file *file = zalloc(sizeof(*file));
80
81 if (!file)
82 return NULL;
83
84 if (dump_to)
85 file->file = dump_to;
86 else
87 file->file = stderr;
88
89
90 file->base.write = weston_log_file_write;
91 file->base.destroy = weston_log_subscriber_destroy_log;
92 file->base.destroy_subscription = NULL;
93 file->base.complete = NULL;
94
95 wl_list_init(&file->base.subscription_list);
96
97 return &file->base;
98 }
99