Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file loggingrb.c |
| 2 | * Ringbuffer-backed logging support code. */ |
| 3 | /* |
| 4 | * (C) 2012-2013 by Katerina Barone-Adesi |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 9 | * 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 Welte | 33e940b | 2014-10-26 20:52:25 +0100 | [diff] [blame] | 11 | * the Free Software Foundation; either version 2 of the License, or |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 12 | * (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 Welte | efee361 | 2017-10-16 14:37:05 +0200 | [diff] [blame] | 25 | /*! \addtogroup loggingrb |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 26 | * @{ |
Harald Welte | df8c57f | 2017-10-16 14:37:31 +0200 | [diff] [blame] | 27 | * 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 34 | * \file loggingrb.c */ |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include <osmocom/core/strrb.h> |
| 37 | #include <osmocom/core/logging.h> |
| 38 | #include <osmocom/core/loggingrb.h> |
| 39 | |
| 40 | static 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! Return the number of log strings in the osmo_strrb-backed target. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 47 | * \param[in] target The target to search. |
| 48 | * |
| 49 | * \return The number of log strings in the osmo_strrb-backed target. |
| 50 | */ |
| 51 | size_t log_target_rb_used_size(struct log_target const *target) |
| 52 | { |
| 53 | return osmo_strrb_elements(target->tgt_rb.rb); |
| 54 | } |
| 55 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 56 | /*! Return the capacity of the osmo_strrb-backed target. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 57 | * \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 | */ |
| 63 | size_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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 69 | /*! Return the nth log entry in a target. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 70 | * \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 | */ |
| 75 | const 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 80 | /*! Create a new logging target for ringbuffer-backed logging. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 81 | * \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 | */ |
| 84 | struct 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 | /* @} */ |