new content

This commit is contained in:
gandc 2024-11-23 20:07:07 +03:00
parent deee29f5da
commit 7fc9618d17
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4
5 changed files with 68 additions and 32 deletions

View File

@ -1,23 +1,17 @@
CC = gcc
CFLAGS = -Wall -g -fPIC
LDFLAGS =
CFLAGS = -Wall -Wextra -fPIC
LDFLAGS = -shared
TARGET = libmysyslog.so
LIBRARY = libmysyslog.a
OBJ = src/mysyslog.o
all: $(TARGET)
all: $(LIBRARY)
$(TARGET): libmysyslog.o
$(CC) $(LDFLAGS) -o $@ $^
$(LIBRARY): $(OBJ)
ar rcs $@ $^
libmysyslog.o: libmysyslog.c libmysyslog.h
$(CC) $(CFLAGS) -c $<
clean:
rm -f $(OBJ) $(LIBRARY)
rm -f *.o $(TARGET)
deb:
# Deb package building logic
install: $(LIBRARY)
cp $(LIBRARY) /usr/local/lib
ldconfig
.PHONY: all clean deb install
.PHONY: all clean

View File

@ -1,12 +0,0 @@
Source: libmysyslog
Section: libs
Priority: optional
Maintainer: GandC Snow admin@gandc.ru
Build-Depends: debhelper (>= 9)
Standards-Version: 3.9.6
Homepage: https://git.gandc.ru/mysyslog/libmysyslog
Package: libmysyslog
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: Core library for the MySyslog logging system

View File

@ -1,4 +0,0 @@
#!/usr/bin/make -f
%:
dh $@

39
libmysyslog/libmysyslog.c Normal file
View File

@ -0,0 +1,39 @@
#include "libmysyslog.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
static const char* log_levels[] = {"DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"};
int log_to_text(const char* msg, int level, const char* path);
int log_to_json(const char* msg, int level, const char* path);
int mysyslog(const char* msg, int level, int driver, int format, const char* path) {
if (driver == TEXT_DRIVER) {
return log_to_text(msg, level, path);
} else if (driver == JSON_DRIVER) {
return log_to_json(msg, level, path);
} else {
return -1; // Unsupported driver
}
}
int log_to_text(const char* msg, int level, const char* path) {
FILE* file = fopen(path, "a");
if (!file) return -1;
time_t now = time(NULL);
fprintf(file, "%ld %s %s\n", now, log_levels[level], msg);
fclose(file);
return 0;
}
int log_to_json(const char* msg, int level, const char* path) {
FILE* file = fopen(path, "a");
if (!file) return -1;
time_t now = time(NULL);
fprintf(file, "{\"timestamp\":%ld,\"log_level\":\"%s\",\"message\":\"%s\"}\n", now, log_levels[level], msg);
fclose(file);
return 0;
}

19
libmysyslog/libmysyslog.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef LIBMYSYSLOG_H
#define LIBMYSYSLOG_H
enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
CRITICAL
};
enum LogDriver {
TEXT_DRIVER,
JSON_DRIVER
};
int mysyslog(const char* msg, int level, int driver, int format, const char* path);
#endif // LIBMYSYSLOG_H