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