fix bug and add log
This commit is contained in:
@@ -8,7 +8,10 @@
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -17,10 +20,43 @@ FILE* g_fp = nullptr;
|
||||
std::string g_path;
|
||||
constexpr size_t kMaxSize = 2 * 1024 * 1024; // 2 MB before rotation
|
||||
|
||||
// Throttle bookkeeping: one slot per distinct key passed to log_throttled().
|
||||
struct ThrottleSlot {
|
||||
int64_t last_emit_ms = 0; // when we last actually wrote a line for this key
|
||||
uint32_t suppressed = 0; // number of calls swallowed since last_emit_ms
|
||||
};
|
||||
std::unordered_map<std::string, ThrottleSlot> g_throttle;
|
||||
constexpr int64_t kThrottleWindowMs = 2000; // 2 seconds
|
||||
|
||||
pid_t currentTid() {
|
||||
return static_cast<pid_t>(syscall(SYS_gettid));
|
||||
}
|
||||
|
||||
int64_t nowMs() {
|
||||
timespec ts{};
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return static_cast<int64_t>(ts.tv_sec) * 1000 + ts.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
// Assumes g_mtx is held. Writes a single pre-formatted line to disk + logcat.
|
||||
void writeLineLocked(const char* body) {
|
||||
pid_t tid = currentTid();
|
||||
|
||||
timespec ts{};
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
struct tm tm_info{};
|
||||
localtime_r(&ts.tv_sec, &tm_info);
|
||||
char timebuf[32];
|
||||
strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm_info);
|
||||
|
||||
if (g_fp) {
|
||||
std::fprintf(g_fp, "[%s.%03ld][tid=%d] %s\n",
|
||||
timebuf, ts.tv_nsec / 1000000, tid, body);
|
||||
std::fflush(g_fp);
|
||||
}
|
||||
__android_log_print(ANDROID_LOG_INFO, "FACE_DBG", "[tid=%d] %s", tid, body);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace DebugLog {
|
||||
@@ -67,22 +103,53 @@ void log(const char* fmt, ...) {
|
||||
va_end(ap);
|
||||
if (n < 0) return;
|
||||
|
||||
pid_t tid = currentTid();
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
writeLineLocked(buf);
|
||||
}
|
||||
|
||||
timespec ts{};
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
struct tm tm_info{};
|
||||
localtime_r(&ts.tv_sec, &tm_info);
|
||||
char timebuf[32];
|
||||
strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm_info);
|
||||
void log_throttled(const char* key, const char* fmt, ...) {
|
||||
char buf[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int n = std::vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0) return;
|
||||
|
||||
const char* k = key ? key : "";
|
||||
int64_t now = nowMs();
|
||||
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
if (g_fp) {
|
||||
std::fprintf(g_fp, "[%s.%03ld][tid=%d] %s\n",
|
||||
timebuf, ts.tv_nsec / 1000000, tid, buf);
|
||||
std::fflush(g_fp);
|
||||
auto it = g_throttle.find(k);
|
||||
if (it == g_throttle.end()) {
|
||||
// First time we see this key -> always log.
|
||||
ThrottleSlot slot;
|
||||
slot.last_emit_ms = now;
|
||||
slot.suppressed = 0;
|
||||
g_throttle.emplace(k, slot);
|
||||
writeLineLocked(buf);
|
||||
return;
|
||||
}
|
||||
__android_log_print(ANDROID_LOG_INFO, "FACE_DBG", "[tid=%d] %s", tid, buf);
|
||||
|
||||
ThrottleSlot& slot = it->second;
|
||||
int64_t elapsed = now - slot.last_emit_ms;
|
||||
if (elapsed < kThrottleWindowMs) {
|
||||
// Still inside the 2s window -> swallow.
|
||||
++slot.suppressed;
|
||||
return;
|
||||
}
|
||||
|
||||
// Window elapsed: emit the line, annotated with how many we swallowed.
|
||||
if (slot.suppressed > 0) {
|
||||
char annotated[1200];
|
||||
std::snprintf(annotated, sizeof(annotated),
|
||||
"%s (repeated %u times in last %lldms, key=%s)",
|
||||
buf, slot.suppressed, (long long)elapsed, k);
|
||||
writeLineLocked(annotated);
|
||||
} else {
|
||||
writeLineLocked(buf);
|
||||
}
|
||||
slot.last_emit_ms = now;
|
||||
slot.suppressed = 0;
|
||||
}
|
||||
|
||||
void flush() {
|
||||
|
||||
Reference in New Issue
Block a user