Skip to content

Commit

Permalink
Added error check to __RELATIVE_PATH__ macro and resolved Clang warni…
Browse files Browse the repository at this point in the history
…ng, ensured robust path handling. Da path be clear now, boss.
  • Loading branch information
Galfurian committed Nov 20, 2024
1 parent 29da42e commit 75be783
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions libc/inc/syslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#pragma once

#include "string.h"

// Log levels for setting the severity of log messages.
#define LOG_EMERG 0 ///< Emergency: system unusable.
#define LOG_ALERT 1 ///< Alert: immediate action required.
Expand Down Expand Up @@ -75,7 +77,8 @@ int __syslog(const char *file, const char *fun, int line, short log_level, const
/// MENTOS_ROOT = "/path/to/mentos" and
/// __FILE__ = "/path/to/mentos/src/kernel/main.c", the result will be
/// "src/kernel/main.c".
#define __RELATIVE_PATH__ (__FILE__ + sizeof(MENTOS_ROOT))
#define __RELATIVE_PATH__ \
(strncmp(__FILE__, MENTOS_ROOT, sizeof(MENTOS_ROOT) - 1) == 0 ? (&__FILE__[sizeof(MENTOS_ROOT)]) : __FILE__)

// Wrapper macro to simplify usage
#define syslog(log_level, format, ...) __syslog(__RELATIVE_PATH__, __func__, __LINE__, log_level, format, ##__VA_ARGS__)
#define syslog(...) __syslog(__RELATIVE_PATH__, __func__, __LINE__, __VA_ARGS__)
19 changes: 18 additions & 1 deletion mentos/inc/io/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "sys/kernel_levels.h"
#include "string.h"

#ifndef __DEBUG_LEVEL__
/// Defines the debug level, by default we set it to notice.
Expand Down Expand Up @@ -54,7 +55,23 @@ void dbg_puts(const char *s);
/// @param ... the list of arguments.
void dbg_printf(const char *file, const char *fun, int line, char *header, short log_level, const char *format, ...);

#define __RELATIVE_PATH__ (__FILE__ + sizeof(MENTOS_ROOT))
/// @brief Extracts the relative path of the current file from the project root.
///
/// This macro calculates the relative path of the file (`__FILE__`) by skipping
/// the prefix defined by `MENTOS_ROOT`. It is used to simplify file path
/// logging by removing the absolute path up to the project root.
///
/// @note Ensure that `MENTOS_ROOT` is correctly defined as the root path of the
/// project. If `__FILE__` does not start with `MENTOS_ROOT`, the behavior is
/// undefined.
///
/// @example
/// If
/// MENTOS_ROOT = "/path/to/mentos" and
/// __FILE__ = "/path/to/mentos/src/kernel/main.c", the result will be
/// "src/kernel/main.c".
#define __RELATIVE_PATH__ \
(strncmp(__FILE__, MENTOS_ROOT, sizeof(MENTOS_ROOT) - 1) == 0 ? (&__FILE__[sizeof(MENTOS_ROOT)]) : __FILE__)

/// General logging macro that logs a message at the specified log level.
/// Only logs messages if the specified log level is less than or equal to __DEBUG_LEVEL__.
Expand Down

0 comments on commit 75be783

Please sign in to comment.