MRTKLIB 0.7.14
Modernized GNSS Positioning Library
Loading...
Searching...
No Matches
mrtk_core.c File Reference

Core implementation of MRTKLIB context management and logging. More...

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mrtklib/mrtklib.h"
Include dependency graph for mrtk_core.c:

Go to the source code of this file.

Classes

struct  mrtk_context_s
 Internal context structure. More...

Macros

#define MAX_ERROR_LEN   256
 Maximum length of error messages stored in context.
#define MAX_LOG_LEN   1024
 Maximum length of formatted log messages.

Functions

static void default_log_callback (mrtk_context_t *ctx, mrtk_log_level_t level, const char *msg)
 Default log callback implementation.
static void mrtk_set_error (mrtk_context_t *ctx, const char *fmt,...)
 Set the internal error message.
mrtk_context_t * mrtk_context_new (void)
 Create a new library context.
void mrtk_context_free (mrtk_context_t *ctx)
 Free the library context.
void mrtk_context_set_log_callback (mrtk_context_t *ctx, mrtk_log_cb_t cb)
 Set a custom log callback.
void mrtk_context_set_log_level (mrtk_context_t *ctx, mrtk_log_level_t level)
 Set the minimum log level.
mrtk_log_level_t mrtk_context_get_log_level (const mrtk_context_t *ctx)
 Get the current log level.
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.
void mrtk_log (mrtk_context_t *ctx, mrtk_log_level_t level, const char *fmt,...)
 Log a formatted message.
const char * mrtk_get_last_error (const mrtk_context_t *ctx)
 Get the last error message.
void mrtk_clear_error (mrtk_context_t *ctx)
 Clear the last error message.
void mrtk_context_set_user_data (mrtk_context_t *ctx, void *user_data)
 Set user-defined data.
void * mrtk_context_get_user_data (const mrtk_context_t *ctx)
 Get user-defined data.
const char * mrtk_version_string (void)
 Get the library version string.

Variables

mrtk_context_t * g_mrtk_legacy_ctx = NULL
 Global context for legacy RTKLIB trace() bridge.

Detailed Description

Core implementation of MRTKLIB context management and logging.

This file contains the implementation of the opaque context structure and all associated lifecycle, logging, and error handling functions.

Definition in file mrtk_core.c.

Macro Definition Documentation

◆ MAX_ERROR_LEN

#define MAX_ERROR_LEN   256

Maximum length of error messages stored in context.

Definition at line 47 of file mrtk_core.c.

Referenced by mrtk_set_error().

◆ MAX_LOG_LEN

#define MAX_LOG_LEN   1024

Maximum length of formatted log messages.

Definition at line 50 of file mrtk_core.c.

Referenced by mrtk_log_v().

Function Documentation

◆ default_log_callback()

void default_log_callback ( mrtk_context_t * ctx,
mrtk_log_level_t level,
const char * msg )
static

Default log callback implementation.

Outputs log messages to stdout (DEBUG, INFO, WARN) or stderr (ERROR). This is the default handler used when no custom callback is registered.

Parameters
ctxContext instance (unused in default implementation)
levelSeverity level of the message
msgFormatted message string

Definition at line 83 of file mrtk_core.c.

References MRTK_LOG_DEBUG, MRTK_LOG_ERROR, MRTK_LOG_INFO, MRTK_LOG_NONE, and MRTK_LOG_WARN.

Referenced by mrtk_context_new(), and mrtk_context_set_log_callback().

Here is the caller graph for this function:

◆ mrtk_clear_error()

void mrtk_clear_error ( mrtk_context_t * ctx)

Clear the last error message.

Parameters
ctxContext instance

Definition at line 319 of file mrtk_core.c.

References mrtk_context_s::last_error.

◆ mrtk_context_free()

void mrtk_context_free ( mrtk_context_t * ctx)

Free the library context.

Free the library context and release all associated resources.

Releases all memory associated with the context. Safe to call with NULL.

Parameters
ctxContext to free, or NULL (no-op)

Definition at line 184 of file mrtk_core.c.

References free.

◆ mrtk_context_get_log_level()

mrtk_log_level_t mrtk_context_get_log_level ( const mrtk_context_t * ctx)

Get the current log level.

Get the current log level threshold.

Parameters
ctxContext instance
Returns
Current log level, or MRTK_LOG_NONE if ctx is NULL

Definition at line 233 of file mrtk_core.c.

References mrtk_context_s::log_level, and MRTK_LOG_NONE.

◆ mrtk_context_get_user_data()

