aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2023-02-01 19:28:08 +0100
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2023-02-01 19:28:08 +0100
commit730c6fe66ce122e2ce49a57df0cece4504e8471f (patch)
treeb9deb52005f0ce88b46d041f4b32aee80cff834a
parente91664dfbd02bdbdf14c90e1fa1b30d85682e6b6 (diff)
downloadlog-730c6fe66ce122e2ce49a57df0cece4504e8471f.tar.gz
log-730c6fe66ce122e2ce49a57df0cece4504e8471f.zip
Add log.c and log.h
-rw-r--r--log.c134
-rw-r--r--log.h24
2 files changed, 158 insertions, 0 deletions
diff --git a/log.c b/log.c
new file mode 100644
index 0000000..68dfeb7
--- /dev/null
+++ b/log.c
@@ -0,0 +1,134 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "log.h"
+
+#define DBG_LVL 0
+#define INF_LVL 1
+#define WRN_LVL 2
+#define ERR_LVL 3
+
+#define BUFF_SIZE 256
+
+static int
+check_term_colors()
+{
+ FILE *fp;
+ char path[1024];
+
+ /* Open the command for reading. */
+ fp = popen("echo $COLORTERM", "r");
+ if (fp == NULL) {
+ printf("Failed to run command\n" );
+ exit(1);
+ }
+
+ /* Read the output a line at a time - output it. */
+ while (fgets(path, sizeof(path), fp) != NULL) {
+ /* printf("%s", path); */
+ if (strcmp(path, "truecolor\n") == 0) {
+ pclose(fp);
+ return 0;
+ }
+ }
+
+ /* close */
+ pclose(fp);
+
+ return -1;
+}
+
+static void
+log_init(Log *log)
+{
+ char buff[BUFF_SIZE];
+ FILE *file = fopen("log.config", "r");
+
+ /* Use the coded configuration */
+ if (file == NULL)
+ return;
+
+ while (fgets(buff, BUFF_SIZE, file) != NULL) {
+ /*printf("%s", strtok(buff, "="));*/
+ }
+
+ fclose(file);
+}
+
+Log *
+log_create(const char *filename) {
+ Log *log = (Log *) malloc(sizeof(Log));
+ /*log->filename = filename;
+ log->level = DBG_LVL;*/
+
+ log_init(log);
+
+ return log;
+}
+
+static void
+print_log(Log *log, int level, char *msg)
+{
+ time_t t;
+ t = time(NULL);
+ struct tm tm = *localtime(&t);
+ const char *lvl;
+
+ /* Don't display log if the choosen level is major.
+ * For example: log->level is ERROR and level is INFO, only ERROR logs will be shown. */
+ if (log->level > level) return;
+
+ switch (level) {
+ case DBG_LVL:
+ lvl = "[DEBUG]";
+ break;
+ case INF_LVL:
+ lvl = "[INFO]";
+ break;
+ case ERR_LVL:
+ lvl = "[ERROR]";
+ break;
+ default:
+ lvl = "[DEFAULT]";
+ break;
+ }
+
+ check_term_colors();
+ printf("%d-%02d-%02d %02d:%02d %10s: <%s> %s\n",
+ tm.tm_year + 1900,
+ tm.tm_mon,
+ tm.tm_mday,
+ tm.tm_hour,
+ tm.tm_min,
+ lvl,
+ log->filename,
+ msg);
+}
+
+void
+log_debug(Log *log, char *msg)
+{
+ print_log(log, DBG_LVL, msg);
+}
+
+void
+log_error(Log *log, char *msg)
+{
+ print_log(log, ERR_LVL, msg);
+}
+
+void
+log_info(Log *log, char *msg)
+{
+ print_log(log, INF_LVL, msg);
+}
+
+void
+log_warning(Log *log, char *msg)
+{
+ print_log(log, WRN_LVL, msg);
+}
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..7fdfa92
--- /dev/null
+++ b/log.h
@@ -0,0 +1,24 @@
+/* See LICENSE file for copyright and license details. */
+
+#ifndef __LOG_H__
+#define __LOG_H__
+
+typedef struct Log {
+ const char *filename;
+ int level;
+} Log;
+
+Log *log_create(const char *);
+
+void log_debug(Log *, char *);
+void log_error(Log *, char *);
+void log_info(Log *, char *);
+void log_warn(Log *, char *);
+
+int log_is_debug(Log *);
+int log_is_error(Log *);
+int log_is_info(Log *);
+int log_is_warn(Log *);
+
+
+#endif /* __LOG_H__ */