refactor: standardize error reporting on the log module
- Add log_perror() helper (context + strerror(errno)) to the log module - Replace all bare perror() calls with log_perror() so errors are routed through the unified logger (stderr sink + optional --log-file sink) - Convert fprintf(stderr, "Error:/Warning: ...") in client code to log_message(); raw fprintf kept only for progress/stats output
This commit is contained in:
+25
-25
@@ -60,7 +60,7 @@ static bool parse_positive_int(const char* s, int* out_val) {
|
||||
static int set_string_option(char** dest, const char* value, const char* option_name) {
|
||||
char* dup = str_dup(value);
|
||||
if (!dup) {
|
||||
fprintf(stderr, "Error: memory allocation failed for %s\n", option_name);
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed for %s", option_name);
|
||||
return -1;
|
||||
}
|
||||
free(*dest);
|
||||
@@ -71,7 +71,7 @@ static int set_string_option(char** dest, const char* value, const char* option_
|
||||
/* Parse a string as a positive integer into *dest. Returns true on success, false on error. */
|
||||
static int set_positive_int_option(int* dest, const char* value, const char* option_name) {
|
||||
if (!parse_positive_int(value, dest)) {
|
||||
fprintf(stderr, "Error: %s must be a positive integer\n", option_name);
|
||||
log_message(LOG_LEVEL_ERROR, "%s must be a positive integer", option_name);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -80,7 +80,7 @@ static int set_positive_int_option(int* dest, const char* value, const char* opt
|
||||
/* Parse a string as a non-negative integer into *dest. Returns true on success, false on error. */
|
||||
static int set_nonneg_int_option(int* dest, const char* value, const char* option_name) {
|
||||
if (!parse_nonneg_int(value, dest)) {
|
||||
fprintf(stderr, "Error: %s must be a non-negative integer\n", option_name);
|
||||
log_message(LOG_LEVEL_ERROR, "%s must be a non-negative integer", option_name);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -94,7 +94,7 @@ static int parse_ull_arg(const char* val, unsigned long long* out, const char* o
|
||||
errno = 0;
|
||||
unsigned long long v = strtoull(val, &end, 10);
|
||||
if (errno != 0 || *end != '\0') {
|
||||
fprintf(stderr, "Error: %s must be a non-negative integer\n", optname);
|
||||
log_message(LOG_LEVEL_ERROR, "%s must be a non-negative integer", optname);
|
||||
return -1;
|
||||
}
|
||||
*out = v;
|
||||
@@ -106,13 +106,13 @@ static int config_add_pattern(char*** patterns, int* count, const char* value,
|
||||
const char* optname) {
|
||||
char** tmp = realloc(*patterns, (*count + 1) * sizeof(char*));
|
||||
if (!tmp) {
|
||||
fprintf(stderr, "Error: memory allocation failed for %s\n", optname);
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed for %s", optname);
|
||||
return -1;
|
||||
}
|
||||
*patterns = tmp;
|
||||
char* dup = str_dup(value);
|
||||
if (!dup) {
|
||||
fprintf(stderr, "Error: memory allocation failed for %s\n", optname);
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed for %s", optname);
|
||||
return -1;
|
||||
}
|
||||
(*patterns)[(*count)++] = dup;
|
||||
@@ -215,7 +215,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (entry) {
|
||||
if (entry->kind != OPT_FLAG) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "Error: missing argument for %s\n", entry->name);
|
||||
log_message(LOG_LEVEL_ERROR, "missing argument for %s", entry->name);
|
||||
return -1;
|
||||
}
|
||||
if (apply_table_option(config, entry, argv[++i]) != 0)
|
||||
@@ -241,7 +241,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (set_positive_int_option(&config->ssh_port, argv[++i], "-p") != 0)
|
||||
return -1;
|
||||
if (config->ssh_port > 65535) {
|
||||
log_message(LOG_LEVEL_ERROR, "SSH port must be 1-65535\n");
|
||||
log_message(LOG_LEVEL_ERROR, "SSH port must be 1-65535");
|
||||
return -1;
|
||||
}
|
||||
} else if (opt_is(argv[i], "--exclude", NULL) && i + 1 < argc) {
|
||||
@@ -259,7 +259,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (val >= DELTA_BLOCK_SIZE_MIN && val <= DELTA_BLOCK_SIZE_MAX)
|
||||
config->delta_block_size = (uint32_t)val;
|
||||
else
|
||||
fprintf(stderr, "Warning: --delta-block value %llu out of range, using default\n", val);
|
||||
log_message(LOG_LEVEL_WARNING, "--delta-block value %llu out of range, using default", val);
|
||||
} else if (opt_is(argv[i], "--delta-max", NULL) && i + 1 < argc) {
|
||||
unsigned long long val;
|
||||
if (parse_ull_arg(argv[++i], &val, "--delta-max") != 0)
|
||||
@@ -267,7 +267,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (val >= DELTA_MIN_FILE_SIZE)
|
||||
config->delta_max_file_size = val;
|
||||
else
|
||||
fprintf(stderr, "Warning: --delta-max value %llu too small, using default\n", val);
|
||||
log_message(LOG_LEVEL_WARNING, "--delta-max value %llu too small, using default", val);
|
||||
} else if (opt_is(argv[i], "-c", "-z")) {
|
||||
config->use_compression = true;
|
||||
log_message(LOG_LEVEL_INFO, "Enabled Compression");
|
||||
@@ -276,7 +276,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
long level = strtol(argv[i + 1], &end_ptr, 10);
|
||||
if (*end_ptr == '\0') {
|
||||
if (level < 1 || level > 22) {
|
||||
fprintf(stderr, "Error: compression level must be 1-22\n");
|
||||
log_message(LOG_LEVEL_ERROR, "compression level must be 1-22");
|
||||
return -1;
|
||||
}
|
||||
config->compression_level = (int)level;
|
||||
@@ -298,11 +298,11 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
log_message(LOG_LEVEL_INFO, "Enabled Chunk Serialization");
|
||||
} else if (opt_is(argv[i], "--server-port", NULL) && i + 1 < argc) {
|
||||
if (!parse_positive_int(argv[++i], &config->server_port)) {
|
||||
fprintf(stderr, "Error: invalid --server-port value: %s\n", argv[i]);
|
||||
log_message(LOG_LEVEL_ERROR, "invalid --server-port value: %s", argv[i]);
|
||||
return -1;
|
||||
}
|
||||
if (config->server_port > 65535) {
|
||||
fprintf(stderr, "Error: server port must be 1-65535\n");
|
||||
log_message(LOG_LEVEL_ERROR, "server port must be 1-65535");
|
||||
return -1;
|
||||
}
|
||||
} else if (opt_is(argv[i], "--bwlimit", NULL) && i + 1 < argc) {
|
||||
@@ -310,11 +310,11 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (parse_ull_arg(argv[++i], &kbps, "--bwlimit") != 0)
|
||||
return -1;
|
||||
if (kbps == 0) {
|
||||
fprintf(stderr, "Error: --bwlimit must be a positive integer\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--bwlimit must be a positive integer");
|
||||
return -1;
|
||||
}
|
||||
if (kbps > ULLONG_MAX / 1024) {
|
||||
fprintf(stderr, "Error: --bwlimit value too large\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--bwlimit value too large");
|
||||
return -1;
|
||||
}
|
||||
io_set_bwlimit(kbps * 1024);
|
||||
@@ -324,7 +324,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (parse_ull_arg(argv[++i], &val, "--chunk-size") != 0)
|
||||
return -1;
|
||||
if (val == 0) {
|
||||
fprintf(stderr, "Error: --chunk-size must be a positive integer\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--chunk-size must be a positive integer");
|
||||
return -1;
|
||||
}
|
||||
config->chunk_size = val;
|
||||
@@ -336,7 +336,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
}
|
||||
FILE* lf = fopen(argv[++i], "a");
|
||||
if (!lf) {
|
||||
fprintf(stderr, "Error: could not open log file '%s': %s\n", argv[i], strerror(errno));
|
||||
log_message(LOG_LEVEL_ERROR, "could not open log file '%s': %s", argv[i], strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
config->log_file = lf;
|
||||
@@ -358,7 +358,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
if (set_positive_int_option(&config->compression_level, argv[++i], "--compress-level") != 0)
|
||||
return -1;
|
||||
if (config->compression_level < 1 || config->compression_level > 22) {
|
||||
fprintf(stderr, "Error: --compress-level must be between 1 and 22\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--compress-level must be between 1 and 22");
|
||||
return -1;
|
||||
}
|
||||
} else if (argv[i][0] == '-') {
|
||||
@@ -381,7 +381,7 @@ int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
static int read_patterns_from_file(const char* filepath, char*** patterns, int* count) {
|
||||
FILE* fp = fopen(filepath, "r");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Error: could not open pattern file '%s': %s\n", filepath, strerror(errno));
|
||||
log_message(LOG_LEVEL_ERROR, "could not open pattern file '%s': %s", filepath, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
char* line = NULL;
|
||||
@@ -420,7 +420,7 @@ int main(int argc, char* argv[]) {
|
||||
bool config_owned_by_pipeline = false;
|
||||
Config* config = config_create();
|
||||
if (!config) {
|
||||
fprintf(stderr, "Error: failed to allocate config\n");
|
||||
log_message(LOG_LEVEL_ERROR, "failed to allocate config");
|
||||
return 1;
|
||||
}
|
||||
config->save_to_disk = save_to_disk;
|
||||
@@ -441,20 +441,20 @@ int main(int argc, char* argv[]) {
|
||||
free(config->receive_root_directory);
|
||||
config->send_directory = str_dup(argv[positional_args[0]]);
|
||||
if (!config->send_directory) {
|
||||
fprintf(stderr, "Error: memory allocation failed\n");
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed");
|
||||
exit_code = 1;
|
||||
goto cleanup;
|
||||
}
|
||||
config->receive_root_directory = str_dup(argv[positional_args[1]]);
|
||||
if (!config->receive_root_directory) {
|
||||
fprintf(stderr, "Error: memory allocation failed\n");
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed");
|
||||
exit_code = 1;
|
||||
goto cleanup;
|
||||
}
|
||||
config->save_to_disk = true;
|
||||
config_parse_ssh_dest(config);
|
||||
} else if (positional_count == 1) {
|
||||
fprintf(stderr, "Error: missing destination argument\n");
|
||||
log_message(LOG_LEVEL_ERROR, "missing destination argument");
|
||||
print_usage();
|
||||
exit_code = 1;
|
||||
goto cleanup;
|
||||
@@ -462,7 +462,7 @@ int main(int argc, char* argv[]) {
|
||||
if (!config->send_directory && env_source) {
|
||||
config->send_directory = str_dup(env_source);
|
||||
if (!config->send_directory) {
|
||||
fprintf(stderr, "Error: memory allocation failed\n");
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed");
|
||||
exit_code = 1;
|
||||
goto cleanup;
|
||||
}
|
||||
@@ -470,7 +470,7 @@ int main(int argc, char* argv[]) {
|
||||
if (!config->receive_root_directory && env_dest) {
|
||||
config->receive_root_directory = str_dup(env_dest);
|
||||
if (!config->receive_root_directory) {
|
||||
fprintf(stderr, "Error: memory allocation failed\n");
|
||||
log_message(LOG_LEVEL_ERROR, "memory allocation failed");
|
||||
exit_code = 1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ static ScannerOptions scanner_options_from_config(const Config* config, int num_
|
||||
static Client* connect_transfer_client(const Config* config) {
|
||||
if (config->transport == TRANSPORT_SSH) {
|
||||
if (config->use_sendfile) {
|
||||
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
|
||||
log_message(LOG_LEVEL_ERROR, "-f/--sendfile is not supported with SSH transport");
|
||||
return NULL;
|
||||
}
|
||||
return client_connect_ssh(config->ssh_destination, config->ssh_port,
|
||||
@@ -377,7 +377,7 @@ static int send_chunks_multithreaded(void* pipeline_context) {
|
||||
Client* client = connect_transfer_client(context->config);
|
||||
if (!client) {
|
||||
if (context->config->transport == TRANSPORT_TCP)
|
||||
fprintf(stderr, "Error: could not connect to server%s\n",
|
||||
log_message(LOG_LEVEL_ERROR, "could not connect to server%s",
|
||||
context->config->use_tls ? " via TLS" : "");
|
||||
pipeline_cancel(context);
|
||||
mark_sender_done(context);
|
||||
@@ -425,7 +425,7 @@ static int send_chunks_multithreaded(void* pipeline_context) {
|
||||
return thrd_error;
|
||||
}
|
||||
if (send_chunk(client, current_chunk, context->config) != 0) {
|
||||
fprintf(stderr, "Error: unexpected error while sending chunk\n");
|
||||
log_message(LOG_LEVEL_ERROR, "unexpected error while sending chunk");
|
||||
chunk_destroy(current_chunk);
|
||||
pipeline_cancel(context);
|
||||
disconnect_transfer_client(client);
|
||||
@@ -585,7 +585,7 @@ int send_files(Config* config) {
|
||||
Client* client = connect_transfer_client(config);
|
||||
if (!client) {
|
||||
if (config->transport == TRANSPORT_TCP)
|
||||
fprintf(stderr, "Error: could not connect to server%s\n", config->use_tls ? " via TLS" : "");
|
||||
log_message(LOG_LEVEL_ERROR, "could not connect to server%s", config->use_tls ? " via TLS" : "");
|
||||
return 1;
|
||||
}
|
||||
ProtocolSession session;
|
||||
@@ -736,7 +736,7 @@ int send_files_multithreaded(Config* config) {
|
||||
sender_created = (thrd_create(&sender, send_chunks_multithreaded, context) == thrd_success);
|
||||
|
||||
if (!scanner_created || !loader_created || !sender_created) {
|
||||
perror("Error creating threads");
|
||||
log_perror("Error creating threads");
|
||||
pipeline_cancel(context);
|
||||
mtx_lock(&context->mutex_progress);
|
||||
context->sender_done = true;
|
||||
@@ -756,7 +756,7 @@ int send_files_multithreaded(Config* config) {
|
||||
if (config->show_progress) {
|
||||
progress_created = (thrd_create(&progress, progress_thread_fn, context) == thrd_success);
|
||||
if (!progress_created) {
|
||||
perror("Error creating progress thread");
|
||||
log_perror("Error creating progress thread");
|
||||
/* Non-fatal; continue without progress reporting */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,38 @@
|
||||
#include "client_validation.h"
|
||||
#include "log.h"
|
||||
#include "usage.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/* Validate config after parsing. Returns true if valid. */
|
||||
bool validate_config(const Config* config) {
|
||||
if (!config->send_directory || !config->receive_root_directory) {
|
||||
fprintf(stderr, "Error: source and destination directories are required\n");
|
||||
log_message(LOG_LEVEL_ERROR, "source and destination directories are required");
|
||||
print_usage();
|
||||
return false;
|
||||
}
|
||||
if (config->use_sendfile && (config->use_chunk_serialization || config->use_compression)) {
|
||||
fprintf(stderr, "Error: -f/--sendfile cannot be combined with -c (compression) or -s (chunk "
|
||||
"serialization)\n");
|
||||
log_message(LOG_LEVEL_ERROR, "-f/--sendfile cannot be combined with -c (compression) or -s "
|
||||
"(chunk serialization)");
|
||||
return false;
|
||||
}
|
||||
if (config->transport == TRANSPORT_SSH && config->use_sendfile) {
|
||||
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
|
||||
log_message(LOG_LEVEL_ERROR, "-f/--sendfile is not supported with SSH transport");
|
||||
return false;
|
||||
}
|
||||
if (config->use_incremental && config->use_chunk_serialization) {
|
||||
fprintf(stderr, "Error: --incremental is not supported with -s (chunk serialization)\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--incremental is not supported with -s (chunk serialization)");
|
||||
return false;
|
||||
}
|
||||
if (config->use_delta && !config->use_incremental) {
|
||||
fprintf(stderr, "Error: --delta requires --incremental\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--delta requires --incremental");
|
||||
return false;
|
||||
}
|
||||
if (config->use_delta && config->use_chunk_serialization) {
|
||||
fprintf(stderr, "Error: --delta cannot be combined with -s (chunk serialization)\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--delta cannot be combined with -s (chunk serialization)");
|
||||
return false;
|
||||
}
|
||||
if (config->use_delta && config->use_sendfile) {
|
||||
fprintf(stderr, "Error: --delta cannot be combined with -f (sendfile)\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--delta cannot be combined with -f (sendfile)");
|
||||
return false;
|
||||
}
|
||||
if (config->append || config->append_verify) {
|
||||
@@ -42,7 +43,7 @@ bool validate_config(const Config* config) {
|
||||
}
|
||||
if (config->use_tls) {
|
||||
if (!config->tls_cert || !config->tls_key) {
|
||||
fprintf(stderr, "Error: --tls requires --cert and --key\n");
|
||||
log_message(LOG_LEVEL_ERROR, "--tls requires --cert and --key");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "log.h"
|
||||
#include "scanner.h"
|
||||
#include "array_list.h"
|
||||
#include "chunk.h"
|
||||
@@ -226,7 +227,7 @@ static int open_next_directory(DirectoryScanner* scanner) {
|
||||
free(de);
|
||||
scanner->current_dir = opendir(scanner->current_path);
|
||||
if (scanner->current_dir == NULL) {
|
||||
perror("Could not open directory");
|
||||
log_perror("Could not open directory");
|
||||
free(scanner->current_path);
|
||||
scanner->current_path = NULL;
|
||||
scanner->failed = true;
|
||||
@@ -564,7 +565,7 @@ static bool scan_root_directory(ParallelScanner* ps, const char* root_directory,
|
||||
ArrayList* subdirs) {
|
||||
DIR* dir = opendir(root_directory);
|
||||
if (!dir) {
|
||||
perror("Could not open root directory for parallel scan");
|
||||
log_perror("Could not open root directory for parallel scan");
|
||||
return false;
|
||||
}
|
||||
const struct dirent* entry;
|
||||
|
||||
+1
-1
@@ -129,7 +129,7 @@ void handler(int file_descriptor) {
|
||||
if (receiver_created)
|
||||
writer_created = thrd_create(&writer, write_thread, context) == thrd_success;
|
||||
if (!receiver_created || !writer_created) {
|
||||
perror("Error creating Threads");
|
||||
log_perror("Error creating Threads");
|
||||
if (receiver_created) {
|
||||
mtx_lock(&context->mutex);
|
||||
atomic_store(&context->cancelled, true);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "log.h"
|
||||
#include "array_list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -6,7 +7,7 @@
|
||||
ArrayList* array_list_create(void (*item_destroyer)(void* item)) {
|
||||
ArrayList* list = (ArrayList*)malloc(sizeof(ArrayList));
|
||||
if (list == NULL) {
|
||||
perror("ERROR: Could not allocate memory for array list struct");
|
||||
log_perror("ERROR: Could not allocate memory for array list struct");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ static bool array_list_extend(ArrayList* array_list) {
|
||||
new_capacity = INITIAL_ARRAY_SIZE;
|
||||
void* new_items = realloc(array_list->items, new_capacity * sizeof(void*));
|
||||
if (new_items == NULL) {
|
||||
perror("ERROR: Could not reallocate memory for array list items");
|
||||
log_perror("ERROR: Could not reallocate memory for array list items");
|
||||
return false;
|
||||
}
|
||||
array_list->items = new_items;
|
||||
@@ -68,7 +69,7 @@ void** array_list_to_array(const ArrayList* array_list) {
|
||||
}
|
||||
void** array = malloc(array_list->size * sizeof(void*));
|
||||
if (array == NULL) {
|
||||
perror("Could not malloc space for array from array list!");
|
||||
log_perror("Could not malloc space for array from array list!");
|
||||
return NULL;
|
||||
}
|
||||
memcpy(array, array_list->items, array_list->size * sizeof(void*));
|
||||
|
||||
+3
-3
@@ -20,7 +20,7 @@
|
||||
Chunk* chunk_create(File** items, int element_count) {
|
||||
Chunk* chunk = (Chunk*)malloc(sizeof(Chunk));
|
||||
if (chunk == NULL) {
|
||||
perror("ERROR: Could not allocate memory for chunk structure");
|
||||
log_perror("ERROR: Could not allocate memory for chunk structure");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
|
||||
|
||||
char* path = malloc(path_len + 1);
|
||||
if (path == NULL) {
|
||||
perror("Could not allocate memory for file path");
|
||||
log_perror("Could not allocate memory for file path");
|
||||
array_list_delete(files);
|
||||
return NULL;
|
||||
}
|
||||
@@ -191,7 +191,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
|
||||
|
||||
void* file_data = malloc(file_data_size > 0 ? file_data_size : 1);
|
||||
if (file_data == NULL) {
|
||||
perror("Could not allocate memory for file data");
|
||||
log_perror("Could not allocate memory for file data");
|
||||
file_destroy(file);
|
||||
array_list_delete(files);
|
||||
return NULL;
|
||||
|
||||
+5
-5
@@ -29,7 +29,7 @@ File* file_create(const char* path) {
|
||||
return NULL;
|
||||
File* file = (File*)malloc(sizeof(File));
|
||||
if (file == NULL) {
|
||||
perror("ERROR: Could not allocate memory for file struct");
|
||||
log_perror("ERROR: Could not allocate memory for file struct");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ void file_destroy(void* item) {
|
||||
FileMetadata* file_metadata_create(const struct stat* stats) {
|
||||
FileMetadata* m = malloc(sizeof(FileMetadata));
|
||||
if (m == NULL) {
|
||||
perror("ERROR: Could not allocate memory for file metadata");
|
||||
log_perror("ERROR: Could not allocate memory for file metadata");
|
||||
return NULL;
|
||||
}
|
||||
m->mode = stats->st_mode;
|
||||
@@ -96,7 +96,7 @@ bool file_load_data(File* file) {
|
||||
return true;
|
||||
file->data->data = malloc(file->data->size);
|
||||
if (file->data->data == NULL) {
|
||||
perror("Could not allocate memory for file data");
|
||||
log_perror("Could not allocate memory for file data");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -125,13 +125,13 @@ bool file_write_to_disk(const char* path, const void* data, unsigned long long d
|
||||
size_t file_content_to_buffer(File* file) {
|
||||
FILE* file_pointer = fopen(file->path, "rb");
|
||||
if (file_pointer == NULL) {
|
||||
perror("Could not open the file!");
|
||||
log_perror("Could not open the file!");
|
||||
return 0;
|
||||
}
|
||||
size_t bytes_read = fread(file->data->data, 1, file->data->size, file_pointer);
|
||||
if (bytes_read != (size_t)file->data->size) {
|
||||
fclose(file_pointer);
|
||||
perror("Read unexpected number of bytes from File!");
|
||||
log_perror("Read unexpected number of bytes from File!");
|
||||
return 0;
|
||||
}
|
||||
fclose(file_pointer);
|
||||
|
||||
@@ -62,7 +62,7 @@ bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int
|
||||
|
||||
int fd = open(file->path, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("Could not open file for sendfile");
|
||||
log_perror("Could not open file for sendfile");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int
|
||||
if (sent == -1) {
|
||||
if (errno == EAGAIN || errno == EINTR)
|
||||
continue;
|
||||
perror("sendfile failed");
|
||||
log_perror("sendfile failed");
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "log.h"
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
static const char* log_level_strings[] = {"DEBUG", "INFO", "WARN", "ERROR"};
|
||||
@@ -50,3 +52,7 @@ void log_message(LogLevel log_level, const char* format, ...) {
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
|
||||
void log_perror(const char* context) {
|
||||
log_message(LOG_LEVEL_ERROR, "%s: %s", context, strerror(errno));
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
typedef enum { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR } LogLevel;
|
||||
|
||||
void log_message(LogLevel log_level, const char* message, ...);
|
||||
void log_perror(const char* context);
|
||||
void set_log_level(LogLevel level);
|
||||
void log_set_file(FILE* fp);
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* que
|
||||
return context;
|
||||
|
||||
fail:
|
||||
perror("Error initializing synchronization objects");
|
||||
log_perror("Error initializing synchronization objects");
|
||||
if (init >= 6)
|
||||
cnd_destroy(&context->condition_not_empty_loader);
|
||||
if (init >= 5)
|
||||
@@ -116,7 +116,7 @@ PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue*
|
||||
return context;
|
||||
|
||||
fail:
|
||||
perror("Error initializing synchronization objects");
|
||||
log_perror("Error initializing synchronization objects");
|
||||
if (init >= 3)
|
||||
cnd_destroy(&context->condition_not_empty);
|
||||
if (init >= 2)
|
||||
|
||||
+4
-3
@@ -1,3 +1,4 @@
|
||||
#include "log.h"
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
@@ -13,7 +14,7 @@ Queue* queue_create(int capacity, void (*destroyer)(void* item)) {
|
||||
|
||||
Queue* queue = (Queue*)malloc(sizeof(Queue));
|
||||
if (queue == NULL) {
|
||||
perror("ERROR: Could not allocate memory for queue structure");
|
||||
log_perror("ERROR: Could not allocate memory for queue structure");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -72,7 +73,7 @@ static bool queue_double_capacity(Queue* queue) {
|
||||
new_capacity = 100;
|
||||
void** new_items = malloc(new_capacity * sizeof(void*));
|
||||
if (new_items == NULL) {
|
||||
perror("ERROR: Could not allocate memory for doubling capacity of queue.");
|
||||
log_perror("ERROR: Could not allocate memory for doubling capacity of queue.");
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < queue->size; i++)
|
||||
@@ -127,7 +128,7 @@ bool queue_enqueue_multithreaded_cancel(Queue* queue, void* item, mtx_t* mutex,
|
||||
|
||||
void* queue_dequeue(Queue* queue) {
|
||||
if (queue == NULL || queue_is_empty(queue)) {
|
||||
perror("ERROR: Could not dequeue from null or empty queue.");
|
||||
log_perror("ERROR: Could not dequeue from null or empty queue.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "log.h"
|
||||
#include "transport_ssh.h"
|
||||
#include "utils.h"
|
||||
#include <fcntl.h>
|
||||
@@ -76,7 +77,7 @@ Client* client_connect_ssh(const char* destination, int port, const char* server
|
||||
|
||||
int sv[2];
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
|
||||
perror("socketpair failed");
|
||||
log_perror("socketpair failed");
|
||||
remote_dest_destroy(&r);
|
||||
return NULL;
|
||||
}
|
||||
@@ -89,7 +90,7 @@ Client* client_connect_ssh(const char* destination, int port, const char* server
|
||||
|
||||
int exec_pipe[2];
|
||||
if (pipe(exec_pipe) < 0) {
|
||||
perror("pipe failed");
|
||||
log_perror("pipe failed");
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
remote_dest_destroy(&r);
|
||||
@@ -98,7 +99,7 @@ Client* client_connect_ssh(const char* destination, int port, const char* server
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork failed");
|
||||
log_perror("fork failed");
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
close(exec_pipe[0]);
|
||||
@@ -151,7 +152,7 @@ Client* client_connect_ssh(const char* destination, int port, const char* server
|
||||
ssh_argv[ac++] = "--stdio";
|
||||
ssh_argv[ac] = NULL;
|
||||
execvp("ssh", ssh_argv);
|
||||
perror("exec of ssh failed");
|
||||
log_perror("exec of ssh failed");
|
||||
ssize_t wret = write(exec_pipe[1], "x", 1);
|
||||
(void)wret;
|
||||
_exit(1);
|
||||
|
||||
@@ -30,20 +30,20 @@ static void sigchld_handler(int sig) {
|
||||
Server* server_create(int port) {
|
||||
Server* server = (Server*)malloc(sizeof(Server));
|
||||
if (server == NULL) {
|
||||
perror("Could not allocate space for Server");
|
||||
log_perror("Could not allocate space for Server");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (file_descriptor < 0) {
|
||||
perror("Could not create Socket!");
|
||||
log_perror("Could not create Socket!");
|
||||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
server->file_descriptor = file_descriptor;
|
||||
int opt = 1;
|
||||
if (setsockopt(server->file_descriptor, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
|
||||
perror("Error setting a socket option!");
|
||||
log_perror("Error setting a socket option!");
|
||||
close(server->file_descriptor);
|
||||
free(server);
|
||||
return NULL;
|
||||
@@ -59,7 +59,7 @@ Server* server_create(int port) {
|
||||
|
||||
if (bind(server->file_descriptor, (struct sockaddr*)&server->address, server->address_length) <
|
||||
0) {
|
||||
perror("Could not bind server");
|
||||
log_perror("Could not bind server");
|
||||
close(server->file_descriptor);
|
||||
free(server);
|
||||
return NULL;
|
||||
@@ -83,7 +83,7 @@ void server_delete(Server** server) {
|
||||
static void accept_loop(Server* server, void (*child_fn)(int, void*), void* child_ctx,
|
||||
const char* log_fmt) {
|
||||
if (listen(server->file_descriptor, SOMAXCONN) < 0) {
|
||||
perror("Could not listen on port!");
|
||||
log_perror("Could not listen on port!");
|
||||
return;
|
||||
}
|
||||
signal(SIGCHLD, sigchld_handler);
|
||||
@@ -92,7 +92,7 @@ static void accept_loop(Server* server, void (*child_fn)(int, void*), void* chil
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
int fd = accept(server->file_descriptor, (struct sockaddr*)&client_addr, &client_len);
|
||||
if (fd < 0) {
|
||||
perror("Could not accept the connection");
|
||||
log_perror("Could not accept the connection");
|
||||
continue;
|
||||
}
|
||||
tcp_apply_socket_timeout(fd);
|
||||
@@ -222,7 +222,7 @@ bool tcp_connect_socket(Client* client, char* host, int port) {
|
||||
freeaddrinfo(result);
|
||||
|
||||
if (!connected) {
|
||||
perror("Could not connect to Server!");
|
||||
log_perror("Could not connect to Server!");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
#include "array_list.h"
|
||||
#include "libgen.h"
|
||||
@@ -54,7 +55,7 @@ bool mkdir_r(const char* path) {
|
||||
struct stat st;
|
||||
if (stat(path_current, &st) != 0) {
|
||||
if (mkdir(path_current, 0755) != 0) {
|
||||
perror("Could not create directory");
|
||||
log_perror("Could not create directory");
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user