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 | * |
| 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 Welte | 33e940b | 2014-10-26 20:52:25 +0100 | [diff] [blame] | 9 | * the Free Software Foundation; either version 2 of the License, or |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 10 | * (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 Welte | efee361 | 2017-10-16 14:37:05 +0200 | [diff] [blame] | 23 | /*! \addtogroup loggingrb |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 24 | * @{ |
Harald Welte | df8c57f | 2017-10-16 14:37:31 +0200 | [diff] [blame] | 25 | * 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 32 | * \file loggingrb.c */ |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 33 | |
| 34 | #include <osmocom/core/strrb.h> |
| 35 | #include <osmocom/core/logging.h> |
| 36 | #include <osmocom/core/loggingrb.h> |
| 37 | |
| 38 | static 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 44 | /*! 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] | 45 | * \param[in] target The target to search. |
| 46 | * |
| 47 | * \return The number of log strings in the osmo_strrb-backed target. |
| 48 | */ |
| 49 | size_t log_target_rb_used_size(struct log_target const *target) |
| 50 | { |
| 51 | return osmo_strrb_elements(target->tgt_rb.rb); |
| 52 | } |
| 53 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! Return the capacity of the osmo_strrb-backed target. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 55 | * \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 | */ |
| 61 | size_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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 67 | /*! Return the nth log entry in a target. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 68 | * \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 | */ |
| 73 | const 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 78 | /*! Create a new logging target for ringbuffer-backed logging. |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 79 | * \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 | */ |
| 82 | struct 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 | /* @} */ |