Browse Source

Log with custom prefix function added

master
parent
commit
2421318550
  1. 1
      README.md
  2. 13
      src/slog.c
  3. 3
      src/slog.h
  4. 2
      testing/test.c

1
README.md

@ -17,6 +17,7 @@ int main() {
log_info("Some information: %d %s", 123, "fr"); log_info("Some information: %d %s", 123, "fr");
log_error("%s", "error message"); log_error("%s", "error message");
log_debug("Debug won't be printed because the flag was not set"); log_debug("Debug won't be printed because the flag was not set");
log_with_prefix("[Custom prefix] ", "5+(4*90)==%d", 5+(4*90));
// Creates file with fopen's mode and sets log output to it // Creates file with fopen's mode and sets log output to it
create_log_file("logs.log", "w"); create_log_file("logs.log", "w");

13
src/slog.c

@ -52,6 +52,19 @@ void close_log_file() {
} }
} }
// Log a message with custom prefix. Counts as INFO log message
void log_with_prefix(const char* prefix, const char* message, ...) {
if (LOG_OUTPUT && ((LOGGING_LEVEL & INFO) == INFO) && prefix && message) {
va_list arg;
fprintf(LOG_OUTPUT, "%s", prefix);
va_start(arg, message);
vfprintf(LOG_OUTPUT, message, arg);
va_end(arg);
fprintf(LOG_OUTPUT, "\n");
}
}
// Log information // Log information
void log_info(const char* message, ...) { void log_info(const char* message, ...) {
if (LOG_OUTPUT && ((LOGGING_LEVEL & INFO) == INFO) && message) { if (LOG_OUTPUT && ((LOGGING_LEVEL & INFO) == INFO) && message) {

3
src/slog.h

@ -40,6 +40,9 @@ int create_log_file(const char* path, const char* mode);
// Does nothing if log output is std(out|error|input) // Does nothing if log output is std(out|error|input)
void close_log_file(); void close_log_file();
// Log a message with custom prefix. Counts as INFO log message
void log_with_prefix(const char* prefix, const char* message, ...);
// Log information // Log information
void log_info(const char* message, ...); void log_info(const char* message, ...);

2
testing/test.c

@ -13,4 +13,6 @@ int main() {
close_log_file(); close_log_file();
log_warning("And to %s again", "stdout"); log_warning("And to %s again", "stdout");
log_with_prefix("[Custom prefix] ", "5+(4*90)==%d", 5+(4*90));
} }
Loading…
Cancel
Save