add libmysyslog sources

This commit is contained in:
gandc 2025-03-15 23:47:56 +03:00
parent 21f5c0f955
commit 4bb5c2a119
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4
2 changed files with 58 additions and 0 deletions

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