aboutsummaryrefslogtreecommitdiff
path: root/src/logger.c
diff options
context:
space:
mode:
authorpk33 <pk33@pk33.space>2024-11-01 23:51:15 +0100
committerpk33 <pk33@pk33.space>2024-11-01 23:51:15 +0100
commit294b585994da4427ac98a8353ff41aed5f301d54 (patch)
treeaafcd885c1fe28819ef1a511da5e6344411b5597 /src/logger.c
parentf0958551b43959174d28b2056f584d2081494e6c (diff)
downloadengine33-294b585994da4427ac98a8353ff41aed5f301d54.tar.gz
Import and fix old code
Diffstat (limited to 'src/logger.c')
-rw-r--r--src/logger.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/logger.c b/src/logger.c
new file mode 100644
index 0000000..c944da8
--- /dev/null
+++ b/src/logger.c
@@ -0,0 +1,77 @@
+#include <linux/vt.h>
+
+#include "fcntl.h"
+#include "stdarg.h"
+#include "errno.h"
+#include "time.h"
+#include "unistd.h"
+#include "stdio.h"
+#include "string.h"
+
+#include "logger.h"
+
+Logger __logger;
+
+
+static char *colours[5] = {
+ "[\x1b[36mDebug\x1b[37m]",
+ "[\x1b[32mInfo\x1b[37m]",
+ "[\x1b[35mWarn\x1b[37m]",
+ "[\x1b[33mError\x1b[37m]",
+ "[\x1b[31mFatal\x1b[37m]",
+};
+
+Size logger_init( void )
+{
+ String logfile[512];
+
+
+ snprintf(logfile, 512, "%s/%s", getenv("HOME"), ".engine33.log");
+
+ __logger.fd = open(logfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
+
+ if( __logger.fd < 0 ) {
+ printf( "Unable to initialize logger. Exiting." );
+ return E33_EXIT_FAILURE;
+ }
+
+ __logger.func = 0;
+
+ dprintf( __logger.fd, "\033[3J\033[1;1HWelcome to Engine33!\r\n\r\n" );
+
+ return E33_EXIT_SUCCESS;
+}
+
+void logger_term( void )
+{
+ dprintf( __logger.fd, "\r\n\r\nGoodbye!" );
+ close( __logger.fd );
+}
+
+void logger( Size level, const char *fmt, ... )
+{
+ time_t currentTime;
+ struct tm *m_time;
+
+
+ va_list args;
+ va_start(args, fmt);
+
+ time(&currentTime);
+ m_time = localtime(&currentTime);
+
+ dprintf( __logger.fd, "Engine33 [%02d:%02d:%02d] -", m_time->tm_hour, m_time->tm_min, m_time->tm_sec );
+
+ dprintf( __logger.fd, " %s ", colours[level] );
+ vdprintf(__logger.fd, fmt, args);
+
+ if( (level > LOG_INFO) && errno ) {
+ dprintf( __logger.fd, ". Reason: %s", strerror(errno) );
+ errno = 0;
+ }
+
+ dprintf( __logger.fd, ".\r\n" );
+
+ va_end(args);
+}
+