void * mrtk_context_get_user_data ( const mrtk_context_t * ctx)

Get user-defined data.

Retrieve user-defined data from the context.

Parameters
ctxContext instance
Returns
Associated user data, or NULL if ctx is NULL or no data was set

Definition at line 347 of file mrtk_core.c.

References mrtk_context_s::user_data.

◆ mrtk_context_new()

mrtk_context_t * mrtk_context_new ( void )

Create a new library context.

Allocates memory for a new context and initializes it with default values. The default configuration is:

  • log_level: MRTK_LOG_INFO
  • log_callback: default_log_callback (stdout/stderr output)
  • last_error: empty string
  • user_data: NULL
Returns
Newly allocated context, or NULL if memory allocation fails.

Definition at line 162 of file mrtk_core.c.

References calloc, default_log_callback(), mrtk_context_s::last_error, mrtk_context_s::log_callback, mrtk_context_s::log_level, MRTK_LOG_INFO, and mrtk_context_s::user_data.

Here is the call graph for this function:

◆ mrtk_context_set_log_callback()

void mrtk_context_set_log_callback ( mrtk_context_t * ctx,
mrtk_log_cb_t cb )

Set a custom log callback.

Set a custom log callback handler.

Parameters
ctxContext instance
cbCallback function, or NULL to restore default logger

Definition at line 202 of file mrtk_core.c.

References default_log_callback(), and mrtk_context_s::log_callback.

Here is the call graph for this function:

◆ mrtk_context_set_log_level()

void mrtk_context_set_log_level ( mrtk_context_t * ctx,
mrtk_log_level_t level )

Set the minimum log level.

Set the minimum log level threshold.

Parameters
ctxContext instance
levelNew minimum log level

Definition at line 221 of file mrtk_core.c.

References mrtk_context_s::log_level.

◆ mrtk_context_set_user_data()

void mrtk_context_set_user_data ( mrtk_context_t * ctx,
void * user_data )

Set user-defined data.

Associate user-defined data with the context.

Parameters
ctxContext instance
user_dataPointer to associate with context

Definition at line 335 of file mrtk_core.c.

References mrtk_context_s::user_data.

◆ mrtk_get_last_error()

const char * mrtk_get_last_error ( const mrtk_context_t * ctx)

Get the last error message.

Parameters
ctxContext instance
Returns
Error message string, or "Invalid context" if ctx is NULL

Definition at line 307 of file mrtk_core.c.

References mrtk_context_s::last_error.

◆ mrtk_log()

void mrtk_log ( mrtk_context_t * ctx,
mrtk_log_level_t level,
const char * fmt,
... )

Log a formatted message.

Convenience wrapper around mrtk_log_v() that accepts variadic arguments.

Parameters
ctxContext instance
levelMessage severity level
fmtprintf-style format string
...Format arguments

Definition at line 289 of file mrtk_core.c.

References mrtk_log_v().

Referenced by mrtk_set_error().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrtk_log_v()

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.

This is the core formatting and dispatch function. Both mrtk_log() and the legacy trace() bridge delegate to this function.

Parameters
ctxContext instance
levelMessage severity level
fmtprintf-style format string
argsPre-initialized va_list

Definition at line 251 of file mrtk_core.c.

References mrtk_context_s::log_callback, MAX_LOG_LEN, and MRTK_LOG_NONE.

Referenced by mrtk_log().

Here is the caller graph for this function:

◆ mrtk_set_error()

void mrtk_set_error ( mrtk_context_t * ctx,
const char * fmt,
... )
static

Set the internal error message.

Stores a formatted error message in the context for later retrieval via mrtk_get_last_error(). Also logs the error message at ERROR level.

Parameters
ctxContext instance
fmtprintf-style format string
...Format arguments
Note
This is an internal function not exposed in the public API. Library modules use this to report errors.

Definition at line 131 of file mrtk_core.c.

References mrtk_context_s::last_error, MAX_ERROR_LEN, mrtk_log(), and MRTK_LOG_ERROR.

Here is the call graph for this function:

◆ mrtk_version_string()

const char * mrtk_version_string ( void )

Get the library version string.

Returns
Static version string (e.g., "0.1.0")

Definition at line 363 of file mrtk_core.c.

Variable Documentation

◆ g_mrtk_legacy_ctx

mrtk_context_t* g_mrtk_legacy_ctx = NULL

Global context for legacy RTKLIB trace() bridge.

See also
g_mrtk_legacy_ctx declaration in mrtklib.h

Definition at line 40 of file mrtk_core.c.