Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | /*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_LOGGING_LOG_OUTPUT_H_
#define ZEPHYR_INCLUDE_LOGGING_LOG_OUTPUT_H_
#include <logging/log_msg.h>
#include <sys/util.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Log output API
* @defgroup log_output Log output API
* @ingroup logger
* @{
*/
/** @brief Flag forcing ANSI escape code colors, red (errors), yellow
* (warnings).
*/
#define LOG_OUTPUT_FLAG_COLORS BIT(0)
/** @brief Flag forcing timestamp */
#define LOG_OUTPUT_FLAG_TIMESTAMP BIT(1)
/** @brief Flag forcing timestamp formatting. */
#define LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP BIT(2)
/** @brief Flag forcing severity level prefix. */
#define LOG_OUTPUT_FLAG_LEVEL BIT(3)
/** @brief Flag preventing the logger from adding CR and LF characters. */
#define LOG_OUTPUT_FLAG_CRLF_NONE BIT(4)
/** @brief Flag forcing a single LF character for line breaks. */
#define LOG_OUTPUT_FLAG_CRLF_LFONLY BIT(5)
/** @brief Flag forcing syslog format specified in RFC 5424
*/
#define LOG_OUTPUT_FLAG_FORMAT_SYSLOG BIT(6)
/**
* @brief Prototype of the function processing output data.
*
* @param data Data.
* @param length Data length.
* @param ctx User context.
*
* @return Number of bytes processed, dropped or discarded.
*
* @note If the log output function cannot process all of the data, it is
* its responsibility to mark them as dropped or discarded by returning
* the corresponding number of bytes dropped or discarded to the caller.
*/
typedef int (*log_output_func_t)(u8_t *buf, size_t size, void *ctx);
/* @brief Control block structure for log_output instance. */
struct log_output_control_block {
size_t offset;
void *ctx;
const char *hostname;
};
/** @brief Log_output instance structure. */
struct log_output {
log_output_func_t func;
struct log_output_control_block *control_block;
u8_t *buf;
size_t size;
};
/** @brief Create log_output instance.
*
* @param _name Instance name.
* @param _func Function for processing output data.
* @param _buf Pointer to the output buffer.
* @param _size Size of the output buffer.
*/
#define LOG_OUTPUT_DEFINE(_name, _func, _buf, _size) \
static struct log_output_control_block _name##_control_block; \
static const struct log_output _name = { \
.func = _func, \
.control_block = &_name##_control_block, \
.buf = _buf, \
.size = _size, \
}
/** @brief Process log messages to readable strings.
*
* Function is using provided context with the buffer and output function to
* process formatted string and output the data.
*
* @param log_output Pointer to the log output instance.
* @param msg Log message.
* @param flags Optional flags.
*/
void log_output_msg_process(const struct log_output *log_output,
struct log_msg *msg,
u32_t flags);
/** @brief Process log string
*
* Function is formatting provided string adding optional prefixes and
* postfixes.
*
* @param log_output Pointer to log_output instance.
* @param src_level Log source and level structure.
* @param timestamp Timestamp.
* @param fmt String.
* @param ap String arguments.
* @param flags Optional flags.
*
*/
void log_output_string(const struct log_output *log_output,
struct log_msg_ids src_level, u32_t timestamp,
const char *fmt, va_list ap, u32_t flags);
/** @brief Process log hexdump
*
* Function is formatting provided hexdump adding optional prefixes and
* postfixes.
*
* @param log_output Pointer to log_output instance.
* @param src_level Log source and level structure.
* @param timestamp Timestamp.
* @param metadata String.
* @param data Data.
* @param length Data length.
* @param flags Optional flags.
*
*/
void log_output_hexdump(const struct log_output *log_output,
struct log_msg_ids src_level, u32_t timestamp,
const char *metadata, const u8_t *data,
u32_t length, u32_t flags);
/** @brief Process dropped messages indication.
*
* Function prints error message indicating lost log messages.
*
* @param log_output Pointer to the log output instance.
* @param cnt Number of dropped messages.
*/
void log_output_dropped_process(const struct log_output *log_output, u32_t cnt);
/** @brief Flush output buffer.
*
* @param log_output Pointer to the log output instance.
*/
void log_output_flush(const struct log_output *log_output);
/** @brief Function for setting user context passed to the output function.
*
* @param log_output Pointer to the log output instance.
* @param ctx User context.
*/
static inline void log_output_ctx_set(const struct log_output *log_output,
void *ctx)
{
log_output->control_block->ctx = ctx;
}
/** @brief Function for setting hostname of this device
*
* @param log_output Pointer to the log output instance.
* @param hostname Hostname of this device
*/
static inline void log_output_hostname_set(const struct log_output *log_output,
const char *hostname)
{
log_output->control_block->hostname = hostname;
}
/** @brief Set timestamp frequency.
*
* @param freq Frequency in Hz.
*/
void log_output_timestamp_freq_set(u32_t freq);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_LOGGING_LOG_OUTPUT_H_ */
|