Timestamps on logs and stuff.

This commit is contained in:
Linus_Probert 2018-01-24 09:35:21 +01:00
parent 6853f860f6
commit eea667f5f1
3 changed files with 41 additions and 8 deletions

View File

@ -190,20 +190,25 @@ gui_render_panel(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
void void
gui_log(const char *fmt, ...) gui_log(const char *fmt, ...)
{ {
char buffer[log_data.strlen];
char *new_message; char *new_message;
unsigned int i; unsigned int i;
char tstamp[10];
new_message = ec_malloc(log_data.strlen * sizeof(char));
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
#ifndef _MSC_VER #ifndef _MSC_VER
vsprintf(new_message, fmt, args); vsprintf(buffer, fmt, args);
#else // _MSC_VER #else // _MSC_VER
vsprintf_s(new_message, log_data.strlen, fmt, args); vsprintf_s(buffer, log_data.strlen, fmt, args);
#endif // _MSC_VER #endif // _MSC_VER
va_end(args); va_end(args);
new_message = ec_malloc(log_data.strlen * sizeof(char));
timestamp(tstamp, 10);
m_sprintf(new_message, log_data.strlen, "%s > %s", tstamp, buffer);
log_data.count++; log_data.count++;
if (log_data.count > log_data.len) { if (log_data.count > log_data.len) {
log_data.count = log_data.len; log_data.count = log_data.len;

View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <time.h>
#include "defines.h" #include "defines.h"
@ -56,7 +57,10 @@ void
debug(const char *fmt, ...) debug(const char *fmt, ...)
{ {
va_list args; va_list args;
printf("[--] "); char tstamp[10];
timestamp(tstamp, 10);
printf("[%s][--] ", tstamp);
va_start(args, fmt); va_start(args, fmt);
vprintf(fmt, args); vprintf(fmt, args);
va_end(args); va_end(args);
@ -67,7 +71,10 @@ void
info(const char * fmt, ...) info(const char * fmt, ...)
{ {
va_list args; va_list args;
printf("[**] "); char tstamp[10];
timestamp(tstamp, 10);
printf("[%s][**] ", tstamp);
va_start(args, fmt); va_start(args, fmt);
vprintf(fmt, args); vprintf(fmt, args);
va_end(args); va_end(args);
@ -78,7 +85,10 @@ void
error(const char *fmt, ...) error(const char *fmt, ...)
{ {
va_list args; va_list args;
fprintf(stderr, "[!*] Error "); char tstamp[10];
timestamp(tstamp, 10);
fprintf(stderr, "[%s][!*] ", tstamp);
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
@ -89,7 +99,10 @@ void
fatal(const char *fmt, ...) fatal(const char *fmt, ...)
{ {
va_list args; va_list args;
fprintf(stderr, "[!!] Fatal Error "); char tstamp[10];
timestamp(tstamp, 10);
fprintf(stderr, "[%s][!!] ", tstamp);
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
@ -106,3 +119,15 @@ void
fatal("in ec_malloc() on memory allocation"); fatal("in ec_malloc() on memory allocation");
return ptr; return ptr;
} }
void
timestamp(char *tstamp, size_t sz)
{
time_t cTime;
struct tm *tm_info;
time(&cTime);
tm_info = localtime(&cTime);
strftime(tstamp, sz, "%H:%M:%S", tm_info);
}

View File

@ -25,4 +25,7 @@ m_strncat(char *dest, size_t destsz, char *src, size_t srcsz);
void void
m_sprintf(char *dest, size_t destsz, const char *format, ...); m_sprintf(char *dest, size_t destsz, const char *format, ...);
void
timestamp(char *tstamp, size_t sz);
#endif // UTIL_H_ #endif // UTIL_H_