Browse Source

basic logging implementation

master
Arwed Mett 4 years ago
parent
commit
3b3aa86769
Signed by: Pfeifenjoy GPG Key ID: 86943827297DA9FC
  1. 46
      CMakeLists.txt
  2. 22
      include/logging/logging.h
  3. 135
      src/logging.c

46
CMakeLists.txt

@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.5)
project(logging VERSION 0.0.1 LANGUAGES C)
add_library(logging src/logging.c)
target_include_directories(logging PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
install(TARGETS logging EXPORT LoggingTargets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
install(DIRECTORY include/logging DESTINATION include)
install(EXPORT LoggingTargets
FILE LoggingTargets.cmake
NAMESPACE Logging::
DESTINATION lib/cmake/logging
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(LoggingConfigVersion.cmake
COMPATIBILITY SameMajorVersion
)
install(FILES build/LoggingConfigVersion.cmake
DESTINATION lib/cmake/logging
)
#CPack
set(CPACK_PACKAGE_CONTACT "arwed.mett@googlemail.com")
if(APPLE)
set(CPACK_GENERATOR "TGZ;ZIP;productbuild")
elseif(UNIX)
set(CPACK_GENERATOR "TGZ;ZIP;RPM;DEB")
elseif(WIN32)
set(CPACK_GENERATOR "ZIP;NSIS;NSIS64")
else()
set(CPACK_GENERATOR "TGZ;ZIP")
endif()
set(CPACK_BUNDLE_NAME ${PROJECT_NAME})
include(CPack)

22
include/logging/logging.h

@ -0,0 +1,22 @@
#ifndef LOGGING_H_LSUOR4FY
#define LOGGING_H_LSUOR4FY
#include "stdarg.h"
typedef unsigned char debug_level;
void vinfo(const char *, va_list);
void info(const char *, ...);
void vwarn(const char *, va_list);
void warn(const char *, ...);
void verror(const char *, va_list);
void error(const char *, ...);
void vdebug(const debug_level, const char *, va_list);
void debug(const debug_level, const char *, ...);
void set_debug_level(debug_level);
#endif /* end of include guard: LOGGING_H_LSUOR4FY */

135
src/logging.c

@ -0,0 +1,135 @@
#include "logging/logging.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "string.h"
#include "assert.h"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define RESET "\x1b[0m"
#define MAX_LABEL_WIDTH 8
debug_level current_debug_level = 0;
char *scolor(FILE *out, const char *color, const char *text) {
char *result;
size_t length;
//check if the programs output is a file
if(isatty(fileno(out))) {
length = strlen(color) + strlen(text) + strlen(RESET) + 1;
result = malloc(length * sizeof(char));
snprintf(result, length, "%s%s%s", color, text, RESET);
} else {
length = strlen(text) + 1;
result = malloc(length * sizeof(char));
snprintf(result, length, "%s", text);
}
result[length - 1] = 0;
return result;
}
char *printr(char c, size_t n) {
char *result = malloc((n + 1) * sizeof(char));
for(size_t i = 0; i < n; ++i) {
result[i] = c;
}
result[n] = 0;
return result;
}
char *make_label(FILE *out, const char *color, const char *name) {
assert(strlen(name) <= MAX_LABEL_WIDTH);
char *colored_name = scolor(out, color, name);
char *space = printr(' ', MAX_LABEL_WIDTH - strlen(name));
size_t length = MAX_LABEL_WIDTH + strlen("[") + strlen("] ") + 1;
char *result = (char *) malloc(length * sizeof(char));
snprintf(result, length, "[%s]%s ", colored_name, space);
result[length - 1] = 0;
free(colored_name);
free(space);
return result;
}
void vlog(
FILE *out,
const char *label_text,
const char *color,
const char *text,
va_list args
) {
//label
char *label = make_label(out, color, label_text);
//create format
size_t format_length = strlen(label) + strlen(text) + 2;
char *format = (char *) malloc(format_length * sizeof(char));
snprintf(format, format_length, "%s%s\n", label, text);
format[format_length - 1] = 0;
//print
vfprintf(out, format, args);
free(label);
free(format);
}
void vinfo(const char *text, va_list args) {
vlog(stdout, "INFO", BLUE, text, args);
}
void info(const char *text, ...) {
va_list args;
va_start(args, text);
vinfo(text, args);
va_end(args);
}
void vwarn(const char *text, va_list args) {
vlog(stderr, "WARN", YELLOW, text, args);
}
void warn(const char *text, ...) {
va_list args;
va_start(args, text);
vwarn(text, args);
va_end(args);
}
void verror(const char *formatting, va_list args) {
vlog(stderr, "ERROR", RED, formatting, args);
}
void error(const char *text, ...) {
va_list args;
va_start(args, text);
verror(text, args);
va_end(args);
}
void vdebug(const debug_level level, const char *text, va_list args) {
if(level <= current_debug_level) {
vlog(stdout, "DEBUG", GREEN, text, args);
}
}
void debug(const debug_level level, const char *text, ...) {
va_list args;
va_start(args, text);
vdebug(level, text, args);
va_end(args);
}
void set_debug_level(debug_level level) {
current_debug_level = level;
}
Loading…
Cancel
Save