Kasianov Nikolai Alekseevich
2 years ago
commit
41660b4883
7 changed files with 254 additions and 0 deletions
@ -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. |
@ -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)
|
@ -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 |
@ -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"); |
||||
} |
||||
} |
@ -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 <stdio.h> |
||||
#include <stdarg.h> |
||||
|
||||
// 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 |
@ -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"); |
||||
} |
Loading…
Reference in new issue