From 41660b48832693115cf84366a84209a74efbb3fb Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Fri, 25 Nov 2022 09:31:51 +0300 Subject: [PATCH] Initial commit --- .gitignore | 5 +++ LICENSE | 8 ++++ Makefile | 27 +++++++++++++ README.md | 38 ++++++++++++++++++ src/slog.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ src/slog.h | 55 ++++++++++++++++++++++++++ testing/test.c | 16 ++++++++ 7 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/slog.c create mode 100644 src/slog.h create mode 100644 testing/test.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a83d269 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.vscode/ +test +logs.log +bin/ +build/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..83924cd --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..300feed --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +CC:=gcc +CFLAGSLIB:=-O2 -Wall -Werror -c +CFLAGSTEST:=-O2 -Wall -Werror -static +SRC:=src/slog.c +TESTDIR:=testing +TEST:=$(TESTDIR)/test.c +BUILDDIR:=build +BINDIR:=bin +LIBBINNAME:=slog.a +TESTBINNAME:=test +RELEASEDIR:=release + +lib: + $(CC) $(CFLAGSLIB) $(SRC) && \ + mkdir -p $(BUILDDIR) && \ + mv *.o $(BUILDDIR) && \ + mkdir -p $(BINDIR) && \ + ar rcs $(BINDIR)/$(LIBBINNAME) $(BUILDDIR)/*.o + + +test: lib + $(CC) $(CFLAGSTEST) $(SRC) $(TEST) -o $(TESTDIR)/$(TESTBINNAME) && \ + cd $(TESTDIR) && \ + ./$(TESTBINNAME) + +clean: + rm -rf $(TESTBINNAME) $(RELEASEDIR) $(BINDIR) $(BUILDDIR) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..397a516 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Slog - dead simple logging library for C/C++ +(Pun интендед) + +## Usage + +``` +#include "logger.h" + +int main() { + set_log_output(stdout); + set_log_level(INFO|ERROR|WARNING); + + log_info("Some information: %d %s", 123, "fr"); + log_error("%s", "error message"); + log_debug("Debug won't be printed because the flag was not set"); + + // Creates file with fopen's mode and sets log output to it + create_log_file("logs.log", "w"); + log_info("Writing to a log file now"); + + // Closes previously opened log file and sets log output to stdout. + // Does nothing if log output is std(out|error|input) + close_log_file(); + + log_warning("And to %s again", "stdout"); +} +``` + +## Build + +- `make` - if you have `gcc` and `make` itself + +or + +- take out the header and an implementation file and compile the other way that is suitable in your environment + +## License +MIT \ No newline at end of file diff --git a/src/slog.c b/src/slog.c new file mode 100644 index 0000000..fc02eaa --- /dev/null +++ b/src/slog.c @@ -0,0 +1,105 @@ +/* +The MIT License (MIT) +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "slog.h" + +// Current logging level +int LOGGING_LEVEL = INFO|WARNING|ERROR|DEBUG; + +// Current logging output +FILE* LOG_OUTPUT; + +// Sets logging level to a specified one +void set_log_level(int level) { + LOGGING_LEVEL = level; +} + +// Output messages to output instead +void set_log_output(FILE* output) { + if (output) { + LOG_OUTPUT = output; + } else { + LOG_OUTPUT = stdout; + } +} + +// Creates file with fopen's mode and sets log output to it. Returns 0 +// in case of success, non 0 value otherwise +int create_log_file(const char* path, const char* mode) { + FILE* log_file; + + log_file = fopen(path, mode); + if (!log_file) { + return 1; + } + + set_log_output(log_file); + + return 0; +} + +// Closes previously opened log file and sets log output to stdout. +// Does nothing if log output is std(out|error|input) +void close_log_file() { + if (LOG_OUTPUT && !(LOG_OUTPUT == stdout || LOG_OUTPUT == stderr || LOG_OUTPUT == stdin)) { + fclose(LOG_OUTPUT); + set_log_output(stdout); + } +} + +// Log information +void log_info(const char* message, ...) { + if (LOG_OUTPUT && ((LOGGING_LEVEL & INFO) == INFO) && message) { + va_list arg; + + fprintf(LOG_OUTPUT, "[INFO] "); + va_start(arg, message); + vfprintf(LOG_OUTPUT, message, arg); + va_end(arg); + fprintf(LOG_OUTPUT, "\n"); + } +} + +// Log warning +void log_warning(const char* message, ...) { + if (LOG_OUTPUT && ((LOGGING_LEVEL & WARNING) == WARNING) && message) { + va_list arg; + + fprintf(LOG_OUTPUT, "[WARNING] "); + va_start(arg, message); + vfprintf(LOG_OUTPUT, message, arg); + va_end(arg); + fprintf(LOG_OUTPUT, "\n"); + } +} + +// Log error +void log_error(const char* message, ...) { + if (LOG_OUTPUT && ((LOGGING_LEVEL & ERROR) == ERROR) && message) { + va_list arg; + + fprintf(LOG_OUTPUT, "[ERROR] "); + va_start(arg, message); + vfprintf(LOG_OUTPUT, message, arg); + va_end(arg); + fprintf(LOG_OUTPUT, "\n"); + } +} + +// Debug log +void log_debug(const char* message, ...) { + if (LOG_OUTPUT && ((LOGGING_LEVEL & DEBUG) == DEBUG) && message) { + va_list arg; + + fprintf(LOG_OUTPUT, "[DEBUG] "); + va_start(arg, message); + vfprintf(LOG_OUTPUT, message, arg); + va_end(arg); + fprintf(LOG_OUTPUT, "\n"); + } +} \ No newline at end of file diff --git a/src/slog.h b/src/slog.h new file mode 100644 index 0000000..f45ad5c --- /dev/null +++ b/src/slog.h @@ -0,0 +1,55 @@ +/* +The MIT License (MIT) +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef LOGGER_HEADER +#define LOGGER_HEADER + +#include +#include + +// Used to specify which messages to log and which to not +enum LogLevel { + INFO = 1, + WARNING = 1 << 1, + ERROR = 1 << 2, + DEBUG = 1 << 3, +}; + +// Current logging level +extern int LOGGING_LEVEL; + +// Current logging output +extern FILE* LOG_OUTPUT; + +// Sets logging level to a specified one +void set_log_level(int level); + +// Output messages to output +void set_log_output(FILE* output); + +// Creates file with fopen's mode and sets log output to it. Returns 0 +// in case of success, non 0 value otherwise +int create_log_file(const char* path, const char* mode); + +// Closes previously opened log file and sets log output to stdout. +// Does nothing if log output is std(out|error|input) +void close_log_file(); + +// Log information +void log_info(const char* message, ...); + +// Log warning +void log_warning(const char* message, ...); + +// Log error +void log_error(const char* message, ...); + +// Debug log +void log_debug(const char* message, ...); + +#endif \ No newline at end of file diff --git a/testing/test.c b/testing/test.c new file mode 100644 index 0000000..fe5bb64 --- /dev/null +++ b/testing/test.c @@ -0,0 +1,16 @@ +#include "../src/slog.h" + +int main() { + set_log_output(stdout); + set_log_level(INFO|ERROR|WARNING); + + log_info("Some information: %d %s", 123, "fr"); + log_error("%s", "error message"); + log_debug("Debug won't be printed because the flag was not set"); + + create_log_file("logs.log", "w"); + log_info("Writing to a log file now"); + close_log_file(); + + log_warning("And to %s again", "stdout"); +} \ No newline at end of file