Marius Vlad | 8b3ab3c | 2019-06-22 20:12:24 +0300 | [diff] [blame] | 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> |
Pekka Paalanen | c232f8d | 2019-04-05 16:09:45 +0300 | [diff] [blame] | 29 | #include "shared/helpers.h" |
Marius Vlad | 8b3ab3c | 2019-06-22 20:12:24 +0300 | [diff] [blame] | 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 * |
| 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 |
| 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 | |
Leandro Ribeiro | 1ded661 | 2020-02-06 16:43:51 -0300 | [diff] [blame] | 57 | static void |
| 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); |
Leandro Ribeiro | 23491cd | 2020-02-03 22:59:16 -0300 | [diff] [blame] | 61 | |
| 62 | weston_log_subscriber_release(subscriber); |
Leandro Ribeiro | 1ded661 | 2020-02-06 16:43:51 -0300 | [diff] [blame] | 63 | free(file); |
| 64 | } |
| 65 | |
Marius Vlad | 8b3ab3c | 2019-06-22 20:12:24 +0300 | [diff] [blame] | 66 | /** Creates a file type of subscriber |
| 67 | * |
Leandro Ribeiro | 1ded661 | 2020-02-06 16:43:51 -0300 | [diff] [blame] | 68 | * Should be destroyed using weston_log_subscriber_destroy() |
Marius Vlad | 8b3ab3c | 2019-06-22 20:12:24 +0300 | [diff] [blame] | 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 | * |
Leandro Ribeiro | 1ded661 | 2020-02-06 16:43:51 -0300 | [diff] [blame] | 73 | * @sa weston_log_subscriber_destroy |
Marius Vlad | 8b3ab3c | 2019-06-22 20:12:24 +0300 | [diff] [blame] | 74 | * |
| 75 | */ |
| 76 | WL_EXPORT struct weston_log_subscriber * |
| 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; |
Leandro Ribeiro | 1ded661 | 2020-02-06 16:43:51 -0300 | [diff] [blame] | 91 | file->base.destroy = weston_log_subscriber_destroy_log; |
Leandro Ribeiro | 8c02ea1 | 2020-02-06 15:41:32 -0300 | [diff] [blame] | 92 | file->base.destroy_subscription = NULL; |
Marius Vlad | 8b3ab3c | 2019-06-22 20:12:24 +0300 | [diff] [blame] | 93 | file->base.complete = NULL; |
| 94 | |
| 95 | wl_list_init(&file->base.subscription_list); |
| 96 | |
| 97 | return &file->base; |
| 98 | } |