diff --git a/oct_log.c b/oct_log.c new file mode 100644 index 0000000..b9ad673 --- /dev/null +++ b/oct_log.c @@ -0,0 +1,26 @@ +#include +#include "oct_log.h" + +FILE* oct_log_output_file; +int oct_log_level; + +// If filename is null, default to stderr +int oct_log_init(char* filename, int level) { + if (filename) { + oct_log_output_file = fopen(filename, "w"); + } + else { + oct_log_output_file = stderr; + } + oct_log_level = level; + + OCT_LOG_INFO("Initialized logging, output file: %s", filename ? filename : "stderr"); + return 1; +} + +int oct_log_deinit() { + if (oct_log_output_file != stderr) { + fclose(oct_log_output_file); + } + return 1; +} diff --git a/oct_log.h b/oct_log.h new file mode 100644 index 0000000..31cb8dc --- /dev/null +++ b/oct_log.h @@ -0,0 +1,46 @@ +#ifndef OCT_LOG_H +#define OCT_LOG_H + +#include + +#define OCT_LOG_LEVEL_ERROR 0 +#define OCT_LOG_LEVEL_WARNING 1 +#define OCT_LOG_LEVEL_INFO 2 +#define OCT_LOG_LEVEL_DEBUG 3 + +extern FILE* oct_log_output_file; +extern int oct_log_level; + +#define OCT_LOG_ERROR(...) \ + if (oct_log_level >= OCT_LOG_LEVEL_ERROR) { \ + fprintf(oct_log_output_file, "[ERR]: "); \ + fprintf(oct_log_output_file, __VA_ARGS__); \ + fprintf(oct_log_output_file, "\n"); \ + } + +#define OCT_LOG_WARNING(...) \ + if (oct_log_level >= OCT_LOG_LEVEL_WARNING) { \ + fprintf(oct_log_output_file, "[WAR]: "); \ + fprintf(oct_log_output_file, __VA_ARGS__); \ + fprintf(oct_log_output_file, "\n"); \ + } + +#define OCT_LOG_INFO(...) \ + if (oct_log_level >= OCT_LOG_LEVEL_INFO) { \ + fprintf(oct_log_output_file, "[INF]: "); \ + fprintf(oct_log_output_file, __VA_ARGS__); \ + fprintf(oct_log_output_file, "\n"); \ + } + +#define OCT_LOG_DEBUG(...) \ + if (oct_log_level >= OCT_LOG_LEVEL_DEBUG) { \ + fprintf(oct_log_output_file, "[DBG]: "); \ + fprintf(oct_log_output_file, __VA_ARGS__); \ + fprintf(oct_log_output_file, "\n"); \ + } + +// If filename is null, default to stderr +int oct_log_init(char* filename, int level); +int oct_log_deinit(); + +#endif