| 1 | /* Error handling and logging utilities */ |
| 2 | |
| 3 | #ifndef ERROR_H |
| 4 | #define ERROR_H |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdarg.h> |
| 8 | #include <errno.h> |
| 9 | #include <stdbool.h> |
| 10 | |
| 11 | /* Error codes */ |
| 12 | typedef enum { |
| 13 | ERR_SUCCESS = 0, |
| 14 | ERR_INVALID_ARGS = 1, |
| 15 | ERR_CONFIG_NOT_FOUND = 2, |
| 16 | ERR_CONFIG_INVALID = 3, |
| 17 | ERR_CONFIG_WRITE_FAILED = 4, |
| 18 | ERR_ACCOUNT_NOT_FOUND = 5, |
| 19 | ERR_ACCOUNT_INVALID = 6, |
| 20 | ERR_ACCOUNT_EXISTS = 7, |
| 21 | ERR_GIT_NOT_FOUND = 8, |
| 22 | ERR_GIT_CONFIG_FAILED = 9, |
| 23 | ERR_GIT_NOT_REPO = 10, |
| 24 | ERR_GIT_NOT_REPOSITORY = 10, /* Alias for consistency */ |
| 25 | ERR_GIT_CONFIG_NOT_FOUND = 11, |
| 26 | ERR_GIT_REPOSITORY_INVALID = 12, |
| 27 | ERR_SSH_AGENT_FAILED = 13, |
| 28 | ERR_SSH_KEY_FAILED = 14, |
| 29 | ERR_SSH_KEY_NOT_FOUND = 15, |
| 30 | ERR_SSH_CONNECTION_FAILED = 16, |
| 31 | ERR_SSH_NOT_FOUND = 29, |
| 32 | ERR_SSH_AGENT_NOT_FOUND = 30, |
| 33 | ERR_SSH_KEY_LOAD_FAILED = 31, |
| 34 | ERR_SSH_AGENT_START_FAILED = 32, |
| 35 | ERR_SSH_KEY_INVALID = 33, |
| 36 | ERR_SSH_KEY_PERMISSIONS = 34, |
| 37 | ERR_SSH_KEY_OWNERSHIP = 35, |
| 38 | ERR_SSH_AGENT_SOCKET_INVALID = 36, |
| 39 | ERR_GPG_NOT_FOUND = 37, |
| 40 | ERR_GPG_KEY_FAILED = 38, |
| 41 | ERR_GPG_KEY_NOT_FOUND = 39, |
| 42 | ERR_GPG_SIGNING_FAILED = 40, |
| 43 | ERR_MEMORY_ALLOCATION = 41, |
| 44 | ERR_FILE_IO = 42, |
| 45 | ERR_PERMISSION_DENIED = 43, |
| 46 | ERR_NETWORK_ERROR = 44, |
| 47 | ERR_SYSTEM_CALL = 45, |
| 48 | ERR_SYSTEM_REQUIREMENT = 46, |
| 49 | ERR_SYSTEM_COMMAND_FAILED = 47, |
| 50 | ERR_INVALID_PATH = 48, |
| 51 | ERR_UNKNOWN = 99 |
| 52 | } error_code_t; |
| 53 | |
| 54 | /* Log levels */ |
| 55 | typedef enum { |
| 56 | LOG_LEVEL_DEBUG, |
| 57 | LOG_LEVEL_INFO, |
| 58 | LOG_LEVEL_WARNING, |
| 59 | LOG_LEVEL_ERROR, |
| 60 | LOG_LEVEL_CRITICAL |
| 61 | } log_level_t; |
| 62 | |
| 63 | /* Error context for detailed error reporting */ |
| 64 | typedef struct { |
| 65 | error_code_t code; |
| 66 | char message[512]; |
| 67 | char details[1024]; |
| 68 | const char *file; |
| 69 | int line; |
| 70 | const char *function; |
| 71 | int system_errno; |
| 72 | } error_context_t; |
| 73 | |
| 74 | /* Global error context */ |
| 75 | extern error_context_t g_last_error; |
| 76 | |
| 77 | /* Logging configuration */ |
| 78 | extern log_level_t g_log_level; |
| 79 | extern FILE *g_log_file; |
| 80 | extern bool g_log_to_stderr; |
| 81 | |
| 82 | /* Macros for error reporting with context */ |
| 83 | #define set_error(code, fmt, ...) \ |
| 84 | set_error_context((code), __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 85 | |
| 86 | #define set_system_error(code, fmt, ...) \ |
| 87 | set_system_error_context((code), __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 88 | |
| 89 | /* Logging macros */ |
| 90 | #define log_debug(fmt, ...) \ |
| 91 | log_message(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 92 | |
| 93 | #define log_info(fmt, ...) \ |
| 94 | log_message(LOG_LEVEL_INFO, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 95 | |
| 96 | #define log_warning(fmt, ...) \ |
| 97 | log_message(LOG_LEVEL_WARNING, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 98 | |
| 99 | #define log_error(fmt, ...) \ |
| 100 | log_message(LOG_LEVEL_ERROR, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 101 | |
| 102 | #define log_critical(fmt, ...) \ |
| 103 | log_message(LOG_LEVEL_CRITICAL, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 104 | |
| 105 | /* Function prototypes */ |
| 106 | |
| 107 | /** |
| 108 | * Initialize error handling and logging system |
| 109 | */ |
| 110 | int error_init(log_level_t level, const char *log_file_path); |
| 111 | |
| 112 | /** |
| 113 | * Cleanup error handling system |
| 114 | */ |
| 115 | void error_cleanup(void); |
| 116 | |
| 117 | /** |
| 118 | * Set error context with detailed information |
| 119 | */ |
| 120 | void set_error_context(error_code_t code, const char *file, int line, |
| 121 | const char *function, const char *fmt, ...); |
| 122 | |
| 123 | /** |
| 124 | * Set error context including system errno |
| 125 | */ |
| 126 | void set_system_error_context(error_code_t code, const char *file, int line, |
| 127 | const char *function, const char *fmt, ...); |
| 128 | |
| 129 | /** |
| 130 | * Get last error information |
| 131 | */ |
| 132 | const error_context_t *get_last_error(void); |
| 133 | |
| 134 | /** |
| 135 | * Clear last error |
| 136 | */ |
| 137 | void clear_error(void); |
| 138 | |
| 139 | /** |
| 140 | * Convert error code to human-readable string |
| 141 | */ |
| 142 | const char *error_code_to_string(error_code_t code); |
| 143 | |
| 144 | /** |
| 145 | * Log message with context information |
| 146 | */ |
| 147 | void log_message(log_level_t level, const char *file, int line, |
| 148 | const char *function, const char *fmt, ...); |
| 149 | |
| 150 | /** |
| 151 | * Set logging level |
| 152 | */ |
| 153 | void set_log_level(log_level_t level); |
| 154 | |
| 155 | /** |
| 156 | * Set log output file (NULL for stderr) |
| 157 | */ |
| 158 | int set_log_file(const char *file_path); |
| 159 | |
| 160 | /** |
| 161 | * Enable/disable logging to stderr |
| 162 | */ |
| 163 | void set_log_to_stderr(bool enable); |
| 164 | |
| 165 | /** |
| 166 | * Format error message for user display |
| 167 | */ |
| 168 | void format_error_message(char *buffer, size_t buffer_size, |
| 169 | const error_context_t *error); |
| 170 | |
| 171 | /** |
| 172 | * Print formatted error to stderr |
| 173 | */ |
| 174 | void print_error(const char *prefix); |
| 175 | |
| 176 | /** |
| 177 | * Check if error level should be logged |
| 178 | */ |
| 179 | bool should_log(log_level_t level); |
| 180 | |
| 181 | /** |
| 182 | * Get current timestamp for logging |
| 183 | */ |
| 184 | void get_timestamp(char *buffer, size_t buffer_size); |
| 185 | |
| 186 | /** |
| 187 | * Safe string functions that set error context on failure |
| 188 | */ |
| 189 | int safe_strncpy(char *dest, const char *src, size_t dest_size); |
| 190 | int safe_strncat(char *dest, const char *src, size_t dest_size); |
| 191 | int safe_snprintf(char *buffer, size_t buffer_size, const char *fmt, ...); |
| 192 | |
| 193 | /** |
| 194 | * Memory allocation functions that set error context on failure |
| 195 | */ |
| 196 | void *safe_malloc(size_t size); |
| 197 | void *safe_calloc(size_t nmemb, size_t size); |
| 198 | void *safe_realloc(void *ptr, size_t size); |
| 199 | |
| 200 | #endif /* ERROR_H */ |