new content

This commit is contained in:
gandc 2024-11-23 19:48:49 +03:00
parent 611202b84f
commit deee29f5da
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4
5 changed files with 31 additions and 39 deletions

View File

@ -1,23 +1,17 @@
CC = gcc
CFLAGS = -Wall -g -fPIC
LDFLAGS =
CFLAGS = -Wall -Wextra -fPIC
LDFLAGS = -shared
TARGET = libmysyslog-json.so
LIBRARY = libmysyslog-json.a
OBJ = src/log_json.o
all: $(TARGET)
all: $(LIBRARY)
$(TARGET): libmysyslog-json.o
$(CC) $(LDFLAGS) -o $@ $^
$(LIBRARY): $(OBJ)
ar rcs $@ $^
libmysyslog-json.o: libmysyslog-json.c libmysyslog-json.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,8 +0,0 @@
#ifndef LOG_JSON_H
#define LOG_JSON_H
#include "mysyslog.h"
void log_json(const char* msg, LogLevel level, const char* path);
#endif // LOG_JSON_H

View File

@ -0,0 +1,15 @@
#include "libmysyslog-json.h"
#include <stdio.h>
#include <time.h>
static const char* log_levels[] = {"DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"};
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;
}

View File

@ -0,0 +1,6 @@
#ifndef LIBMYSYSLOG_JSON_H
#define LIBMYSYSLOG_JSON_H
int log_to_json(const char* msg, int level, const char* path);
#endif // LIBMYSYSLOG_JSON_H

View File

@ -1,15 +0,0 @@
#include <stdio.h>
#include <time.h>
#include "log_json.h"
void log_json(const char* msg, LogLevel level, const char* path) {
FILE* f = fopen(path, "a");
if (!f) return;
time_t t = time(NULL);
struct tm* tm_info = localtime(&t);
// Format log entry in JSON
fprintf(f, "{\"timestamp\": %ld, \"log_level\": \"%d\", \"process\": \"example-app\", \"message\": \"%s\"}\n", t, level, msg);
fclose(f);
}