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