Use `struct tm` to store time
This commit is contained in:
		
							parent
							
								
									811a5f122f
								
							
						
					
					
						commit
						d1afe12773
					
				
							
								
								
									
										8
									
								
								log.cpp
								
								
								
								
							
							
						
						
									
										8
									
								
								log.cpp
								
								
								
								
							|  | @ -6,9 +6,10 @@ | |||
| 
 | ||||
| std::vector<LogEntry> log_entries; | ||||
| 
 | ||||
| LogEntry::LogEntry(time_t time_, std::string message_) : time(time_), message(std::move(message_)) { | ||||
| LogEntry::LogEntry(time_t time, std::string message_) : message(std::move(message_)) { | ||||
|     size_t pos; | ||||
| 
 | ||||
|     localtime_r(&time, &this->time); | ||||
|     while ((pos = this->message.find('\n')) != std::string::npos) { | ||||
|         this->message.replace(pos, 1, 1, ' '); | ||||
|     } | ||||
|  | @ -16,15 +17,14 @@ LogEntry::LogEntry(time_t time_, std::string message_) : time(time_), message(st | |||
| 
 | ||||
| 
 | ||||
| std::string to_string(const LogEntry& entry) { | ||||
|     struct tm tm; | ||||
|     char time_as_str[128] = {0}; | ||||
|     strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&entry.time, &tm)); | ||||
|     strftime(time_as_str, 127 * sizeof(char), "%c", &entry.time); | ||||
|     return std::string(1, '[') + time_as_str + "] " + entry.message; | ||||
| } | ||||
| 
 | ||||
| void print_log(const LogEntry& entry) { | ||||
|     char time_as_str[128] = {0}; | ||||
|     strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&entry.time)); | ||||
|     strftime(time_as_str, 127 * sizeof(char), "%c", &entry.time); | ||||
|     fprintf(stderr, "[%s] %s\n", time_as_str, entry.message.c_str()); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										5
									
								
								log.h
								
								
								
								
							
							
						
						
									
										5
									
								
								log.h
								
								
								
								
							|  | @ -1,14 +1,15 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <ctime> | ||||
| #include <string> | ||||
| #include <vector> | ||||
| 
 | ||||
| struct LogEntry { | ||||
|     time_t time; | ||||
|     struct tm time; | ||||
|     std::string message; | ||||
| 
 | ||||
|     LogEntry() = default; | ||||
|     LogEntry(time_t time_, std::string message_); | ||||
|     LogEntry(time_t time, std::string message_); | ||||
| }; | ||||
| extern std::vector<LogEntry> log_entries; | ||||
| 
 | ||||
