test: libhectic
This commit is contained in:
34
package/c/libhectic/default.nix
Normal file
34
package/c/libhectic/default.nix
Normal file
@@ -0,0 +1,34 @@
|
||||
{ stdenv, gcc, lib }:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
pname = "libhectic";
|
||||
version = "1.0";
|
||||
src = ./.;
|
||||
doCheck = true;
|
||||
|
||||
buildPhase = ''
|
||||
mkdir -p target
|
||||
${gcc}/bin/cc -Wall -Wextra -g -pedantic -fsanitize=address -c libhectic.c -o target/libhectic.o
|
||||
${gcc}/bin/ar rcs target/libhectic.a target/libhectic.o
|
||||
'';
|
||||
|
||||
checkPhase = ''
|
||||
mkdir -p target/test
|
||||
for test_file in test/*.c; do
|
||||
exe="target/test/$(basename ''${test_file%.c})"
|
||||
${gcc}/bin/cc -Wall -Wextra -g -pedantic -fsanitize=address -I. "$test_file" -Ltarget -l:libhectic.a -o "$exe"
|
||||
"$exe"
|
||||
done
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/lib $out/include
|
||||
cp target/libhectic.a $out/lib/
|
||||
cp libhectic.h $out/include/
|
||||
'';
|
||||
|
||||
meta = {
|
||||
description = "libhectic";
|
||||
license = lib.licenses.mit;
|
||||
};
|
||||
}
|
||||
76
package/c/libhectic/libhectic.c
Normal file
76
package/c/libhectic/libhectic.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "libhectic.h"
|
||||
|
||||
void set_output_color_mode(ColorMode mode) {
|
||||
color_mode = mode;
|
||||
}
|
||||
|
||||
// ------------
|
||||
// -- Logger --
|
||||
// ------------
|
||||
|
||||
const char* log_level_to_string(LogLevel level) {
|
||||
switch (level) {
|
||||
case LOG_LEVEL_DEBUG: return "DEBUG";
|
||||
case LOG_LEVEL_LOG: return "LOG";
|
||||
case LOG_LEVEL_INFO: return "INFO";
|
||||
case LOG_LEVEL_NOTICE: return "NOTICE";
|
||||
case LOG_LEVEL_WARN: return "WARN";
|
||||
case LOG_LEVEL_EXCEPTION: return "EXCEPTION";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
LogLevel log_level_from_string(const char *level_str) {
|
||||
if (!level_str) return LOG_LEVEL_INFO;
|
||||
if (strcmp(level_str, "DEBUG") == 0)
|
||||
return LOG_LEVEL_DEBUG;
|
||||
else if (strcmp(level_str, "LOG") == 0)
|
||||
return LOG_LEVEL_LOG;
|
||||
else if (strcmp(level_str, "INFO") == 0)
|
||||
return LOG_LEVEL_INFO;
|
||||
else if (strcmp(level_str, "NOTICE") == 0)
|
||||
return LOG_LEVEL_NOTICE;
|
||||
else if (strcmp(level_str, "WARN") == 0)
|
||||
return LOG_LEVEL_WARN;
|
||||
else if (strcmp(level_str, "EXCEPTION") == 0)
|
||||
return LOG_LEVEL_EXCEPTION;
|
||||
else
|
||||
return LOG_LEVEL_INFO;
|
||||
}
|
||||
|
||||
LogLevel current_log_level = LOG_LEVEL_INFO;
|
||||
|
||||
void logger_level_reset() {
|
||||
current_log_level = LOG_LEVEL_INFO;
|
||||
}
|
||||
|
||||
void logger_level(LogLevel level) {
|
||||
current_log_level = level;
|
||||
}
|
||||
|
||||
void init_logger(void) {
|
||||
current_log_level = log_level_from_string(getenv("LOG_LEVEL"));
|
||||
}
|
||||
|
||||
char* log_message(LogLevel level, int line, const char *format, ...) {
|
||||
if (level < current_log_level) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm tm_info;
|
||||
localtime_r(&now, &tm_info);
|
||||
static char timeStr[20];
|
||||
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info);
|
||||
|
||||
fprintf(stderr, "%s %d %s: ", timeStr, line, log_level_to_string(level));
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
return timeStr;
|
||||
}
|
||||
138
package/c/libhectic/libhectic.h
Normal file
138
package/c/libhectic/libhectic.h
Normal file
@@ -0,0 +1,138 @@
|
||||
#ifndef EPRINTF_H
|
||||
#define EPRINTF_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
// -------------
|
||||
// -- Helpers --
|
||||
// -------------
|
||||
|
||||
// Helper macros for argument counting
|
||||
// NOTE(yukkop): this ugly macroses for avoid all posible warnings
|
||||
#define PP_CAT(a, b) PP_CAT_I(a, b)
|
||||
#define PP_CAT_I(a, b) a##b
|
||||
|
||||
#define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
|
||||
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
|
||||
#define PP_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N
|
||||
#define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0
|
||||
|
||||
// ------------
|
||||
// -- Colors --
|
||||
// ------------
|
||||
|
||||
// Color mode enumeration
|
||||
typedef enum {
|
||||
COLOR_MODE_AUTO,
|
||||
COLOR_MODE_FORCE,
|
||||
COLOR_MODE_DISABLE
|
||||
} ColorMode;
|
||||
|
||||
// Static color mode variable
|
||||
static ColorMode color_mode = COLOR_MODE_AUTO;
|
||||
|
||||
// Function to set color mode
|
||||
void set_output_color_mode(ColorMode mode);
|
||||
|
||||
// Macros for detecting terminal and color usage
|
||||
#define IS_TERMINAL() (isatty(fileno(stderr)))
|
||||
#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL()))
|
||||
|
||||
#define COLOR_RED (USE_COLOR() ? "\033[1;31m" : "")
|
||||
#define COLOR_RESET (USE_COLOR() ? "\033[0m" : "")
|
||||
|
||||
// ------------
|
||||
// -- Errors --
|
||||
// ------------
|
||||
|
||||
// Define color macros based on output type
|
||||
//#define ERROR_PREFIX PP_CAT_I(COLOR_RED, "Error: ")
|
||||
//#define ERROR_SUFFIX PP_CAT_I(COLOR_RESET, "\n")
|
||||
#define ERROR_PREFIX (USE_COLOR() ? "\033[1;31mError: " : "Error: ")
|
||||
#define ERROR_SUFFIX (USE_COLOR() ? "\033[0m\n" : "\n")
|
||||
|
||||
// eprintf handling 1 or more arguments
|
||||
#define eprintf_1(fmt) \
|
||||
fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, ERROR_SUFFIX)
|
||||
|
||||
#define eprintf_2(fmt, ...) \
|
||||
fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, __VA_ARGS__, ERROR_SUFFIX)
|
||||
|
||||
#define eprintf(...) \
|
||||
PP_CAT(eprintf_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
#define todo fprintf(stderr, "%sNot implimented yet%s", COLOR_RED, COLOR_RESET);exit(1)
|
||||
|
||||
// ------------
|
||||
// -- Logger --
|
||||
// ------------
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_DEBUG,
|
||||
LOG_LEVEL_LOG,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_NOTICE,
|
||||
LOG_LEVEL_WARN,
|
||||
LOG_LEVEL_EXCEPTION
|
||||
} LogLevel;
|
||||
|
||||
void logger_level(LogLevel level);
|
||||
|
||||
LogLevel log_level_from_string(const char *level_str);
|
||||
|
||||
char* log_message(LogLevel level, int line, const char *format, ...);
|
||||
|
||||
// DEBUG level
|
||||
#define raise_debug_1(fmt) \
|
||||
log_message(LOG_LEVEL_DEBUG, __LINE__, fmt)
|
||||
#define raise_debug_2(fmt, ...) \
|
||||
log_message(LOG_LEVEL_DEBUG, __LINE__, fmt, __VA_ARGS__)
|
||||
#define raise_debug(...) \
|
||||
PP_CAT(raise_debug_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
// LOG level
|
||||
#define raise_log_1(fmt) \
|
||||
log_message(LOG_LEVEL_LOG, __LINE__, fmt)
|
||||
#define raise_log_2(fmt, ...) \
|
||||
log_message(LOG_LEVEL_LOG, __LINE__, fmt, __VA_ARGS__)
|
||||
#define raise_log(...) \
|
||||
PP_CAT(raise_log_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
// INFO level
|
||||
#define raise_info_1(fmt) \
|
||||
log_message(LOG_LEVEL_INFO, __LINE__, fmt)
|
||||
#define raise_info_2(fmt, ...) \
|
||||
log_message(LOG_LEVEL_INFO, __LINE__, fmt, __VA_ARGS__)
|
||||
#define raise_info(...) \
|
||||
PP_CAT(raise_info_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
// NOTICE level
|
||||
#define raise_notice_1(fmt) \
|
||||
log_message(LOG_LEVEL_NOTICE, __LINE__, fmt)
|
||||
#define raise_notice_2(fmt, ...) \
|
||||
log_message(LOG_LEVEL_NOTICE, __LINE__, fmt, __VA_ARGS__)
|
||||
#define raise_notice(...) \
|
||||
PP_CAT(raise_notice_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
// WARN level
|
||||
#define raise_warn_1(fmt) \
|
||||
log_message(LOG_LEVEL_WARN, __LINE__, fmt)
|
||||
#define raise_warn_2(fmt, ...) \
|
||||
log_message(LOG_LEVEL_WARN, __LINE__, fmt, __VA_ARGS__)
|
||||
#define raise_warn(...) \
|
||||
PP_CAT(raise_warn_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
// EXCEPTION level
|
||||
#define raise_exception_1(fmt) \
|
||||
log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt)
|
||||
#define raise_exception_2(fmt, ...) \
|
||||
log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt, __VA_ARGS__)
|
||||
#define raise_exception(...) \
|
||||
PP_CAT(raise_exception_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
#endif // EPRINTF_H
|
||||
49
package/c/libhectic/test/test.c
Normal file
49
package/c/libhectic/test/test.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "libhectic.h"
|
||||
|
||||
// Reusable test function for any raise_* logging function.
|
||||
// The logging function must have the signature: char* func(const char* message)
|
||||
void test_raise_generic(const char* (*raise_func)(const char*), LogLevel log_level, const char* level_str) {
|
||||
FILE *orig_stderr = stderr;
|
||||
FILE *temp = tmpfile();
|
||||
char result_buffer[256];
|
||||
|
||||
if (!temp) {
|
||||
perror("tmpfile");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
stderr = temp;
|
||||
|
||||
logger_level(log_level);
|
||||
int line = __LINE__;
|
||||
char* time_str = raise_func("message");
|
||||
logger_level_reset();
|
||||
|
||||
fflush(stderr);
|
||||
fseek(temp, 0, SEEK_SET);
|
||||
size_t nread = fread(result_buffer, 1, sizeof(result_buffer) - 1, temp);
|
||||
result_buffer[nread] = '\0';
|
||||
|
||||
stderr = orig_stderr;
|
||||
fclose(temp);
|
||||
|
||||
char expected_buffer[256];
|
||||
sprintf(expected_buffer, "%s %d %s: message\n", time_str, line + 1, level_str, message);
|
||||
assert(strcmp(result_buffer, expected_buffer) == 0);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
set_output_color_mode(COLOR_MODE_DISABLE);
|
||||
|
||||
test_raise_generic(raise_log, LOG_LEVEL_LOG, "LOG");
|
||||
test_raise_generic(raise_debug, LOG_LEVEL_DEBUG, "DEBUG");
|
||||
test_raise_generic(raise_warn, LOG_LEVEL_INFO, "INFO");
|
||||
test_raise_generic(raise_notice, LOG_LEVEL_NOTICE, "NOTICE");
|
||||
test_raise_generic(raise_warn, LOG_LEVEL_WARN, "WARN");
|
||||
test_raise_generic(raise_exception, LOG_LEVEL_EXCEPTION, "EXCEPTION");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user