new content
This commit is contained in:
parent
611202b84f
commit
deee29f5da
@ -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
|
||||
@ -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
|
||||
15
libmysyslog-json/libmysyslog-json.c
Normal file
15
libmysyslog-json/libmysyslog-json.c
Normal 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;
|
||||
}
|
||||
6
libmysyslog-json/libmysyslog-json.h
Normal file
6
libmysyslog-json/libmysyslog-json.h
Normal 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
|
||||
@ -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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user