mirror of
https://github.com/sigmasternchen/CFloor
synced 2025-03-15 04:18:55 +00:00
moved getTimestamp to util module
This commit is contained in:
parent
241b3975e1
commit
8ea65f4539
4 changed files with 50 additions and 46 deletions
|
@ -4,12 +4,11 @@
|
|||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "logging.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef BACKTRACE
|
||||
#include <execinfo.h>
|
||||
|
@ -89,50 +88,6 @@ void printBacktrace() {
|
|||
#endif
|
||||
}
|
||||
|
||||
char* getTimestamp() {
|
||||
#define MSEC_LENGTH (3)
|
||||
#define TIME_LENGTH (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + MSEC_LENGTH)
|
||||
char* result = malloc(TIME_LENGTH + 1);
|
||||
if (result == NULL) {
|
||||
fprintf(stderr, "\nDEVASTATING: Couldn't malloc for log time.\n");
|
||||
callCritical();
|
||||
exit(EXIT_DEVASTATING);
|
||||
}
|
||||
|
||||
|
||||
int millisec;
|
||||
struct tm* tm_info;
|
||||
struct timeval tv;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
millisec = (int) (tv.tv_usec / 1000.0);
|
||||
if (millisec >= 1000) {
|
||||
millisec -=1000;
|
||||
tv.tv_sec++;
|
||||
}
|
||||
|
||||
tm_info = localtime(&tv.tv_sec);
|
||||
|
||||
strftime(result, 26, "%Y-%m-%dT%H:%M:%S", tm_info);
|
||||
|
||||
char* msecBuffer = malloc(1 + MSEC_LENGTH + 1);
|
||||
|
||||
if (msecBuffer == NULL) {
|
||||
fprintf(stderr, "\nDEVASTATING: Couldn't malloc for log time.\n");
|
||||
callCritical();
|
||||
exit(EXIT_DEVASTATING);
|
||||
}
|
||||
|
||||
snprintf(msecBuffer, 1 + MSEC_LENGTH + 1, ".%03d", millisec);
|
||||
|
||||
strncat(result, msecBuffer, TIME_LENGTH + 1);
|
||||
|
||||
free(msecBuffer);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char* getLoglevelString(loglevel_t loglevel, bool color) {
|
||||
#define DEBUG_STRING "[DEBUG]"
|
||||
#define VERBOSE_STRING "[VERBOSE]"
|
||||
|
|
|
@ -25,6 +25,7 @@ typedef int loglevel_t;
|
|||
|
||||
void setLogging(FILE* file, loglevel_t loglevel, bool color);
|
||||
void setCriticalHandler(void (*handler)());
|
||||
void callCritical();
|
||||
|
||||
void printBacktrace();
|
||||
|
||||
|
|
46
src/util.c
46
src/util.c
|
@ -5,6 +5,8 @@
|
|||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
|
@ -196,3 +198,47 @@ int strlenOfNumber(long long number) {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
char* getTimestamp() {
|
||||
#define MSEC_LENGTH (3)
|
||||
#define TIME_LENGTH (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + MSEC_LENGTH)
|
||||
char* result = malloc(TIME_LENGTH + 1);
|
||||
if (result == NULL) {
|
||||
fprintf(stderr, "\nDEVASTATING: Couldn't malloc for log time.\n");
|
||||
callCritical();
|
||||
exit(EXIT_DEVASTATING);
|
||||
}
|
||||
|
||||
|
||||
int millisec;
|
||||
struct tm* tm_info;
|
||||
struct timeval tv;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
millisec = (int) (tv.tv_usec / 1000.0);
|
||||
if (millisec >= 1000) {
|
||||
millisec -=1000;
|
||||
tv.tv_sec++;
|
||||
}
|
||||
|
||||
tm_info = localtime(&tv.tv_sec);
|
||||
|
||||
strftime(result, 26, "%Y-%m-%dT%H:%M:%S", tm_info);
|
||||
|
||||
char* msecBuffer = malloc(1 + MSEC_LENGTH + 1);
|
||||
|
||||
if (msecBuffer == NULL) {
|
||||
fprintf(stderr, "\nDEVASTATING: Couldn't malloc for log time.\n");
|
||||
callCritical();
|
||||
exit(EXIT_DEVASTATING);
|
||||
}
|
||||
|
||||
snprintf(msecBuffer, 1 + MSEC_LENGTH + 1, ".%03d", millisec);
|
||||
|
||||
strncat(result, msecBuffer, TIME_LENGTH + 1);
|
||||
|
||||
free(msecBuffer);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -22,4 +22,6 @@ char* strclone(const char* string);
|
|||
|
||||
int strlenOfNumber(long long number);
|
||||
|
||||
char* getTimestamp();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue