blob: 64033fc3f7c1a5c40e2fdad4bacbe63696833987 [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
Harald Welte33e940b2014-10-26 20:52:25 +01008 * the Free Software Foundation; either version 2 of the License, or
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00009 * (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
Harald Welte96e2a002017-06-12 21:44:18 +020026/*! \file loggingrb.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027 * libosmocore logging backend for a ring-buffer of last log messages
Harald Welte96e2a002017-06-12 21:44:18 +020028 */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000029
30#include <osmocom/core/strrb.h>
31#include <osmocom/core/logging.h>
32#include <osmocom/core/loggingrb.h>
33
34static void _rb_output(struct log_target *target,
35 unsigned int level, const char *log)
36{
37 osmo_strrb_add(target->tgt_rb.rb, log);
38}
39
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040/*! Return the number of log strings in the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000041 * \param[in] target The target to search.
42 *
43 * \return The number of log strings in the osmo_strrb-backed target.
44 */
45size_t log_target_rb_used_size(struct log_target const *target)
46{
47 return osmo_strrb_elements(target->tgt_rb.rb);
48}
49
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050/*! Return the capacity of the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000051 * \param[in] target The target to search.
52 *
53 * Note that this is the capacity (aka max number of messages).
54 * It is not the number of unused message slots.
55 * \return The number of log strings in the osmo_strrb-backed target.
56 */
57size_t log_target_rb_avail_size(struct log_target const *target)
58{
59 struct osmo_strrb *rb = target->tgt_rb.rb;
60 return rb->size - 1;
61}
62
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063/*! Return the nth log entry in a target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000064 * \param[in] target The target to search.
65 * \param[in] logindex The index of the log entry/error message.
66 *
67 * \return A pointer to the nth message, or NULL if logindex is invalid.
68 */
69const char *log_target_rb_get(struct log_target const *target, size_t logindex)
70{
71 return osmo_strrb_get_nth(target->tgt_rb.rb, logindex);
72}
73
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074/*! Create a new logging target for ringbuffer-backed logging.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000075 * \param[in] size The capacity (number of messages) of the logging target.
76 * \returns A log target in case of success, NULL in case of error.
77 */
78struct log_target *log_target_create_rb(size_t size)
79{
80 struct log_target *target;
81 struct osmo_strrb *rb;
82
83 target = log_target_create();
84 if (!target)
85 return NULL;
86
87 rb = osmo_strrb_create(target, size + 1);
88 if (!rb) {
89 log_target_destroy(target);
90 return NULL;
91 }
92
93 target->tgt_rb.rb = rb;
94 target->type = LOG_TGT_TYPE_STRRB;
95 target->output = _rb_output;
96
97 return target;
98}
99
100/* @} */