blob: 25e36b46a1600f1b82813054b2d86b4dcdc1313f [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
Leandro Ribeiro1ded6612020-02-06 16:43:51 -030057static void
58weston_log_subscriber_destroy_log(struct weston_log_subscriber *subscriber)
59{
60 struct weston_debug_log_file *file = to_weston_debug_log_file(subscriber);
Leandro Ribeiro23491cd2020-02-03 22:59:16 -030061
62 weston_log_subscriber_release(subscriber);
Leandro Ribeiro1ded6612020-02-06 16:43:51 -030063 free(file);
64}
65
Marius Vlad8b3ab3c2019-06-22 20:12:24 +030066/** Creates a file type of subscriber
67 *
Leandro Ribeiro1ded6612020-02-06 16:43:51 -030068 * Should be destroyed using weston_log_subscriber_destroy()
Marius Vlad8b3ab3c2019-06-22 20:12:24 +030069 *
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 *
Leandro Ribeiro1ded6612020-02-06 16:43:51 -030073 * @sa weston_log_subscriber_destroy
Marius Vlad8b3ab3c2019-06-22 20:12:24 +030074 *
75 */
76WL_EXPORT struct weston_log_subscriber *
77weston_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;
Leandro Ribeiro1ded6612020-02-06 16:43:51 -030091 file->base.destroy = weston_log_subscriber_destroy_log;
Leandro Ribeiro8c02ea12020-02-06 15:41:32 -030092 file->base.destroy_subscription = NULL;
Marius Vlad8b3ab3c2019-06-22 20:12:24 +030093 file->base.complete = NULL;
94
95 wl_list_init(&file->base.subscription_list);
96
97 return &file->base;
98}