diff --git a/documents/references.md b/documents/references.md index 8c8d4adb1d6e42623d91a2cbc3fa409536b26f50..45127340e64b83ea6815007103408e2e3fce761d 100644 --- a/documents/references.md +++ b/documents/references.md @@ -41,6 +41,15 @@ Most parallelization logic is from the book "Introduction to Parallel Programmin <https://stackoverflow.com/a/35190285> <https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html> +#### Logging +* Create Dir - <https://stackoverflow.com/a/7430262> +* Write to File - <https://www.programiz.com/c-programming/examples/write-file> +* Getting File/LineNum: +** <https://stackoverflow.com/a/42135375> +** <https://stackoverflow.com/a/8884408> +* Function Macros - <https://gcc.gnu.org/onlinedocs/cpp/Function-like-Macros.html#Function-like-Macros> + + ### Makefiles #### Passing Command Line Args <https://stackoverflow.com/a/47008498> diff --git a/src/main.c b/src/main.c index 558457013c77a1884c302df32b351b80459d18c4..3adfe93b2bee53b1e5d8c58b255e7ce04ccce7ec 100644 --- a/src/main.c +++ b/src/main.c @@ -179,7 +179,7 @@ void run_program() { total_processors, seconds_per_load, total_loads, process_rank ); - arr_run(thread_args_ptr); + // arr_run(thread_args_ptr); free_thread_struct(thread_args_ptr); diff --git a/src/structs.c b/src/structs.c index 88244988f4e504ad12add7d69a8a3f09e7680666..1b9e26eb016f241e57a3de345b9f3b567839f27f 100644 --- a/src/structs.c +++ b/src/structs.c @@ -10,6 +10,23 @@ #endif +// Global Variables. +thread_struct* thread_args_ptr; + + +/** + * Writes a single line to log file for processor. + */ +void _log(char const* file, long line, char const* message) { + + // Print to console (debugging only). + printf(" P%3i [%s:%ld] %s\n", thread_args_ptr->thread_num, file, line, message); + + // Log to file. + fprintf(thread_args_ptr->log_file, "[%s:%ld] %s\n", file, line, message); +} + + /** * Initializes a "thread_struct" object. */ @@ -22,22 +39,50 @@ thread_struct* initialize_thread_struct( ) { // Create struct. - thread_struct* new_struct = calloc(1, sizeof(thread_struct)); + thread_args_ptr = calloc(1, sizeof(thread_struct)); // Populate fields. - new_struct->total_processors = total_processors; - new_struct->seconds_per_load = seconds_per_load; - new_struct->total_loads = total_loads; - new_struct->thread_num = thread_num; - new_struct->remaining_loads = 0; + thread_args_ptr->total_processors = total_processors; + thread_args_ptr->seconds_per_load = seconds_per_load; + thread_args_ptr->total_loads = total_loads; + thread_args_ptr->thread_num = thread_num; + thread_args_ptr->remaining_loads = 0; + + // Create logging dir if doesn't exist. + struct stat st = {0}; + if (stat("logging/", &st) == -1) { + mkdir("logging/", 0700); + } - return new_struct; + // Initialize logging location. + char* log_location = calloc(256, sizeof(char)); + sprintf(log_location, "logging/thread_%i.log", thread_num); + FILE* file_ptr = fopen(log_location, "w"); + + // Check for errors. + if (file_ptr == NULL) { + printf("Error opening logging location at \"%s\".\n", log_location); + free(log_location); + free(thread_args_ptr); + exit(1); + } else { + thread_args_ptr->log_file_location = log_location; + thread_args_ptr->log_file = file_ptr; + } + + log(""); + log("Initializing arg_struct."); + return thread_args_ptr; } /** * Destroyes passed "thread_struct" object and all associated fields. */ -void free_thread_struct(thread_struct* a_struct) { - free(a_struct); +void free_thread_struct(thread_struct* thread_args_ptr) { + log("Destroying arg_struct."); + + fclose(thread_args_ptr->log_file); + free(thread_args_ptr->log_file_location); + free(thread_args_ptr); } diff --git a/src/structs.h b/src/structs.h index 0d71d006d4abd214160058ad4cb951a8bc0eb2ba..408626da0aedefb69bdfc1bb2f2e647ce0ce2085 100644 --- a/src/structs.h +++ b/src/structs.h @@ -9,6 +9,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> +#include <sys/stat.h> /** @@ -24,6 +26,9 @@ typedef struct { // "Local" thread values. int thread_num; int remaining_loads; + char* log_file_location; + FILE* log_file; + } thread_struct; @@ -31,3 +36,6 @@ typedef struct { // Function Prototypes. thread_struct* initialize_thread_struct(); void free_thread_struct(); +void _log(); + +#define log(message) _log(__FILE__, __LINE__, (message))