diff --git a/storm/Common.hpp b/storm/Common.hpp deleted file mode 100644 index bffb7d5..0000000 --- a/storm/Common.hpp +++ /dev/null @@ -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 diff --git a/storm/Handle.hpp b/storm/Handle.hpp index b4264d2..462dc70 100644 --- a/storm/Handle.hpp +++ b/storm/Handle.hpp @@ -7,4 +7,18 @@ typedef struct name##__* name #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 diff --git a/storm/Log.cpp b/storm/Log.cpp index 8b259b4..1d8cc9f 100644 --- a/storm/Log.cpp +++ b/storm/Log.cpp @@ -9,12 +9,14 @@ #if defined(WHOA_SYSTEM_LINUX) #include +#include #include #include #endif #if defined(WHOA_SYSTEM_MAC) #include +#include #include #include #include @@ -27,6 +29,22 @@ #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 { HSLOG log; _LOG* next; @@ -41,10 +59,6 @@ typedef struct _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_defaultdir_critsect = nullptr; @@ -306,7 +320,7 @@ static bool OpenLogFile(const char* filename, FILE** file, uint32_t flags) { char newfilename[STORM_MAX_PATH]; PrependDefaultDir(newfilename, STORM_MAX_PATH, filename); CreateFileDirectory(newfilename); - *file = fopen(newfilename, (flags & SLOG_FLAG_APPEND) ? "a" : "w"); + *file = fopen(newfilename, (flags & SLOG_FLAG_APPEND) ? "ab" : "wb"); return (*file != nullptr); } return false; @@ -325,15 +339,18 @@ static void OutputIndent(LOG* logptr) { } static void OutputReturn(LOG* logptr) { - // strcpy(&logptr->buffer[logptr->bufferused], "\r\n"); - // logptr->bufferused += 2; - - // No need to write "\r\n" with fopen() in text mode - logptr->buffer[logptr->bufferused] = '\n'; - logptr->bufferused++; +#if defined(WHOA_SYSTEM_WIN) + logptr->buffer[logptr->bufferused++] = '\r'; +#endif + logptr->buffer[logptr->bufferused++] = '\n'; + logptr->buffer[logptr->bufferused] = '\0'; } 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) { return; } @@ -358,18 +375,35 @@ static void OutputTime(LOG* logptr, bool show) { if (ticks != lasttime) { lasttime = ticks; + SLOGTIME systime; + +#if defined(WHOA_SYSTEM_WIN) + GetLocalTime(&systime); +#else time_t t = time(nullptr); 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, - sizeof(timestr), - "%d/%d %02d:%02d:%02d.%03d ", - ts->tm_mon + 1, - ts->tm_mday, - ts->tm_hour, - ts->tm_min, - ts->tm_sec, - 0); + sizeof(timestr), + "%u/%u %02u:%02u:%02u.%03u ", + systime.wMonth, + systime.wDay, + systime.wHour, + systime.wMinute, + systime.wSecond, + systime.wMilliseconds); } if (show) { @@ -590,6 +624,8 @@ void SLogVWrite(HSLOG log, const char* format, va_list arglist) { #if defined(WHOA_SYSTEM_WIN) // if (g_opt.echotooutputdebugstring) OutputDebugString(&logptr->buffer[logptr->pendpoint]); +#else + fputs(&logptr->buffer[logptr->pendpoint], stderr); #endif logptr->pendpoint = logptr->bufferused; diff --git a/storm/Log.hpp b/storm/Log.hpp index 0935d0c..9537eab 100644 --- a/storm/Log.hpp +++ b/storm/Log.hpp @@ -5,16 +5,14 @@ #include #include -#include "storm/Common.hpp" +#include "storm/Handle.hpp" #include "storm/String.hpp" -enum : uint32_t { - SLOG_FLAG_DEFAULT = 0, // Create or open log file with first SLogWrite() call - SLOG_FLAG_OPEN_FILE = 1, // Create or open log file with SLogCreate() - SLOG_FLAG_NO_FILE = 2, // Don't use log file (use OutputDebugString or console only) - SLOG_FLAG_APPEND = 4 // Don't truncate existing log file -}; +#define 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() +#define 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 DECLARE_STRICT_HANDLE(HSLOG); DECLARE_STRICT_HANDLE(HLOCKEDLOG);