add -text

This commit is contained in:
gandc 2024-11-23 19:40:46 +03:00
parent 0b820f1e6e
commit 5b1dbe5db0
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-text.so
LIBRARY = libmysyslog-text.a
OBJ = src/log_text.o
all: $(TARGET)
all: $(LIBRARY)
$(TARGET): libmysyslog-text.o
$(CC) $(LDFLAGS) -o $@ $^
$(LIBRARY): $(OBJ)
ar rcs $@ $^
libmysyslog-text.o: libmysyslog-text.c libmysyslog-text.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_TEXT_H
#define LOG_TEXT_H
#include "mysyslog.h"
void log_text(const char* msg, LogLevel level, const char* path);
#endif // LOG_TEXT_H

View File

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

View File

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

View File

@ -1,15 +0,0 @@
#include <stdio.h>
#include <time.h>
#include "log_text.h"
void log_text(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
fprintf(f, "%ld %d example-app %s\n", t, level, msg);
fclose(f);
}