21
21
#include < locale>
22
22
23
23
#include " nlohmann/json.hpp"
24
+ #include < iomanip>
24
25
#include < sstream>
25
26
#include < fstream>
26
27
@@ -150,27 +151,21 @@ void limit_log_file_size(const std::string &log_file, size_t limit)
150
151
}
151
152
}
152
153
153
- void ipc_freez_callback ( bool freez_detected, std::string app_state_path, std::string call_name, int timeout )
154
+ void ipc_freeze_callback ( const std::string &app_state_dir, const std::string & call_name, int total_time, int obs_time )
154
155
{
155
- static int freez_counter = 0 ;
156
- if (freez_detected) {
157
- freez_counter++;
158
- if (freez_counter > 1 ) {
159
- return ;
160
- }
161
- } else {
162
- freez_counter--;
163
- if (freez_counter > 0 ) {
164
- return ;
165
- }
166
- }
167
- const std::string flag_value = " ipc_freez" ;
168
- const std::string flag_name = " detected" ;
156
+ static const std::string flag_name = " detected" ;
157
+ static const std::string flag_value = " ipc_freeze" ;
169
158
170
- static std::string call_log_path = app_state_path + " \\ long_calls.txt" ;
171
- app_state_path += " \\ appState" ;
172
- std::string current_status = read_app_state_data (app_state_path);
159
+ static const std::string call_log_path = app_state_dir + " \\ long_calls.txt" ;
160
+ static const std::string app_state_path = app_state_dir + " \\ appState" ;
161
+ static const auto pid = ::getpid ();
162
+
163
+ const bool freeze_detected = obs_time < 0 ;
164
+
165
+ static std::mutex file_mutex;
166
+ std::unique_lock lock (file_mutex);
173
167
168
+ std::string current_status = read_app_state_data (app_state_path);
174
169
if (current_status.size () != 0 ) {
175
170
std::string updated_status = " " ;
176
171
std::string existing_flag_value = " " ;
@@ -182,7 +177,7 @@ void ipc_freez_callback(bool freez_detected, std::string app_state_path, std::st
182
177
} catch (...) {
183
178
}
184
179
185
- if (freez_detected ) {
180
+ if (freeze_detected ) {
186
181
if (existing_flag_value.empty ())
187
182
jsonEntry[flag_name] = flag_value;
188
183
} else {
@@ -196,17 +191,24 @@ void ipc_freez_callback(bool freez_detected, std::string app_state_path, std::st
196
191
}
197
192
198
193
try {
194
+ static bool limited = false ;
195
+ if (!limited) {
196
+ limited = true ;
197
+ limit_log_file_size (call_log_path, 1024 * 1024 );
198
+ }
199
+
199
200
std::ofstream out_state_file;
200
- limit_log_file_size (call_log_path, 1024 * 1024 );
201
201
out_state_file.open (call_log_path, std::ios::app | std::ios::out);
202
202
if (out_state_file.is_open ()) {
203
- if (freez_detected) {
204
- out_state_file << " [" << getpid () << " ]" << call_name << " :" << timeout;
205
- } else {
206
- out_state_file << " :" << timeout << " ms\n " ;
207
- }
208
- out_state_file.flush ();
209
- out_state_file.close ();
203
+ const auto now = std::chrono::system_clock::now ();
204
+ const auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch ());
205
+ char time_buf[256 ];
206
+ const auto time = std::chrono::system_clock::to_time_t (now);
207
+ ::strftime (time_buf, 256 , " %Y-%m-%d %H:%M:%S" , std::localtime(&time));
208
+
209
+ out_state_file << " [" << time_buf << " ." << std::setw (3 ) << std::setfill (' 0' ) << (now_ms.count () % 1000 ) << " ] [pid:" << pid
210
+ << " , tid:" << std::this_thread::get_id () << " ] " << (freeze_detected ? " (freeze) " : " " ) << call_name
211
+ << " , total:" << total_time << " ms, obs:" << obs_time << " ms" << std::endl;
210
212
}
211
213
} catch (...) {
212
214
}
0 commit comments