Skip to content

Commit 25aebee

Browse files
committed
Added log file metadata header, fixed timestamps, fixed exception handler on Windows, refactoring
1 parent e7b0be6 commit 25aebee

File tree

14 files changed

+134
-97
lines changed

14 files changed

+134
-97
lines changed

Sources/Jazz2/LevelHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ namespace Jazz2
401401

402402
_levelDisplayName = std::move(descriptor.DisplayName);
403403

404-
LOGI("Level \"{}\" \"{}.j2l\" loaded", _levelDisplayName, _levelName);
404+
LOGI("Level \"{}\" (from \"{}.j2l\") loaded", _levelDisplayName, _levelName);
405405

406406
if (!_levelDisplayName.empty()) {
407407
theApplication().GetGfxDevice().setWindowTitle(String(NCINE_APP_NAME " - " + _levelDisplayName));

Sources/Jazz2/Multiplayer/MpLevelHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ namespace Jazz2::Multiplayer
23452345
}
23462346
SendMessage(peer, UI::MessageLevel::Confirm, { infoBuffer, length });
23472347

2348-
auto uptimeSecs = (DateTime::Now().ToUnixMilliseconds() / 1000) - (std::int64_t)serverConfig.StartUnixTimestamp;
2348+
auto uptimeSecs = (DateTime::UtcNow().ToUnixMilliseconds() / 1000) - (std::int64_t)serverConfig.StartUnixTimestamp;
23492349
auto hours = (std::int32_t)(uptimeSecs / 3600);
23502350
auto minutes = (std::int32_t)(uptimeSecs % 3600) / 60;
23512351
auto seconds = (std::int32_t)(uptimeSecs % 60);

Sources/Jazz2/Multiplayer/NetworkManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace Jazz2::Multiplayer
6363
std::memcpy(&_serverConfig->UniqueServerID[0], &PreferencesCache::UniqueServerID[0], arraySize(_serverConfig->UniqueServerID) - sizeof(std::uint16_t));
6464
std::memcpy(&_serverConfig->UniqueServerID[_serverConfig->UniqueServerID.size() - sizeof(std::uint16_t)], &serverPort, sizeof(std::uint16_t));
6565

66-
_serverConfig->StartUnixTimestamp = DateTime::Now().ToUnixMilliseconds() / 1000;
66+
_serverConfig->StartUnixTimestamp = DateTime::UtcNow().ToUnixMilliseconds() / 1000;
6767
bool result = NetworkManagerBase::CreateServer(handler, serverPort);
6868

6969
if (result && !_serverConfig->IsPrivate) {

Sources/Jazz2/PreferencesCache.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -599,45 +599,6 @@ namespace Jazz2
599599
return fs::GetDirectoryName(_configPath);
600600
}
601601

602-
template<class Iterator>
603-
static std::string ToBase64Url(const Iterator begin, const Iterator end)
604-
{
605-
static const StaticArray<64, char> chars {
606-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
607-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
608-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
609-
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
610-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
611-
};
612-
613-
std::string result;
614-
std::size_t c = 0;
615-
StaticArray<3, std::uint8_t> charArray;
616-
617-
for (auto i = begin; i != end; ++i) {
618-
charArray[c++] = static_cast<std::uint8_t>(*i);
619-
if (c == 3) {
620-
result += chars[static_cast<std::uint8_t>((charArray[0] & 0xFC) >> 2)];
621-
result += chars[static_cast<std::uint8_t>(((charArray[0] & 0x03) << 4) + ((charArray[1] & 0xF0) >> 4))];
622-
result += chars[static_cast<std::uint8_t>(((charArray[1] & 0x0F) << 2) + ((charArray[2] & 0xC0) >> 6))];
623-
result += chars[static_cast<std::uint8_t>(charArray[2] & 0x3f)];
624-
c = 0;
625-
}
626-
}
627-
628-
if (c != 0) {
629-
result += chars[static_cast<std::uint8_t>((charArray[0] & 0xFC) >> 2)];
630-
if (c == 1) {
631-
result += chars[static_cast<std::uint8_t>((charArray[0] & 0x03) << 4)];
632-
} else { // c == 2
633-
result += chars[static_cast<std::uint8_t>(((charArray[0] & 0x03) << 4) + ((charArray[1] & 0xF0) >> 4))];
634-
result += chars[static_cast<std::uint8_t>((charArray[1] & 0x0F) << 2)];
635-
}
636-
}
637-
638-
return result;
639-
}
640-
641602
String PreferencesCache::GetDeviceID()
642603
{
643604
#if defined(DEATH_TARGET_X86)
@@ -814,7 +775,7 @@ namespace Jazz2
814775
#else
815776
static const char DeviceDesc[] = "||||"; std::int32_t DeviceDescLength = sizeof(DeviceDesc) - 1;
816777
#endif
817-
return ToBase64Url(DeviceDesc, DeviceDesc + DeviceDescLength);
778+
return toBase64Url(DeviceDesc, DeviceDesc + DeviceDescLength);
818779
}
819780

