Skip to content

Commit 24a6935

Browse files
Improved long calls logs (#1472)
* Improvements of long call logs * Clang format * MacOS build fix * Fix tests * lib-streamlabs-ipc submodule update
1 parent 9241719 commit 24a6935

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

lib-streamlabs-ipc

Binary file not shown.

obs-studio-client/source/nodeobs_api.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Napi::Value api::OBS_API_initAPI(const Napi::CallbackInfo &info)
4747
if (!conn)
4848
return info.Env().Undefined();
4949

50-
conn->set_freez_callback(ipc_freez_callback, path);
50+
conn->set_freeze_callback(ipc_freeze_callback, path);
5151

5252
std::vector<ipc::value> response = conn->call_synchronous_helper(
5353
"API", "OBS_API_initAPI", {ipc::value(path), ipc::value(language), ipc::value(version), ipc::value(crashserverurl)});

obs-studio-client/source/utility.cpp

+29-27
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <locale>
2222

2323
#include "nlohmann/json.hpp"
24+
#include <iomanip>
2425
#include <sstream>
2526
#include <fstream>
2627

@@ -150,27 +151,21 @@ void limit_log_file_size(const std::string &log_file, size_t limit)
150151
}
151152
}
152153

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)
154155
{
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";
169158

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);
173167

168+
std::string current_status = read_app_state_data(app_state_path);
174169
if (current_status.size() != 0) {
175170
std::string updated_status = "";
176171
std::string existing_flag_value = "";
@@ -182,7 +177,7 @@ void ipc_freez_callback(bool freez_detected, std::string app_state_path, std::st
182177
} catch (...) {
183178
}
184179

185-
if (freez_detected) {
180+
if (freeze_detected) {
186181
if (existing_flag_value.empty())
187182
jsonEntry[flag_name] = flag_value;
188183
} else {
@@ -196,17 +191,24 @@ void ipc_freez_callback(bool freez_detected, std::string app_state_path, std::st
196191
}
197192

198193
try {
194+
static bool limited = false;
195+
if (!limited) {
196+
limited = true;
197+
limit_log_file_size(call_log_path, 1024 * 1024);
198+
}
199+
199200
std::ofstream out_state_file;
200-
limit_log_file_size(call_log_path, 1024 * 1024);
201201
out_state_file.open(call_log_path, std::ios::app | std::ios::out);
202202
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;
210212
}
211213
} catch (...) {
212214
}

obs-studio-client/source/utility.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,5 @@ void SetThreadName(const char *threadName);
171171
std::string from_utf16_wide_to_utf8(const wchar_t *from, size_t length = -1);
172172
std::wstring from_utf8_to_utf16_wide(const char *from, size_t length = -1);
173173

174-
//write detected possible reason of abnormal app close to a file used to submit statistics
175-
void ipc_freez_callback(bool freez_detected, std::string app_state_path, std::string call_name, int tiemout);
174+
// write detected possible reason of abnormal app close to a file used to submit statistics
175+
void ipc_freeze_callback(const std::string &app_state_path, const std::string &call_name, int total_time, int obs_time);

0 commit comments

Comments
 (0)