diff --git a/.gitignore b/.gitignore index 3dd39fe45e042ce69bdcd19e5ca9032371190c96..114dacb7eb0731820f94da6cf356fa734bed8e96 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,9 @@ spikes/logging/log_with_macro spikes/logging/log_file.txt spikes/make/read_from_file spikes/unit_testing/unit_tests -spikes/variadic_macros/variadic_macro +spikes/variadic_macros/log_file.txt +spikes/variadic_macros/variadic_macro_to_console +spikes/variadic_macros/variadic_macro_to_file # Ignore vscode files .vscode diff --git a/spikes/variadic_macros/makefile b/spikes/variadic_macros/makefile index 7274de3c89bf18aeb1652922b1b56dd8ad69637b..f0b07ac203493b7fdf424ba42f5e4732fb35336b 100644 --- a/spikes/variadic_macros/makefile +++ b/spikes/variadic_macros/makefile @@ -1,3 +1,9 @@ all: - gcc -Wall -Wpedantic -std=c99 -g *.c -o variadic_macro -lcunit + Please specify spike: [ to_console, to_file ] + +to_console: + gcc -Wall -Wpedantic -std=c99 -g variadic_macro_to_console.c -o variadic_macro_to_console + +to_file: + gcc -Wall -Wpedantic -std=c99 -g variadic_macro_to_file.c -o variadic_macro_to_file diff --git a/spikes/variadic_macros/variadic_macro.c b/spikes/variadic_macros/variadic_macro_to_console.c similarity index 81% rename from spikes/variadic_macros/variadic_macro.c rename to spikes/variadic_macros/variadic_macro_to_console.c index e4edac112ffedc1e73080145dc3616c22b8f9e15..28681b2725b3d81b1b437eff82ae4bbddcefa4e5 100644 --- a/spikes/variadic_macros/variadic_macro.c +++ b/spikes/variadic_macros/variadic_macro_to_console.c @@ -1,12 +1,12 @@ /** * CS 4900 - * Variadic Macro spike. + * Variadic Macro to Console spike. */ /** * Description: - * Spike to test creating macros with dynamic number of passed args. + * Spike to test creating macros with dynamic number of passed args. Prints to console */ @@ -14,8 +14,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "variadic_macro.h" - +#include "variadic_macro_to_console.h" /** diff --git a/spikes/variadic_macros/variadic_macro.h b/spikes/variadic_macros/variadic_macro_to_console.h similarity index 90% rename from spikes/variadic_macros/variadic_macro.h rename to spikes/variadic_macros/variadic_macro_to_console.h index d02d6c048cd5913c3df86b213b984019c30a6091..a81d7cb4e5110ec1bdbd316446885b109f0bddcc 100644 --- a/spikes/variadic_macros/variadic_macro.h +++ b/spikes/variadic_macros/variadic_macro_to_console.h @@ -1,12 +1,6 @@ /** * CS 4900 - * Header file for Variadic Macro spike. - */ - - -/** - * Description: - * Header file to hold variadic macro. + * Header file for Variadic Macro to Console spike. */ @@ -49,7 +43,7 @@ void va_printf_func(const char *source_file_name, va_list va_arglist; va_start(va_arglist, source_format_string); - // Format string with variable length values. + // Print to console. vprintf(format_string, va_arglist); // Clean up. diff --git a/spikes/variadic_macros/variadic_macro_to_file.c b/spikes/variadic_macros/variadic_macro_to_file.c new file mode 100644 index 0000000000000000000000000000000000000000..8d71c38addad40632a7c3b63490ec14d65949195 --- /dev/null +++ b/spikes/variadic_macros/variadic_macro_to_file.c @@ -0,0 +1,29 @@ +/** + * CS 4900 + * Variadic Macro to Log spike. + */ + + +/** + * Description: + * Spike to test creating macros with dynamic number of passed args. Saves to log file. + */ + + +// Import headers. +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "variadic_macro_to_file.h" + + +/** + * Program's main. + * Initializes and runs program. + */ +int main(int argc, char* argv[]) { + va_log("%s\n", "Hello world!"); + va_log("%s %s\n", "Hello", "world!"); + va_log("%s %d %d\n", "Nov", 20, 2018); + exit(0); +} diff --git a/spikes/variadic_macros/variadic_macro_to_file.h b/spikes/variadic_macros/variadic_macro_to_file.h new file mode 100644 index 0000000000000000000000000000000000000000..2f0e3ca8d6a6c92effb4d34374a6f737026f9636 --- /dev/null +++ b/spikes/variadic_macros/variadic_macro_to_file.h @@ -0,0 +1,67 @@ +/** + * CS 4900 + * Header file for Variadic Macro to Log spike. + */ + + +// Import headers. +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + + +// Function Prototypes. +void va_log_func (const char *source_file_name, + int source_line_num, + const char *source_format_string, + ...); + + +// Macro definition. +#define va_log(source_format_string, ...) \ + va_log_func (__FILE__, __LINE__, source_format_string, __VA_ARGS__) + + +/** + * Function called by va_printf macro. Used for printing output of unknown length args. + * Notes: + * * It seems to just segfault if you use a bad/wrong format string specifier. + * Unlike normal printf, it will not give detailed warnings/errors about such. + * * Due to how va_list and va_start work, calling macro needs at least one variable defined. + * This variable is where the __VA_ARGS__ call will start looking for new args to handle. + */ +void va_log_func(const char *source_file_name, + int source_line_num, + const char *source_format_string, + ...) { + + // Get time info. + time_t rawtime; + struct tm * timeinfo; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + + // Create initial formatted string, with default formatting. + char* format_string = calloc(1024, sizeof(char*)); + sprintf(format_string, "[%d-%d-%d %d:%d:%d] %s %d: %s", + timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, + timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, + source_file_name, source_line_num, source_format_string + ); + + // Handle variable length arg input. + va_list va_arglist; + va_start(va_arglist, source_format_string); + + // Save to log file. + FILE* log_file; + log_file = fopen("log_file.txt", "a+"); + vfprintf(log_file, format_string, va_arglist); + + // Clean up. + fclose(log_file); + va_end(va_arglist); + free(format_string); +}