Mstdlib-1.24.0
m_io_trace.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_IO_TRACE_H__
25#define __M_IO_TRACE_H__
26
27#include <mstdlib/base/m_defs.h>
28#include <mstdlib/base/m_types.h>
29#include <mstdlib/io/m_io.h>
30#include <mstdlib/io/m_event.h>
31
32__BEGIN_DECLS
33
34/*! \addtogroup m_io_trace Addon for event and I/O tracing
35 * \ingroup m_eventio_base_addon
36 *
37 * Event and I/O tracing addon
38 *
39 * Allows data to be traced as it flows through the trace layer. For example communication
40 * over serial with an external device could have a trace layer that logs read and
41 * write commands.
42 *
43 * This can be very useful when combined with the M_log logging module.
44 *
45 * @{
46 */
47
48/*! Trace even type. */
53};
55
56/*! Definition for a function callback that is called every time a traceable event is triggered by
57 * the event subsystem.
58 *
59 * \param[in] cb_arg User-specified callback argument registered when the trace was added to the
60 * event handle.
61 * \param[in] type The type of type that has been triggered, see M_io_trace_type_t.
62 * \param[in] event_type The type of event that has been triggered, see M_event_type_t.
63 * \param[in] data Data that is passing though this trace layer.
64 * \param[in] data_len Length of data.
65 */
66typedef void (*M_io_trace_cb_t)(void *cb_arg, M_io_trace_type_t type, M_event_type_t event_type, const unsigned char *data, size_t data_len);
67
68/*! Definition for a function that duplicates a callback argument.
69 *
70 * An io that has an accept event such as net can have connection specific arguments. The trace
71 * layer will be duplicated from the server to the *new* io client connection. This allows
72 * the cb_arg to be duplicated as well.
73 *
74 * \param[in] cb_arg Callback argument of the type provided to M_io_add_trace.
75 */
76typedef void *(*M_io_trace_cb_dup_t)(void *cb_arg);
77
78/*! Definition for a function that destroys a user provided callback data associated with the trace.
79 *
80 * \param[in] cb_arg Callback argument of the type provided to M_io_add_trace.
81 */
82typedef void (*M_io_trace_cb_free_t)(void *cb_arg);
83
84
85/*! Add a trace layer
86 *
87 * \param[in] io io object.
88 * \param[out] layer_id Layer id this is added at.
89 * \param[in] callback Function called when the trace is triggered.
90 * \param[in] cb_arg Argument passed to callback.
91 * \param[in] cb_dup Function to duplicate cb_arg if needed. Optional and can be NULL if not used.
92 * \param[in] cb_free Function to destroy cb_arg when the io object id destroyed.
93 * Optional and can be NULL if not used.
94 *
95 * \return Result.
96 */
97M_API M_io_error_t M_io_add_trace(M_io_t *io, size_t *layer_id, M_io_trace_cb_t callback, void *cb_arg, M_io_trace_cb_dup_t cb_dup, M_io_trace_cb_free_t cb_free);
98
99
100/*! Get the callback arg for a trace layer.
101 *
102 * \param[in] io io object.
103 * \param[in] layer_id Layer id.
104 *
105 * \return Argument on success or NULL on error. NULL is success if a cb_arg was not set.
106 */
107M_API void *M_io_trace_get_callback_arg(M_io_t *io, size_t layer_id);
108
109
110/*! Set the callback arg for the trace layer.
111 *
112 * \param[in] io io object.
113 * \param[in] layer_id Layer id.
114 * \param[in] cb_arg Argument passed to callback.
115 *
116 * \return M_TRUE on success, otherwise M_FALSE on error.
117 */
118M_API M_bool M_io_trace_set_callback_arg(M_io_t *io, size_t layer_id, void *cb_arg);
119
120/*! @} */
121
122__END_DECLS
123
124#endif
125
enum M_event_type M_event_type_t
Definition: m_event.h:189
void * M_io_trace_get_callback_arg(M_io_t *io, size_t layer_id)
M_io_error_t M_io_add_trace(M_io_t *io, size_t *layer_id, M_io_trace_cb_t callback, void *cb_arg, M_io_trace_cb_dup_t cb_dup, M_io_trace_cb_free_t cb_free)
void *(* M_io_trace_cb_dup_t)(void *cb_arg)
Definition: m_io_trace.h:76
void(* M_io_trace_cb_free_t)(void *cb_arg)
Definition: m_io_trace.h:82
M_io_trace_type
Definition: m_io_trace.h:49
enum M_io_trace_type M_io_trace_type_t
Definition: m_io_trace.h:54
M_bool M_io_trace_set_callback_arg(M_io_t *io, size_t layer_id, void *cb_arg)
void(* M_io_trace_cb_t)(void *cb_arg, M_io_trace_type_t type, M_event_type_t event_type, const unsigned char *data, size_t data_len)
Definition: m_io_trace.h:66
@ M_IO_TRACE_TYPE_READ
Definition: m_io_trace.h:50
@ M_IO_TRACE_TYPE_WRITE
Definition: m_io_trace.h:51
@ M_IO_TRACE_TYPE_EVENT
Definition: m_io_trace.h:52
enum M_io_error M_io_error_t
Definition: m_io.h:93
struct M_io M_io_t
Definition: m_io.h:59