MRTKLIB 0.7.14
Modernized GNSS Positioning Library
Loading...
Searching...
No Matches
mrtklib.h
Go to the documentation of this file.
1/*------------------------------------------------------------------------------
2 * mrtklib.h : MRTKLIB public API for context management and logging
3 *
4 * Copyright (C) 2026 H.SHIONO (MRTKLIB Project)
5 * Copyright (C) 2023-2025 Cabinet Office, Japan
6 * Copyright (C) 2024-2025 Lighthouse Technology & Consulting Co. Ltd.
7 * Copyright (C) 2023-2025 Japan Aerospace Exploration Agency
8 * Copyright (C) 2023-2025 TOSHIBA ELECTRONIC TECHNOLOGIES CORPORATION
9 * Copyright (C) 2015- Mitsubishi Electric Corp.
10 * Copyright (C) 2014 Geospatial Information Authority of Japan
11 * Copyright (C) 2014 T.SUZUKI
12 * Copyright (C) 2007-2023 T.TAKASU
13 *
14 * SPDX-License-Identifier: BSD-2-Clause
15 *----------------------------------------------------------------------------*/
16/**
17 * @file mrtklib.h
18 * @brief MRTKLIB Public API — Context Management and Logging
19 *
20 * This header defines the public API for MRTKLIB, a modernized and refactored
21 * version of RTKLIB targeting JAXA QZSS/MADOCA processing. The library is
22 * designed to be:
23 * - Thread-safe through context-based state management
24 * - Re-entrant, allowing multiple simultaneous processing instances
25 * - Library-first with no global state
26 *
27 * @note All public functions require a valid mrtk_context_t pointer unless
28 * otherwise specified.
29 */
30#ifndef MRTKLIB_H
31#define MRTKLIB_H
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#include <stdarg.h>
38#include <stdbool.h>
39#include <stddef.h>
40#include <stdint.h>
41#include <time.h>
42
43/*============================================================================
44 * Version Information
45 *===========================================================================*/
46
47#include "mrtklib/mrtk_version.h"
48
49/*============================================================================
50 * Opaque Context
51 *===========================================================================*/
52
53/**
54 * @brief Opaque library context structure.
55 *
56 * Holds all library state including logging configuration, error state,
57 * and user-defined data. This structure is intentionally opaque to allow
58 * internal changes without breaking API compatibility.
59 *
60 * @note Users must create contexts via mrtk_context_new() and free them
61 * via mrtk_context_free(). Never allocate this structure directly.
62 */
64
65/*============================================================================
66 * Logging
67 *===========================================================================*/
68
69/**
70 * @brief Log severity levels.
71 *
72 * Levels are ordered by severity (DEBUG < INFO < WARN < ERROR < NONE).
73 * Setting a minimum log level filters out all messages below that severity.
74 * MRTK_LOG_NONE disables all output.
75 */
76typedef enum {
77 MRTK_LOG_DEBUG = 0, /**< Detailed debug information for development */
78 MRTK_LOG_INFO, /**< General informational messages */
79 MRTK_LOG_WARN, /**< Warning conditions that may require attention */
80 MRTK_LOG_ERROR, /**< Error conditions that affect processing */
81 MRTK_LOG_NONE /**< Sentinel — disables all logging output */
83
84/**
85 * @brief Log callback function signature.
86 *
87 * Custom log handlers must conform to this signature. The callback is invoked
88 * for each log message that passes the minimum log level filter.
89 *
90 * @param ctx Context instance that generated the log message
91 * @param level Severity level of the message
92 * @param msg Null-terminated formatted message string
93 *
94 * @note The msg pointer is only valid during the callback invocation.
95 * Copy the string if you need to retain it.
96 * @note The callback is invoked synchronously from the calling thread.
97 */
98typedef void (*mrtk_log_cb_t)(mrtk_context_t* ctx, mrtk_log_level_t level, const char* msg);
99
100/*============================================================================
101 * Lifecycle Management
102 *===========================================================================*/
103
104/**
105 * @brief Create a new library context.
106 *
107 * Allocates and initializes a new context with default settings:
108 * - Log level: MRTK_LOG_INFO
109 * - Log callback: Default stdout/stderr logger
110 * - User data: NULL
111 *
112 * @return Pointer to newly created context, or NULL if allocation fails.
113 *
114 * @note The caller is responsible for freeing the context via mrtk_context_free().
115 *
116 * @code{.c}
117 * mrtk_context_t* ctx = mrtk_context_new();
118 * if (ctx == NULL) { ... }
119 * // ... use context ...
120 * mrtk_context_free(ctx);
121 * @endcode
122 */
124
125/**
126 * @brief Free the library context and release all associated resources.
127 *
128 * Safely frees a context created by mrtk_context_new(). After this call,
129 * the context pointer is invalid and must not be used.
130 *
131 * @param ctx Context to free. If NULL, this function does nothing.
132 */
134
135/*============================================================================
136 * Logging Configuration
137 *===========================================================================*/
138
139/**
140 * @brief Set a custom log callback handler.
141 *
142 * Replaces the default logging behavior with a custom callback. This allows
143 * integration with external logging systems, GUI log windows, or file-based
144 * logging.
145 *
146 * @param ctx Context instance
147 * @param cb Callback function to handle log messages. Pass NULL to restore
148 * the default stdout/stderr logger.
149 */
151
152/**
153 * @brief Set the minimum log level threshold.
154 *
155 * Messages below this severity level will be silently discarded.
156 * Setting MRTK_LOG_NONE disables all logging output.
157 *
158 * @param ctx Context instance
159 * @param level Minimum level to log (inclusive)
160 */
162
163/**
164 * @brief Get the current log level threshold.
165 *
166 * @param ctx Context instance
167 * @return Current minimum log level, or MRTK_LOG_NONE if ctx is NULL.
168 */
170
171/**
172 * @brief Log a formatted message.
173 *
174 * Formats and dispatches a log message through the registered callback
175 * if the message level meets or exceeds the minimum threshold.
176 *
177 * @param ctx Context instance
178 * @param level Severity level of this message
179 * @param fmt printf-style format string
180 * @param ... Format arguments
181 */
182void mrtk_log(mrtk_context_t* ctx, mrtk_log_level_t level, const char* fmt, ...);
183
184/**
185 * @brief Log a formatted message using a pre-initialized va_list.
186 *
187 * This is the va_list variant of mrtk_log(), intended for use by wrapper
188 * functions (such as the legacy RTKLIB trace() bridge) that receive variadic
189 * arguments from their own callers.
190 *
191 * @param ctx Context instance
192 * @param level Severity level of this message
193 * @param fmt printf-style format string
194 * @param args Pre-initialized va_list
195 */
196void mrtk_log_v(mrtk_context_t* ctx, mrtk_log_level_t level, const char* fmt, va_list args);
197
198/*============================================================================
199 * Legacy Migration Support
200 *===========================================================================*/
201
202/**
203 * @brief Global context for legacy RTKLIB trace() bridge.
204 *
205 * During the migration period, this global pointer allows the existing
206 * trace() / tracet() functions in rtkcmn.c to route their output through
207 * the new context-based logging system without modifying thousands of
208 * call sites.
209 *
210 * @note Applications must set this to mrtk_context_new() at startup and
211 * free it with mrtk_context_free() at shutdown.
212 * @note This pointer is intentionally global and will be removed once
213 * all modules have been migrated to context-based APIs.
214 */
216
217/*============================================================================
218 * Error Handling
219 *===========================================================================*/
220
221/**
222 * @brief Get the last error message.
223 *
224 * Retrieves the most recent error message stored in the context by an
225 * internal library function. Returns an empty string if no error has occurred.
226 *
227 * @param ctx Context instance
228 * @return Pointer to the error message string, or "Invalid context" if ctx is NULL.
229 *
230 * @note The returned pointer remains valid until the next error occurs or
231 * the context is freed.
232 */
233const char* mrtk_get_last_error(const mrtk_context_t* ctx);
234
235/**
236 * @brief Clear the last error message.
237 *
238 * Resets the error state, clearing any previously stored error message.
239 *
240 * @param ctx Context instance
241 */
243
244/*============================================================================
245 * User Data Management
246 *===========================================================================*/
247
248/**
249 * @brief Associate user-defined data with the context.
250 *
251 * Stores an arbitrary pointer that can be retrieved later. Useful for
252 * passing application-specific state through callbacks.
253 *
254 * @param ctx Context instance
255 * @param user_data Pointer to user data (may be NULL)
256 *
257 * @note The library does not manage the lifetime of user_data.
258 */
260
261/**
262 * @brief Retrieve user-defined data from the context.
263 *
264 * @param ctx Context instance
265 * @return Previously stored user data pointer, or NULL if none was set
266 * or if ctx is NULL.
267 */
269
270/*============================================================================
271 * Version Information
272 *===========================================================================*/
273
274/**
275 * @brief Get the library version string.
276 *
277 * @return Static string containing the version (e.g., "1.2.0")
278 */
279const char* mrtk_version_string(void);
280
281#ifdef __cplusplus
282}
283#endif
284
285#endif /* MRTKLIB_H */
mrtk_context_t * g_mrtk_legacy_ctx
Global context for legacy RTKLIB trace() bridge.
Definition mrtk_core.c:40
mrtk_log_level_t
Log severity levels.
Definition mrtklib.h:76
@ MRTK_LOG_WARN
Definition mrtklib.h:79
@ MRTK_LOG_INFO
Definition mrtklib.h:78
@ MRTK_LOG_DEBUG
Definition mrtklib.h:77
@ MRTK_LOG_ERROR
Definition mrtklib.h:80
@ MRTK_LOG_NONE
Definition mrtklib.h:81
void * mrtk_context_get_user_data(const mrtk_context_t *ctx)
Retrieve user-defined data from the context.
Definition mrtk_core.c:347
void(* mrtk_log_cb_t)(mrtk_context_t *ctx, mrtk_log_level_t level, const char *msg)
Log callback function signature.
Definition mrtklib.h:98
void mrtk_log(mrtk_context_t *ctx, mrtk_log_level_t level, const char *fmt,...)
Log a formatted message.
Definition mrtk_core.c:289
void mrtk_context_set_log_callback(mrtk_context_t *ctx, mrtk_log_cb_t cb)
Set a custom log callback handler.
Definition mrtk_core.c:202
mrtk_context_t * mrtk_context_new(void)
Create a new library context.
Definition mrtk_core.c:162
mrtk_log_level_t mrtk_context_get_log_level(const mrtk_context_t *ctx)
Get the current log level threshold.
Definition mrtk_core.c:233
const char * mrtk_version_string(void)
Get the library version string.
Definition mrtk_core.c:363
void mrtk_clear_error(mrtk_context_t *ctx)
Clear the last error message.
Definition mrtk_core.c:319
void mrtk_log_v(mrtk_context_t *ctx, mrtk_log_level_t level, const char *fmt, va_list args)
Log a formatted message using a pre-initialized va_list.
Definition mrtk_core.c:251
void mrtk_context_free(mrtk_context_t *ctx)
Free the library context and release all associated resources.
Definition mrtk_core.c:184
void mrtk_context_set_log_level(mrtk_context_t *ctx, mrtk_log_level_t level)
Set the minimum log level threshold.
Definition mrtk_core.c:221
void mrtk_context_set_user_data(mrtk_context_t *ctx, void *user_data)
Associate user-defined data with the context.
Definition mrtk_core.c:335
struct mrtk_context_s mrtk_context_t
Opaque library context structure.
Definition mrtklib.h:63
const char * mrtk_get_last_error(const mrtk_context_t *ctx)
Get the last error message.
Definition mrtk_core.c:307
Internal context structure.
Definition mrtk_core.c:62
void * user_data
Definition mrtk_core.c:66