820781
String PreferencesCache::GetEffectivePlayerName()

Sources/Jazz2/Scripting/JJ2PlusDefinitions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,11 +2207,11 @@ namespace Jazz2::Scripting
22072207
}
22082208
std::uint64_t unixTimeSec() {
22092209
noop();
2210-
return Containers::DateTime::Now().ToUnixMilliseconds();
2210+
return Containers::DateTime::UtcNow().ToUnixMilliseconds();
22112211
}
22122212
std::uint64_t unixTimeMs() {
22132213
noop();
2214-
return Containers::DateTime::Now().ToUnixMilliseconds() / 1000;
2214+
return Containers::DateTime::UtcNow().ToUnixMilliseconds() / 1000;
22152215
}
22162216

22172217
bool jjRegexIsValid(const String& expression) {

Sources/Shared/Base/Format.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ namespace Death { namespace Implementation {
423423
for (std::size_t formatOffset = 0; formatOffset != format.size(); ) {
424424
// Placeholder begin (or escaped {)
425425
if (format[formatOffset] == '{') {
426-
if (formatOffset + 1 < format.size() && format[formatOffset+1] == '{') {
426+
if (formatOffset + 1 < format.size() && format[formatOffset + 1] == '{') {
427427
writer(format.slice(formatOffset, formatOffset + 1));
428428
formatOffset += 2;
429429
continue;
@@ -442,7 +442,7 @@ namespace Death { namespace Implementation {
442442

443443
// Placeholder end (or escaped })
444444
if (format[formatOffset] == '}') {
445-
if (!inPlaceholder && formatOffset + 1 < format.size() && format[formatOffset+1] == '}') {
445+
if (!inPlaceholder && formatOffset + 1 < format.size() && format[formatOffset + 1] == '}') {
446446
writer(format.slice(formatOffset, formatOffset + 1));
447447
formatOffset += 2;
448448
continue;

Sources/Shared/Containers/DateTime.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace Death { namespace Containers {
4949
static bool IsLeapYear(std::int32_t year) noexcept
5050
{
5151
if (year == -1) {
52-
year = DateTime::Now().GetYear();
52+
year = DateTime::UtcNow().GetYear();
5353
}
5454

5555
return (year % 4 == 0 && ((year % 100) != 0 || (year % 400) == 0));
@@ -857,7 +857,7 @@ namespace Death { namespace Containers {
857857
if (result.IsValid()) {
858858
tm = result.Partitioned();
859859
} else {
860-
tm = DateTime::Now().ResetTime().Partitioned();
860+
tm = DateTime::UtcNow().ResetTime().Partitioned();
861861
}
862862

863863
if (haveMon) {
@@ -1034,29 +1034,16 @@ namespace Death { namespace Containers {
10341034
_dayOfWeek = (Implementation::GetTruncatedJDN(Day, Month, Year) + 2) % 7;
10351035
}
10361036

1037-
DateTime DateTime::Now() noexcept
1038-
{
1039-
#if defined(DEATH_TARGET_WINDOWS)
1040-
SYSTEMTIME st;
1041-
::GetLocalTime(&st);
1042-
return DateTime(st);
1043-
#else
1044-
time_t t = time(nullptr);
1045-
return DateTime(t);
1046-
#endif
1047-
}
1048-
10491037
DateTime DateTime::UtcNow() noexcept
10501038
{
10511039
#if defined(DEATH_TARGET_WINDOWS)
1052-
SYSTEMTIME st;
1053-
::GetSystemTime(&st);
1054-
return DateTime(st);
1040+
FILETIME ft;
1041+
::GetSystemTimeAsFileTime(&ft);
1042+
return DateTime(ft);
10551043
#else
1056-
time_t t = time(nullptr);
1057-
DateTime dt = DateTime(t);
1058-
dt.AdjustToTimezone(UTC);
1059-
return dt;
1044+
struct timeval tp{};
1045+
gettimeofday(&tp, nullptr);
1046+
return DateTime::FromUnixMilliseconds((tp.tv_sec * 1000) + (tp.tv_usec / 1000));
10601047
#endif
10611048
}
10621049

Sources/Shared/Containers/DateTime.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ namespace Death { namespace Containers {
131131
std::int32_t _dayOfWeek;
132132
};
133133

134-
/** @brief Returns @ref DateTime that is set to the current date and time on this computer, expressed as the local time */
135-
static DateTime Now() noexcept;
136134
/** @brief Returns @ref DateTime that is set to the current date and time on this computer, expressed as the UTC time */
137135
static DateTime UtcNow() noexcept;
138136

Sources/Shared/Core/Backward.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,10 +3815,11 @@ namespace Death { namespace Backward {
38153815

38163816
/** @brief Prints standard prologue of the text log file */
38173817
void PrintFilePrologue(IO::Stream* s) {
3818-
auto p = Containers::DateTime::Now().Partitioned();
3818+
auto p = Containers::DateTime::UtcNow().Partitioned();
38193819

38203820
char buffer[64];
3821-
std::int32_t length = snprintf(buffer, sizeof(buffer), "%02u:%02u:%02u.%03u [F] ", p.Hour, p.Minute, p.Second, p.Millisecond);
3821+
std::int32_t length = (std::int32_t)formatInto(buffer, "{:.2}:{:.2}:{:.2}.{:.3} [F] ",
3822+
p.Hour, p.Minute, p.Second, p.Millisecond);
38223823
if (length > 0) {
38233824
s->Write(buffer, length);
38243825
}
@@ -4518,6 +4519,17 @@ namespace Death { namespace Backward {
45184519
auto* _this = GetSingleton();
45194520
auto& context = _this->_context;
45204521

4522+
{
4523+
std::unique_lock<std::mutex> lk(_this->_lock);
4524+
if (_this->_status >= HandlerStatus::Crashed) {
4525+
// Crash handler was already called, wait until it finishes
4526+
while (_this->_status == HandlerStatus::Crashed) {
4527+
::Sleep(10);
4528+
}
4529+
return;
4530+
}
4531+
}
4532+
45214533
if (info == nullptr) {
45224534
# if (defined(_M_IX86) || defined(__i386__)) && defined(DEATH_TARGET_MSVC)
45234535
// RtlCaptureContext() doesn't work on i386
@@ -4548,6 +4560,13 @@ namespace Death { namespace Backward {
45484560

45494561
{
45504562
std::unique_lock<std::mutex> lk(_this->_lock);
4563+
if (_this->_status >= HandlerStatus::Crashed) {
4564+
// Crash handler was already called, wait until it finishes
4565+
while (_this->_status == HandlerStatus::Crashed) {
4566+
::Sleep(10);
4567+
}
4568+
return;
4569+
}
45514570
_this->_status = HandlerStatus::Crashed;
45524571
}
45534572

Sources/Shared/Core/Logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ namespace Death { namespace Trace {
709709

710710
for (ThreadContext* tc : _activeThreadContextsCache) {
711711
TransitEvent const* te = tc->_transitEventBuffer.front();
712-
if (te && (minTs > te->Timestamp)) {
712+
if (te != nullptr && minTs > te->Timestamp) {
713713
minTs = te->Timestamp;
714714
threadContext = tc;
715715
}

0 commit comments

Comments
 (0)