|  |  | |||
|  | @ -70,8 +70,7 @@ static inline std::string rightpad(const std::string& str, size_t characters) { | |||
| 
 | ||||
| std::string to_string(const LogcatEntry& logcat_entry) { | ||||
|     char logcat_style_time_as_str[32] = {0}; | ||||
|     struct tm tm; | ||||
|     strftime(logcat_style_time_as_str, 31 * sizeof(char), "%Y-%m-%d %H:%M:%S", localtime_r(&logcat_entry.time, &tm)); | ||||
|     strftime(logcat_style_time_as_str, 31 * sizeof(char), "%Y-%m-%d %H:%M:%S", &logcat_entry.time); | ||||
| 
 | ||||
|     return std::string(logcat_style_time_as_str) | ||||
|         + ' ' + leftpad(logcat_entry.user.value_or("     "), 5) | ||||
|  | @ -138,10 +137,11 @@ std::optional<LogcatEntry> try_parse_logcat_entry(char* buf, size_t length, Buff | |||
|     if (matches[2].rm_so > -1 && matches[2].rm_eo > -1) { | ||||
|         user = std::string(&buf[matches[2].rm_so], static_cast<size_t>(matches[2].rm_eo - matches[2].rm_so)); | ||||
|     } | ||||
| 
 | ||||
|     // if pcre2 gives us negative offsets then i'll die
 | ||||
|     LogcatEntry logcat_entry = { | ||||
|         .buffer = buffer, | ||||
|         .time = to_long(&buf[matches[1].rm_so], &buf[matches[1].rm_eo]), | ||||
|         // time to be set at the end
 | ||||
|         .user = std::move(user), | ||||
|         .pid = to_unsigned_long_long(&buf[matches[3].rm_so], &buf[matches[3].rm_eo]), | ||||
|         .tid = to_unsigned_long_long(&buf[matches[4].rm_so], &buf[matches[4].rm_eo]), | ||||
|  | @ -149,6 +149,9 @@ std::optional<LogcatEntry> try_parse_logcat_entry(char* buf, size_t length, Buff | |||
|         .tag = std::string(&buf[matches[6].rm_so], static_cast<size_t>(matches[6].rm_eo - matches[6].rm_so)), | ||||
|         .message = std::string(&buf[matches[7].rm_so], static_cast<size_t>(matches[7].rm_eo - matches[7].rm_so)), | ||||
|     }; | ||||
| 
 | ||||
|     time_t time = to_long(&buf[matches[1].rm_so], &buf[matches[1].rm_eo]); | ||||
|     localtime_r(&time, &logcat_entry.time); | ||||
|     return std::move(logcat_entry); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <ctime> | ||||
| #include <string> | ||||
| #include <optional> | ||||
| #include <nlohmann/json.hpp> | ||||
|  | @ -27,7 +28,7 @@ enum class Priority { | |||
| 
 | ||||
| struct LogcatEntry { | ||||
|     Buffer buffer; | ||||
|     time_t time; | ||||
|     struct tm time; | ||||
|     std::optional<std::string> user; | ||||
|     size_t pid; | ||||
|     size_t tid; | ||||
|  |  | |||
|  | @ -52,27 +52,33 @@ void debug_window(LogcatThread& logcat_thread, LogcatEntries& logcat_entries) { | |||
| 
 | ||||
|     ImGui::Separator(); | ||||
|     if (ImGui::Button("Add test entry (w/ user)")) { | ||||
|         logcat_entries.push_back({ | ||||
|         LogcatEntry entry = { | ||||
|             .buffer = Buffer::Main, | ||||
|             .time = time(nullptr), | ||||
|             // time to be set later
 | ||||
|             .user = "blankie", | ||||
|             .pid = 69, | ||||
|             .tid = 420, | ||||
|             .priority = Priority::Error, | ||||
|             .tag = "blanket, inc.", | ||||
|             .message = "Failed to make blanket", | ||||
|         }); | ||||
|         }; | ||||
|         time_t entry_time = time(nullptr); | ||||
|         localtime_r(&entry_time, &entry.time); | ||||
|         logcat_entries.push_back(std::move(entry)); | ||||
|     } | ||||
|     if (ImGui::Button("Add test entry (w/o user)")) { | ||||
|         logcat_entries.push_back({ | ||||
|         LogcatEntry entry = { | ||||
|             .buffer = Buffer::Crash, | ||||
|             .time = time(nullptr), | ||||
|             // time to be set later
 | ||||
|             .pid = 420, | ||||
|             .tid = 69, | ||||
|             .priority = Priority::Fatal, | ||||
|             .tag = "blanket, inc.", | ||||
|             .message = "Failed to invent blankets", | ||||
|         }); | ||||
|         }; | ||||
|         time_t entry_time = time(nullptr); | ||||
|         localtime_r(&entry_time, &entry.time); | ||||
|         logcat_entries.push_back(std::move(entry)); | ||||
|     } | ||||
| 
 | ||||
|     ImGui::End(); | ||||
|  |  | |||
|  | @ -6,9 +6,8 @@ | |||
| #include "logs.h" | ||||
| 
 | ||||
| static inline void render_table_item_context_menu(const LogEntry& log_entry) { | ||||
|     struct tm tm; | ||||
|     char time_as_str[128] = {0}; | ||||
|     strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&log_entry.time, &tm)); | ||||
|     strftime(time_as_str, 127 * sizeof(char), "%c", &log_entry.time); | ||||
| 
 | ||||
|     ImGui::TextDisabled("[%s] %s", time_as_str, log_entry.message.c_str()); | ||||
|     ImGui::Separator(); | ||||
|  | @ -32,9 +31,8 @@ static inline void render_table_item(const LogEntry& log_entry, size_t log_entry | |||
| 
 | ||||
|     ImGui::TableNextRow(); | ||||
|     if (ImGui::TableSetColumnIndex(0)) { | ||||
|         struct tm tm; | ||||
|         char time_as_str[128] = {0}; | ||||
|         strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&log_entry.time, &tm)); | ||||
|         strftime(time_as_str, 127 * sizeof(char), "%c", &log_entry.time); | ||||
| 
 | ||||
|         ImGui::TextUnformatted(time_as_str); | ||||
|         table_item_popup(); | ||||
|  |  | |||
|  | @ -19,9 +19,8 @@ static inline void render_table_item_context_menu(const LogcatEntry& logcat_entr | |||
| 
 | ||||
|     if (ImGui::Selectable("Copy")) ImGui::SetClipboardText(text.c_str()); | ||||
|     if (ImGui::Selectable("Copy Time")) { | ||||
|         struct tm tm; | ||||
|         char time_as_str[128] = {0}; | ||||
|         strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&logcat_entry.time, &tm)); | ||||
|         strftime(time_as_str, 127 * sizeof(char), "%c", &logcat_entry.time); | ||||
|         ImGui::SetClipboardText(time_as_str); | ||||
|     } | ||||
|     if (ImGui::Selectable("Copy User", false, logcat_entry.user ? 0 : ImGuiSelectableFlags_Disabled)) { | ||||
|  | @ -48,9 +47,8 @@ static inline void render_table_item(const LogcatEntry& logcat_entry, size_t log | |||
| 
 | ||||
|     ImGui::TableNextRow(); | ||||
|     if (ImGui::TableSetColumnIndex(0)) { | ||||
|         struct tm tm; | ||||
|         char time_as_str[128] = {0}; | ||||
|         strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&logcat_entry.time, &tm)); | ||||
|         strftime(time_as_str, 127 * sizeof(char), "%c", &logcat_entry.time); | ||||
| 
 | ||||
|         ImGui::TextUnformatted(time_as_str); | ||||
|         table_item_popup(); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue