v0.7.12 - Implemented comprehensive debug system with 6 levels (0-5), file:line tracking at TRACE level, deployment script integration, and default level 5 for development

This commit is contained in:
Your Name
2025-10-13 12:44:18 -04:00
parent 49ffc3d99e
commit e3938a2c85
23 changed files with 2163 additions and 622 deletions

View File

@@ -1,5 +1,6 @@
#define _GNU_SOURCE
#include "config.h"
#include "debug.h"
#include "../nostr_core_lib/nostr_core/nostr_core.h"
#include "../nostr_core_lib/nostr_core/nip017.h"
#include "../nostr_core_lib/nostr_core/nip044.h"
@@ -15,12 +16,6 @@
// External database connection (from main.c)
extern sqlite3* g_db;
// Logging functions (defined in main.c)
extern void log_info(const char* message);
extern void log_success(const char* message);
extern void log_warning(const char* message);
extern void log_error(const char* message);
// Forward declarations for unified handlers
extern int handle_auth_query_unified(cJSON* event, const char* query_type, char* error_message, size_t error_size, struct lws* wsi);
extern int handle_config_query_unified(cJSON* event, const char* query_type, char* error_message, size_t error_size, struct lws* wsi);
@@ -137,14 +132,14 @@ cJSON* process_nip17_admin_message(cJSON* gift_wrap_event, char* error_message,
// This handles commands sent as direct JSON arrays, not wrapped in inner events
int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_message, size_t error_size, struct lws* wsi) {
if (!command_array || !cJSON_IsArray(command_array) || !event) {
log_error("DM Admin: Invalid command array or event");
DEBUG_ERROR("DM Admin: Invalid command array or event");
snprintf(error_message, error_size, "invalid: null command array or event");
return -1;
}
int array_size = cJSON_GetArraySize(command_array);
if (array_size < 1) {
log_error("DM Admin: Empty command array");
DEBUG_ERROR("DM Admin: Empty command array");
snprintf(error_message, error_size, "invalid: empty command array");
return -1;
}
@@ -152,7 +147,7 @@ int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_mes
// Get the command type from the first element
cJSON* command_item = cJSON_GetArrayItem(command_array, 0);
if (!command_item || !cJSON_IsString(command_item)) {
log_error("DM Admin: First element is not a string command");
DEBUG_ERROR("DM Admin: First element is not a string command");
snprintf(error_message, error_size, "invalid: command must be a string");
return -1;
}
@@ -209,7 +204,7 @@ int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_mes
if (strcmp(command_type, "auth_query") == 0) {
const char* query_type = get_tag_value(event, "auth_query", 1);
if (!query_type) {
log_error("DM Admin: Missing auth_query type parameter");
DEBUG_ERROR("DM Admin: Missing auth_query type parameter");
snprintf(error_message, error_size, "invalid: missing auth_query type");
} else {
result = handle_auth_query_unified(event, query_type, error_message, error_size, wsi);
@@ -218,7 +213,7 @@ int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_mes
else if (strcmp(command_type, "config_query") == 0) {
const char* query_type = get_tag_value(event, "config_query", 1);
if (!query_type) {
log_error("DM Admin: Missing config_query type parameter");
DEBUG_ERROR("DM Admin: Missing config_query type parameter");
snprintf(error_message, error_size, "invalid: missing config_query type");
} else {
result = handle_config_query_unified(event, query_type, error_message, error_size, wsi);
@@ -228,7 +223,7 @@ int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_mes
const char* config_key = get_tag_value(event, "config_set", 1);
const char* config_value = get_tag_value(event, "config_set", 2);
if (!config_key || !config_value) {
log_error("DM Admin: Missing config_set parameters");
DEBUG_ERROR("DM Admin: Missing config_set parameters");
snprintf(error_message, error_size, "invalid: missing config_set key or value");
} else {
result = handle_config_set_unified(event, config_key, config_value, error_message, error_size, wsi);
@@ -240,7 +235,7 @@ int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_mes
else if (strcmp(command_type, "system_command") == 0) {
const char* command = get_tag_value(event, "system_command", 1);
if (!command) {
log_error("DM Admin: Missing system_command type parameter");
DEBUG_ERROR("DM Admin: Missing system_command type parameter");
snprintf(error_message, error_size, "invalid: missing system_command type");
} else {
result = handle_system_command_unified(event, command, error_message, error_size, wsi);
@@ -253,13 +248,13 @@ int process_dm_admin_command(cJSON* command_array, cJSON* event, char* error_mes
result = handle_auth_rule_modification_unified(event, error_message, error_size, wsi);
}
else {
log_error("DM Admin: Unknown command type");
DEBUG_ERROR("DM Admin: Unknown command type");
printf(" Unknown command: %s\n", command_type);
snprintf(error_message, error_size, "invalid: unknown DM command type '%s'", command_type);
}
if (result != 0) {
log_error("DM Admin: Command processing failed");
DEBUG_ERROR("DM Admin: Command processing failed");
}
return result;
@@ -592,7 +587,7 @@ int apply_config_change(const char* key, const char* value) {
extern sqlite3* g_db;
if (!g_db) {
log_error("Database not available for config change");
DEBUG_ERROR("Database not available for config change");
return -1;
}
@@ -628,9 +623,9 @@ int apply_config_change(const char* key, const char* value) {
const char* sql = "INSERT OR REPLACE INTO config (key, value, data_type) VALUES (?, ?, ?)";
if (sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL) != SQLITE_OK) {
log_error("Failed to prepare config update statement");
DEBUG_ERROR("Failed to prepare config update statement");
const char* err_msg = sqlite3_errmsg(g_db);
log_error(err_msg);
DEBUG_ERROR(err_msg);
return -1;
}
@@ -640,9 +635,9 @@ int apply_config_change(const char* key, const char* value) {
int result = sqlite3_step(stmt);
if (result != SQLITE_DONE) {
log_error("Failed to update configuration in database");
DEBUG_ERROR("Failed to update configuration in database");
const char* err_msg = sqlite3_errmsg(g_db);
log_error(err_msg);
DEBUG_ERROR(err_msg);
sqlite3_finalize(stmt);
return -1;
}
@@ -766,7 +761,7 @@ int handle_config_confirmation(const char* admin_pubkey, const char* response) {
char error_msg[256];
int send_result = send_nip17_response(admin_pubkey, success_msg, error_msg, sizeof(error_msg));
if (send_result != 0) {
log_error(error_msg);
DEBUG_ERROR(error_msg);
}
// Remove the pending change
@@ -788,7 +783,7 @@ int handle_config_confirmation(const char* admin_pubkey, const char* response) {
char send_error_msg[256];
int send_result = send_nip17_response(admin_pubkey, error_msg, send_error_msg, sizeof(send_error_msg));
if (send_result != 0) {
log_error(send_error_msg);
DEBUG_ERROR(send_error_msg);
}
// Remove the pending change
@@ -890,7 +885,7 @@ int process_config_change_request(const char* admin_pubkey, const char* message)
char error_msg[256];
int send_result = send_nip17_response(admin_pubkey, confirmation, error_msg, sizeof(error_msg));
if (send_result != 0) {
log_error(error_msg);
DEBUG_ERROR(error_msg);
}
free(confirmation);
}
@@ -903,7 +898,7 @@ int process_config_change_request(const char* admin_pubkey, const char* message)
char* generate_stats_json(void) {
extern sqlite3* g_db;
if (!g_db) {
log_error("Database not available for stats generation");
DEBUG_ERROR("Database not available for stats generation");
return NULL;
}
@@ -1007,7 +1002,7 @@ char* generate_stats_json(void) {
cJSON_Delete(response);
if (!json_string) {
log_error("Failed to generate stats JSON");
DEBUG_ERROR("Failed to generate stats JSON");
}
return json_string;
@@ -1113,14 +1108,14 @@ int send_nip17_response(const char* sender_pubkey, const char* response_content,
char* generate_config_text(void) {
extern sqlite3* g_db;
if (!g_db) {
log_error("NIP-17: Database not available for config query");
DEBUG_ERROR("NIP-17: Database not available for config query");
return NULL;
}
// Build comprehensive config text from database
char* config_text = malloc(8192);
if (!config_text) {
log_error("NIP-17: Failed to allocate memory for config text");
DEBUG_ERROR("NIP-17: Failed to allocate memory for config text");
return NULL;
}
@@ -1146,7 +1141,7 @@ char* generate_config_text(void) {
sqlite3_finalize(stmt);
} else {
free(config_text);
log_error("NIP-17: Failed to query config from database");
DEBUG_ERROR("NIP-17: Failed to query config from database");
return NULL;
}
@@ -1161,7 +1156,7 @@ char* generate_config_text(void) {
char* generate_stats_text(void) {
char* stats_json = generate_stats_json();
if (!stats_json) {
log_error("NIP-17: Failed to generate stats for plain text command");
DEBUG_ERROR("NIP-17: Failed to generate stats for plain text command");
return NULL;
}
@@ -1345,7 +1340,7 @@ cJSON* process_nip17_admin_message(cJSON* gift_wrap_event, char* error_message,
// Convert hex private key to bytes
unsigned char relay_privkey[32];
if (nostr_hex_to_bytes(relay_privkey_hex, relay_privkey, sizeof(relay_privkey)) != 0) {
log_error("NIP-17: Failed to convert relay private key from hex");
DEBUG_ERROR("NIP-17: Failed to convert relay private key from hex");
free(relay_privkey_hex);
strncpy(error_message, "NIP-17: Failed to convert relay private key", error_size - 1);
return NULL;
@@ -1355,13 +1350,13 @@ cJSON* process_nip17_admin_message(cJSON* gift_wrap_event, char* error_message,
// Step 3: Decrypt and parse inner event using library function
cJSON* inner_dm = nostr_nip17_receive_dm(gift_wrap_event, relay_privkey);
if (!inner_dm) {
log_error("NIP-17: nostr_nip17_receive_dm returned NULL");
DEBUG_ERROR("NIP-17: nostr_nip17_receive_dm returned NULL");
// Debug: Print the gift wrap event
char* gift_wrap_debug = cJSON_Print(gift_wrap_event);
if (gift_wrap_debug) {
char debug_msg[1024];
snprintf(debug_msg, sizeof(debug_msg), "NIP-17: Gift wrap event: %.500s", gift_wrap_debug);
log_error(debug_msg);
DEBUG_ERROR(debug_msg);
free(gift_wrap_debug);
}
// Debug: Check if private key is valid
@@ -1525,7 +1520,7 @@ int is_nip17_gift_wrap_for_relay(cJSON* event) {
const char* relay_pubkey = get_relay_pubkey_cached();
if (!relay_pubkey) {
log_error("NIP-17: Could not get relay pubkey for validation");
DEBUG_ERROR("NIP-17: Could not get relay pubkey for validation");
return 0;
}
@@ -1605,7 +1600,7 @@ int process_nip17_admin_command(cJSON* dm_event, char* error_message, size_t err
free(stats_text);
if (result != 0) {
log_error(error_msg);
DEBUG_ERROR(error_msg);
return -1;
}
@@ -1623,7 +1618,7 @@ int process_nip17_admin_command(cJSON* dm_event, char* error_message, size_t err
free(config_text);
if (result != 0) {
log_error(error_msg);
DEBUG_ERROR(error_msg);
return -1;
}
@@ -1657,7 +1652,7 @@ int process_nip17_admin_command(cJSON* dm_event, char* error_message, size_t err
if (config_result > 0) {
return 1; // Return positive value to indicate response was handled
} else {
log_error("NIP-17: Configuration change request failed");
DEBUG_ERROR("NIP-17: Configuration change request failed");
return -1; // Return error to prevent generic success response
}
}
@@ -1697,7 +1692,7 @@ int process_nip17_admin_command(cJSON* dm_event, char* error_message, size_t err
cJSON_Delete(command_array);
if (result != 0) {
log_error(error_msg);
DEBUG_ERROR(error_msg);
strncpy(error_message, error_msg, error_size - 1);
return -1;
}