blob: b7d8d1fe6789deb5c404b4fa84218a381f337d16 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file loggingrb.c
2 * Ringbuffer-backed logging support code. */
3/*
4 * (C) 2012-2013 by Katerina Barone-Adesi
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
Harald Welte33e940b2014-10-26 20:52:25 +01009 * the Free Software Foundation; either version 2 of the License, or
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welteefee3612017-10-16 14:37:05 +020023/*! \addtogroup loggingrb
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000024 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 * \file loggingrb.c */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000026
27#include <osmocom/core/strrb.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/core/loggingrb.h>
30
31static void _rb_output(struct log_target *target,
32 unsigned int level, const char *log)
33{
34 osmo_strrb_add(target->tgt_rb.rb, log);
35}
36
Neels Hofmeyr87e45502017-06-20 00:17:59 +020037/*! Return the number of log strings in the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000038 * \param[in] target The target to search.
39 *
40 * \return The number of log strings in the osmo_strrb-backed target.
41 */
42size_t log_target_rb_used_size(struct log_target const *target)
43{
44 return osmo_strrb_elements(target->tgt_rb.rb);
45}
46
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047/*! Return the capacity of the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000048 * \param[in] target The target to search.
49 *
50 * Note that this is the capacity (aka max number of messages).
51 * It is not the number of unused message slots.
52 * \return The number of log strings in the osmo_strrb-backed target.
53 */
54size_t log_target_rb_avail_size(struct log_target const *target)
55{
56 struct osmo_strrb *rb = target->tgt_rb.rb;
57 return rb->size - 1;
58}
59
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060/*! Return the nth log entry in a target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000061 * \param[in] target The target to search.
62 * \param[in] logindex The index of the log entry/error message.
63 *
64 * \return A pointer to the nth message, or NULL if logindex is invalid.
65 */
66const char *log_target_rb_get(struct log_target const *target, size_t logindex)
67{
68 return osmo_strrb_get_nth(target->tgt_rb.rb, logindex);
69}
70
Neels Hofmeyr87e45502017-06-20 00:17:59 +020071/*! Create a new logging target for ringbuffer-backed logging.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000072 * \param[in] size The capacity (number of messages) of the logging target.
73 * \returns A log target in case of success, NULL in case of error.
74 */
75struct log_target *log_target_create_rb(size_t size)
76{
77 struct log_target *target;
78 struct osmo_strrb *rb;
79
80 target = log_target_create();
81 if (!target)
82 return NULL;
83
84 rb = osmo_strrb_create(target, size + 1);
85 if (!rb) {
86 log_target_destroy(target);
87 return NULL;
88 }
89
90 target->tgt_rb.rb = rb;
91 target->type = LOG_TGT_TYPE_STRRB;
92 target->output = _rb_output;
93
94 return target;
95}
96
97/* @} */