2025-12-28 03:14:31 +01:00
|
|
|
/*
|
|
|
|
|
* DWN - Desktop Window Manager
|
2025-12-28 04:30:10 +01:00
|
|
|
* retoor <retoor@molodetz.nl>
|
2025-12-28 03:14:31 +01:00
|
|
|
* Utility functions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef DWN_UTIL_H
|
|
|
|
|
#define DWN_UTIL_H
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdarg.h>
|
2025-12-28 04:30:10 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
/* Contract assertion macro - use for programmer errors */
|
|
|
|
|
#define DWN_ASSERT(cond) assert(cond)
|
|
|
|
|
#define DWN_ASSERT_MSG(cond, msg) assert((cond) && (msg))
|
2025-12-28 03:14:31 +01:00
|
|
|
|
|
|
|
|
/* Logging levels */
|
|
|
|
|
typedef enum {
|
|
|
|
|
LOG_DEBUG,
|
|
|
|
|
LOG_INFO,
|
|
|
|
|
LOG_WARN,
|
|
|
|
|
LOG_ERROR
|
|
|
|
|
} LogLevel;
|
|
|
|
|
|
|
|
|
|
/* Async Logging - non-blocking with max file size (5MB) and rotation */
|
|
|
|
|
void log_init(const char *log_file);
|
|
|
|
|
void log_close(void);
|
|
|
|
|
void log_set_level(LogLevel level);
|
|
|
|
|
void log_flush(void); /* Force flush pending logs (call before exit/crash) */
|
|
|
|
|
void log_msg(LogLevel level, const char *fmt, ...);
|
|
|
|
|
|
|
|
|
|
/* Convenience macros */
|
|
|
|
|
#define LOG_DEBUG(...) log_msg(LOG_DEBUG, __VA_ARGS__)
|
|
|
|
|
#define LOG_INFO(...) log_msg(LOG_INFO, __VA_ARGS__)
|
|
|
|
|
#define LOG_WARN(...) log_msg(LOG_WARN, __VA_ARGS__)
|
|
|
|
|
#define LOG_ERROR(...) log_msg(LOG_ERROR, __VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
/* Memory allocation with error checking */
|
|
|
|
|
void *dwn_malloc(size_t size);
|
|
|
|
|
void *dwn_calloc(size_t nmemb, size_t size);
|
|
|
|
|
void *dwn_realloc(void *ptr, size_t size);
|
|
|
|
|
char *dwn_strdup(const char *s);
|
|
|
|
|
void dwn_free(void *ptr);
|
|
|
|
|
void secure_wipe(void *ptr, size_t size); /* Securely wipe sensitive data */
|
|
|
|
|
|
|
|
|
|
/* String utilities */
|
|
|
|
|
char *str_trim(char *str);
|
|
|
|
|
bool str_starts_with(const char *str, const char *prefix);
|
|
|
|
|
bool str_ends_with(const char *str, const char *suffix);
|
|
|
|
|
int str_split(char *str, char delim, char **parts, int max_parts);
|
|
|
|
|
char *shell_escape(const char *str); /* Escape string for safe shell use */
|
|
|
|
|
|
|
|
|
|
/* File utilities */
|
|
|
|
|
bool file_exists(const char *path);
|
|
|
|
|
char *file_read_all(const char *path);
|
|
|
|
|
bool file_write_all(const char *path, const char *content);
|
|
|
|
|
char *expand_path(const char *path);
|
|
|
|
|
|
|
|
|
|
/* Color utilities */
|
|
|
|
|
unsigned long parse_color(const char *color_str);
|
|
|
|
|
void color_to_rgb(unsigned long color, int *r, int *g, int *b);
|
|
|
|
|
|
|
|
|
|
/* Time utilities */
|
|
|
|
|
long get_time_ms(void);
|
|
|
|
|
void sleep_ms(int ms);
|
|
|
|
|
|
|
|
|
|
/* Process utilities */
|
|
|
|
|
int spawn(const char *cmd);
|
|
|
|
|
int spawn_async(const char *cmd);
|
|
|
|
|
char *spawn_capture(const char *cmd);
|
|
|
|
|
|
|
|
|
|
#endif /* DWN_UTIL_H */
|