Merge pull request #2 from vdm-dev/master

SLog update
This commit is contained in:
phaneron 2024-02-20 16:28:19 -05:00 committed by GitHub
commit 93e4cfc300
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 75 additions and 43 deletions

View file

@ -1,16 +0,0 @@
#ifndef STORM_COMMON_HPP
#define STORM_COMMON_HPP
#define DECLARE_STRICT_HANDLE(name) \
typedef struct name##__ { \
int unused; \
}* name
#define DECLARE_DERIVED_HANDLE(name, base) \
typedef struct name##__ : public base##__ { \
int unused; \
}* name
#endif

View file

@ -7,4 +7,18 @@
typedef struct name##__* name typedef struct name##__* name
#endif #endif
#if !defined(DECLARE_STRICT_HANDLE)
#define DECLARE_STRICT_HANDLE(name) \
typedef struct name##__ { \
int unused; \
}* name
#endif
#if !defined(DECLARE_DERIVED_HANDLE)
#define DECLARE_DERIVED_HANDLE(name, base) \
typedef struct name##__ : public base##__ { \
int unused; \
}* name
#endif
#endif #endif

View file

@ -9,12 +9,14 @@
#if defined(WHOA_SYSTEM_LINUX) #if defined(WHOA_SYSTEM_LINUX)
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <linux/limits.h> #include <linux/limits.h>
#endif #endif
#if defined(WHOA_SYSTEM_MAC) #if defined(WHOA_SYSTEM_MAC)
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <mach/mach_time.h> #include <mach/mach_time.h>
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
@ -27,6 +29,22 @@
#define STORM_LOG_FLUSH_POINT 0xC000 #define STORM_LOG_FLUSH_POINT 0xC000
#if defined(WHOA_SYSTEM_WIN)
typedef SYSTEMTIME SLOGTIME;
#else
typedef struct _SLOGTIME {
uint16_t wYear;
uint16_t wMonth;
uint16_t wDayOfWeek;
uint16_t wDay;
uint16_t wHour;
uint16_t wMinute;
uint16_t wSecond;
uint16_t wMilliseconds;
} SLOGTIME;
#endif
typedef struct _LOG { typedef struct _LOG {
HSLOG log; HSLOG log;
_LOG* next; _LOG* next;
@ -41,10 +59,6 @@ typedef struct _LOG {
} LOG; } LOG;
static uint64_t lasttime = 0;
static size_t timestrlen = 0;
static char timestr[64] = { '\0' };
static SCritSect* s_critsect[STORM_LOG_MAX_CHANNELS] = { nullptr }; static SCritSect* s_critsect[STORM_LOG_MAX_CHANNELS] = { nullptr };
static SCritSect* s_defaultdir_critsect = nullptr; static SCritSect* s_defaultdir_critsect = nullptr;
@ -306,7 +320,7 @@ static bool OpenLogFile(const char* filename, FILE** file, uint32_t flags) {
char newfilename[STORM_MAX_PATH]; char newfilename[STORM_MAX_PATH];
PrependDefaultDir(newfilename, STORM_MAX_PATH, filename); PrependDefaultDir(newfilename, STORM_MAX_PATH, filename);
CreateFileDirectory(newfilename); CreateFileDirectory(newfilename);
*file = fopen(newfilename, (flags & SLOG_FLAG_APPEND) ? "a" : "w"); *file = fopen(newfilename, (flags & SLOG_FLAG_APPEND) ? "ab" : "wb");
return (*file != nullptr); return (*file != nullptr);
} }
return false; return false;
@ -325,15 +339,18 @@ static void OutputIndent(LOG* logptr) {
} }
static void OutputReturn(LOG* logptr) { static void OutputReturn(LOG* logptr) {
// strcpy(&logptr->buffer[logptr->bufferused], "\r\n"); #if defined(WHOA_SYSTEM_WIN)
// logptr->bufferused += 2; logptr->buffer[logptr->bufferused++] = '\r';
#endif
// No need to write "\r\n" with fopen() in text mode logptr->buffer[logptr->bufferused++] = '\n';
logptr->buffer[logptr->bufferused] = '\n'; logptr->buffer[logptr->bufferused] = '\0';
logptr->bufferused++;
} }
static void OutputTime(LOG* logptr, bool show) { static void OutputTime(LOG* logptr, bool show) {
static uint64_t lasttime = 0;
static size_t timestrlen = 0;
static char timestr[64] = { '\0' };
if (logptr->timeStamp == 0) { if (logptr->timeStamp == 0) {
return; return;
} }
@ -358,18 +375,35 @@ static void OutputTime(LOG* logptr, bool show) {
if (ticks != lasttime) { if (ticks != lasttime) {
lasttime = ticks; lasttime = ticks;
SLOGTIME systime;
#if defined(WHOA_SYSTEM_WIN)
GetLocalTime(&systime);
#else
time_t t = time(nullptr); time_t t = time(nullptr);
struct tm* ts = localtime(&t); struct tm* ts = localtime(&t);
// No milliseconds for now systime.wYear = ts->tm_year + 1900;
systime.wMonth = ts->tm_mon + 1;
systime.wDayOfWeek = ts->tm_wday;
systime.wDay = ts->tm_mday;
systime.wHour = ts->tm_hour;
systime.wMinute = ts->tm_min;
systime.wSecond = ts->tm_sec;
struct timeval tv = { 0 };
gettimeofday(&tv, 0);
systime.wMilliseconds = tv.tv_usec / 1000;
#endif
timestrlen = SStrPrintf(timestr, timestrlen = SStrPrintf(timestr,
sizeof(timestr), sizeof(timestr),
"%d/%d %02d:%02d:%02d.%03d ", "%u/%u %02u:%02u:%02u.%03u ",
ts->tm_mon + 1, systime.wMonth,
ts->tm_mday, systime.wDay,
ts->tm_hour, systime.wHour,
ts->tm_min, systime.wMinute,
ts->tm_sec, systime.wSecond,
0); systime.wMilliseconds);
} }
if (show) { if (show) {
@ -590,6 +624,8 @@ void SLogVWrite(HSLOG log, const char* format, va_list arglist) {
#if defined(WHOA_SYSTEM_WIN) #if defined(WHOA_SYSTEM_WIN)
// if (g_opt.echotooutputdebugstring) // if (g_opt.echotooutputdebugstring)
OutputDebugString(&logptr->buffer[logptr->pendpoint]); OutputDebugString(&logptr->buffer[logptr->pendpoint]);
#else
fputs(&logptr->buffer[logptr->pendpoint], stderr);
#endif #endif
logptr->pendpoint = logptr->bufferused; logptr->pendpoint = logptr->bufferused;

View file

@ -5,16 +5,14 @@
#include <cstdarg> #include <cstdarg>
#include <cstdint> #include <cstdint>
#include "storm/Common.hpp" #include "storm/Handle.hpp"
#include "storm/String.hpp" #include "storm/String.hpp"
enum : uint32_t { #define SLOG_FLAG_DEFAULT 0 // Create or open log file with first SLogWrite() call
SLOG_FLAG_DEFAULT = 0, // Create or open log file with first SLogWrite() call #define SLOG_FLAG_OPEN_FILE 1 // Create or open log file with SLogCreate()
SLOG_FLAG_OPEN_FILE = 1, // Create or open log file with SLogCreate() #define SLOG_FLAG_NO_FILE 2 // Don't use log file (use OutputDebugString or console only)
SLOG_FLAG_NO_FILE = 2, // Don't use log file (use OutputDebugString or console only) #define SLOG_FLAG_APPEND 4 // Don't truncate existing log file
SLOG_FLAG_APPEND = 4 // Don't truncate existing log file
};
DECLARE_STRICT_HANDLE(HSLOG); DECLARE_STRICT_HANDLE(HSLOG);
DECLARE_STRICT_HANDLE(HLOCKEDLOG); DECLARE_STRICT_HANDLE(HLOCKEDLOG);