blob: d3dd2543a6c6e9415b2af26638618fdfa2cab0b3 [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 * @{
Harald Weltedf8c57f2017-10-16 14:37:31 +020025 * This adds a log which consist of an in-memory ring buffer. The idea
26 * is that the user can configure his logging in a way that critical
27 * messages get stored in the ring buffer, and that the last few
28 * critical messages can then always obtained by dumping the ring
29 * buffer. It can hence be used as a more generic version of the
30 * "show me the last N alarms" functionality.
31 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020032 * \file loggingrb.c */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000033
34#include <osmocom/core/strrb.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/loggingrb.h>
37
38static void _rb_output(struct log_target *target,
39 unsigned int level, const char *log)
40{
41 osmo_strrb_add(target->tgt_rb.rb, log);
42}
43
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044/*! Return the number of log strings in the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000045 * \param[in] target The target to search.
46 *
47 * \return The number of log strings in the osmo_strrb-backed target.
48 */
49size_t log_target_rb_used_size(struct log_target const *target)
50{
51 return osmo_strrb_elements(target->tgt_rb.rb);
52}
53
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054/*! Return the capacity of the osmo_strrb-backed target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000055 * \param[in] target The target to search.
56 *
57 * Note that this is the capacity (aka max number of messages).
58 * It is not the number of unused message slots.
59 * \return The number of log strings in the osmo_strrb-backed target.
60 */
61size_t log_target_rb_avail_size(struct log_target const *target)
62{
63 struct osmo_strrb *rb = target->tgt_rb.rb;
64 return rb->size - 1;
65}
66
Neels Hofmeyr87e45502017-06-20 00:17:59 +020067/*! Return the nth log entry in a target.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000068 * \param[in] target The target to search.
69 * \param[in] logindex The index of the log entry/error message.
70 *
71 * \return A pointer to the nth message, or NULL if logindex is invalid.
72 */
73const char *log_target_rb_get(struct log_target const *target, size_t logindex)
74{
75 return osmo_strrb_get_nth(target->tgt_rb.rb, logindex);
76}
77
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078/*! Create a new logging target for ringbuffer-backed logging.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000079 * \param[in] size The capacity (number of messages) of the logging target.
80 * \returns A log target in case of success, NULL in case of error.
81 */
82struct log_target *log_target_create_rb(size_t size)
83{
84 struct log_target *target;
85 struct osmo_strrb *rb;
86
87 target = log_target_create();
88 if (!target)
89 return NULL;
90
91 rb = osmo_strrb_create(target, size + 1);
92 if (!rb) {
93 log_target_destroy(target);
94 return NULL;
95 }
96
97 target->tgt_rb.rb = rb;
98 target->type = LOG_TGT_TYPE_STRRB;
99 target->output = _rb_output;
100
101 return target;
102}
103
104/* @} */