Last active
February 25, 2023 17:04
-
-
Save BoQsc/09e0290979071b5efed9201d32c21b85 to your computer and use it in GitHub Desktop.
Output library: A wrapper around printf of C Standard Library for logging and clarity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* A wrapper library for printf function.*/ | |
/* At the core: vfprintf instead of printf is used to allow streams and variadic arguments */ | |
/* Print is also extended into helper functions error() warning() notice() info() */ | |
#include <stdio.h> | |
#include <stdarg.h> | |
// gcc -Wall -Wextra -Q print.c && a | |
// Stream: Error, custom, warning, notice, log, file | |
// Prototypes | |
// print(format, variables) | |
// print(stream, format, variables) | |
// print(error, "%s", var1,var2) | |
// print("format") | |
// print("text") | |
// print("text") | |
// print_error() | |
// print_warning() | |
// print_notice() | |
// log() //logarithm? not great. | |
// log - prints to both stdout stream and a file. | |
// check if first argument is a stream, if is, then select second argument as format. | |
void implementation_print(FILE * stream, const char* format, va_list variadic_arguments){ | |
vfprintf(stream, format, variadic_arguments); | |
} | |
void print(const char* format, ...){ | |
va_list variadic_arguments; | |
va_start(variadic_arguments, format); | |
implementation_print(stdout, format, variadic_arguments); | |
va_end(variadic_arguments); | |
} | |
void print_error(const char* format, ...){ | |
va_list variadic_arguments; | |
va_start(variadic_arguments, format); | |
implementation_print(stderr, format, variadic_arguments); | |
va_end(variadic_arguments); | |
} | |
void print_notice(const char* format, ...){ | |
va_list variadic_arguments; | |
va_start(variadic_arguments, format); | |
implementation_print(stdout, format, variadic_arguments); | |
va_end(variadic_arguments); | |
} | |
void print_warning(const char* format, ...){ | |
va_list variadic_arguments; | |
va_start(variadic_arguments, format); | |
implementation_print(stdout, format, variadic_arguments); | |
va_end(variadic_arguments); | |
} | |
void implementation_log_this(const char* file, const char* format, va_list variadic_arguments){ | |
FILE *file_pointer = fopen(file, "a"); | |
implementation_print(file_pointer, format, variadic_arguments); | |
fclose(file_pointer); | |
} | |
void log_this(const char* file, const char* format, ...){ | |
va_list variadic_arguments; | |
va_start(variadic_arguments, format); | |
implementation_log_this(file, format, variadic_arguments); | |
va_end(variadic_arguments); | |
} | |
void print_and_log(const char* file, const char* format, ...){ | |
va_list variadic_arguments; | |
va_start(variadic_arguments, format); | |
implementation_print(stdout, format, variadic_arguments); | |
implementation_log_this(file, format, variadic_arguments); | |
va_end(variadic_arguments); | |
} | |
// log already reserved by C language library | |
// log log_error log_info | |
// ECHO | |
// print_and_log() | |
int main(){ | |
char * test = "Outhere"; | |
print("Hellows %s\n", test); | |
printf("Hellows %s\n", test); | |
fprintf(stdout,"Hellows %s\n", test); | |
print_notice("notice %s\n", test); | |
print_error("error %s\n", test); | |
print_warning("warning %s\n", test); | |
print_and_log("file.txt", "print and output: %s\n", test); | |
log_this(".//file.txt", "only_log output2: %s\n", test); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment