blob: 2cdb247d3df96e2b9f92356aa4b9430826a51e8f [file] [log] [blame]
Marius Vlad8b3ab3c2019-06-22 20:12:24 +03001/*
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>
Pekka Paalanenc232f8d2019-04-05 16:09:45 +030029#include "shared/helpers.h"
Marius Vlad8b3ab3c2019-06-22 20:12:24 +030030#include <libweston/libweston.h>
31
32#include "weston-log-internal.h"
33
34#include <stdio.h>
35
36/** File type of stream
37 */
38struct weston_debug_log_file {
39 struct weston_log_subscriber base;
40 FILE *file;
41};
42
43static struct weston_debug_log_file *
44to_weston_debug_log_file(struct weston_log_subscriber *sub)
45{
46 return container_of(sub, struct weston_debug_log_file, base);
47}
48
49static void
50weston_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/** Creates a file type of subscriber
58 *
59 * Should be destroyed using weston_log_subscriber_destroy_log()
60 *
61 * @param dump_to if specified, used for writing data to
62 * @returns a weston_log_subscriber object or NULL in case of failure
63 *
64 * @sa weston_log_subscriber_destroy_log
65 *
66 */
67WL_EXPORT struct weston_log_subscriber *
68weston_log_subscriber_create_log(FILE *dump_to)
69{
70 struct weston_debug_log_file *file = zalloc(sizeof(*file));
71
72 if (!file)
73 return NULL;
74
75 if (dump_to)
76 file->file = dump_to;
77 else
78 file->file = stderr;
79
80
81 file->base.write = weston_log_file_write;
82 file->base.destroy = NULL;
83 file->base.complete = NULL;
84
85 wl_list_init(&file->base.subscription_list);
86
87 return &file->base;
88}
89
90/** Destroy the subscriber created with weston_log_subscriber_create_log
91 *
92 * @param subscriber the weston_log_subscriber object to destroy
93 *
94 */
95WL_EXPORT void
96weston_log_subscriber_destroy_log(struct weston_log_subscriber *subscriber)
97{
98 struct weston_debug_log_file *file = to_weston_debug_log_file(subscriber);
99 free(file);
100}