| 78 | } |
| 79 | |
| 80 | void logWrite(LogWriteType type, const char* tag, const char* str, ...) |
| 81 | { |
| 82 | if (type >= LOG_COUNT || !s_logFile.isOpen() || !tag || !str) { return; } |
| 83 | |
| 84 | auto now = std::chrono::system_clock::now(); |
| 85 | std::time_t now_c = std::chrono::system_clock::to_time_t(now); |
| 86 | std::tm now_tm; |
| 87 | |
| 88 | #ifdef _WIN32 |
| 89 | localtime_s(&now_tm, &now_c); // For thread safety on Windows |
| 90 | #else |
| 91 | localtime_r(&now_c, &now_tm); // For thread safety on Linux |
| 92 | #endif |
| 93 | |
| 94 | auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 95 | now.time_since_epoch()) % 1000; |
| 96 | |
| 97 | char timeStr[40]; |
| 98 | if (includeTime) |
| 99 | { |
| 100 | strftime(timeStr, sizeof(timeStr) - 4, "%Y-%b-%d %H:%M:%S", &now_tm); // Leave space for milliseconds |
| 101 | |
| 102 | // Add milliseconds to the formatted time |
| 103 | snprintf(timeStr + strlen(timeStr), 8, ".%03lld - ", milliseconds.count()); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | timeStr[0] = 0; |
| 108 | } |
| 109 | |
| 110 | //Handle the variable input, "printf" style messages |
| 111 | va_list arg; |
| 112 | va_start(arg, str); |
| 113 | vsprintf(s_msgStr, str, arg); |
| 114 | va_end(arg); |
| 115 | |
| 116 | //Format the message |
| 117 | if (type != LOG_MSG) |
| 118 | { |
| 119 | sprintf(s_workStr, "%s[%s : %s] %s\r\n", timeStr, c_typeNames[type], tag, s_msgStr); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | sprintf(s_workStr, "%s[%s] %s\r\n", timeStr, tag, s_msgStr); |
| 124 | } |
| 125 | |
| 126 | //Write to disk |
| 127 | s_logFile.writeBuffer(s_workStr, (u32)strlen(s_workStr)); |
| 128 | //Make sure to flush the file to disk if a crash is likely. |
| 129 | //if (type == LOG_ERROR || type == LOG_CRITICAL) |
| 130 | { |
| 131 | s_logFile.flush(); |
| 132 | } |
| 133 | //Write to the debugger or terminal output. |
| 134 | #ifdef _WIN32 |
| 135 | OutputDebugStringA(s_workStr); |
| 136 | #else |
| 137 | fprintf(stderr, "%s", s_workStr); |