1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /*
- * 2D Physic Engine
- * logger.cpp: The logger
- * Based on pikuma.com Learn Game Physics Engine Programming course.
- * Copyright (c) 2021-2022 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 11/02/2021.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdarg.h>
- #include <time.h>
- #include <logger.h>
- typedef struct logData
- {
- char name[7];
- uint8_t colour;
- } logData;
- static logData dataTable[] =
- {
- [LOG_LOG] = { "LOG", 32 },
- [LOG_ERROR] = { "ERROR", 31 },
- [LOG_WARNING] = { "WARN", 33 },
- [LOG_INFO] = { "INFO", 36 },
- [LOG_CRITICAL] = { "CRITIC", 101 },
- [LOG_DEBUG] = { "DEBUG", 35 },
- };
- static void addLog(LogType type, const char *message, va_list params)
- {
- time_t curtime = time(NULL);
- char buf[30];
- struct tm *p = localtime(&curtime);
- int typeColour = dataTable[type].colour;
- const char *typeName = dataTable[type].name;
- strftime(buf, 38, "%d-%b-%Y %H:%M:%S", p);
- fprintf(stderr, "\x1B[%d;1m%-6s | %-20s - ", typeColour, typeName, buf);
- vfprintf(stderr, message, params);
- fprintf(stderr, "\x1B[0m\n");
- }
- void logger::log(const char *message, ...)
- {
- va_list list;
- va_start(list, message);
- addLog(LOG_LOG, message, list);
- va_end(list);
- }
- void logger::error(const char *message, ...)
- {
- va_list list;
- va_start(list, message);
- addLog(LOG_ERROR, message, list);
- va_end(list);
- }
- void logger::warning(const char *message, ...)
- {
- va_list list;
- va_start(list, message);
- addLog(LOG_WARNING, message, list);
- va_end(list);
- }
- void logger::info(const char *message, ...)
- {
- va_list list;
- va_start(list, message);
- addLog(LOG_INFO, message, list);
- va_end(list);
- }
- void logger::critical(const char *message, ...)
- {
- va_list list;
- va_start(list, message);
- addLog(LOG_CRITICAL, message, list);
- va_end(list);
- }
- void logger::debug(const char *message, ...)
- {
- va_list list;
- va_start(list, message);
- addLog(LOG_DEBUG, message, list);
- va_end(list);
- }
|