blob: 4a80cc8aea9037a5d9d4107abcab6acade563c04 [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 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
Harald Welte33e940b2014-10-26 20:52:25 +010011 * the Free Software Foundation; either version 2 of the License, or
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
Harald Welteefee3612017-10-16 14:37:05 +020025/*! \addtogroup loggingrb
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000026 * @{
Harald Weltedf8c57f2017-10-16 14:37:31 +020027 * This adds a log which consist of an in-memory ring buffer. The idea
28 * is that the user can configure his logging in a way that critical
29 * messages get stored in the ring buffer, and that the last few
30 * critical messages can then always obtained by dumping the ring
31 * buffer. It can hence be used as a more generic version of the
32 * "show me the last N alarms" functionality.
33 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020034 * \file loggingrb.c */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000035
36#include <osmocom/core/strrb.h>
37#include <osmocom/core/logging.h>
38#include <osmocom/core/loggingrb.h>
39
40static void _rb_output(struct log_target *target,
41 unsigned int level, const char *log)
42{
43 osmo_strrb_add(target->tgt_rb.rb, log);
44}
45
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046/*! Return the number of log strings in the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000047 * \param[in] target The target to search.
48 *
49 * \return The number of log strings in the osmo_strrb-backed target.
50 */
51size_t log_target_rb_used_size(struct log_target const *target)
52{
53 return osmo_strrb_elements(target->tgt_rb.rb);
54}
55
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056/*! Return the capacity of the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000057 * \param[in] target The target to search.
58 *
59 * Note that this is the capacity (aka max number of messages).
60 * It is not the number of unused message slots.
61 * \return The number of log strings in the osmo_strrb-backed target.
62 */
63size_t log_target_rb_avail_size(struct log_target const *target)
64{
65 struct osmo_strrb *rb = target->tgt_rb.rb;
66 return rb->size - 1;
67}
68
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069/*! Return the nth log entry in a target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000070 * \param[in] target The target to search.
71 * \param[in] logindex The index of the log entry/error message.
72 *
73 * \return A pointer to the nth message, or NULL if logindex is invalid.
74 */
75const char *log_target_rb_get(struct log_target const *target, size_t logindex)
76{
77 return osmo_strrb_get_nth(target->tgt_rb.rb, logindex);
78}
79
Neels Hofmeyr87e45502017-06-20 00:17:59 +020080/*! Create a new logging target for ringbuffer-backed logging.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000081 * \param[in] size The capacity (number of messages) of the logging target.
82 * \returns A log target in case of success, NULL in case of error.
83 */
84struct log_target *log_target_create_rb(size_t size)
85{
86 struct log_target *target;
87 struct osmo_strrb *rb;
88
89 target = log_target_create();
90 if (!target)
91 return NULL;
92
93 rb = osmo_strrb_create(target, size + 1);
94 if (!rb) {
95 log_target_destroy(target);
96 return NULL;
97 }
98
99 target->tgt_rb.rb = rb;
100 target->type = LOG_TGT_TYPE_STRRB;
101 target->output = _rb_output;
102
103 return target;
104}
105
106/* @} */