Skip to content

Commit dbb8892

Browse files
author
mgulacti
committed
log eklendi
1 parent 09ee65f commit dbb8892

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

libs/platform/get_text_by_id.cpp

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,39 @@ bool GetJsonBuffer(platform::TextSource textSource, string const & localeName, s
9898
catch (base::Json::Exception const & exJson)
9999
{
100100
// Prepare hex sample for logs
101-
std::ostringstream oss;
102-
size_t sampleLen = std::min<size_t>(jsonBuffer.size(), 128);
103-
for (size_t i = 0; i < sampleLen; ++i)
101+
std::ostringstream oss_head, oss_tail;
102+
size_t jsonSize = jsonBuffer.size();
103+
size_t head_len = std::min<size_t>(jsonSize, 128);
104+
for (size_t i = 0; i < head_len; ++i)
104105
{
105-
oss << std::hex << std::setfill('0') << std::setw(2)
106-
<< (static_cast<unsigned int>(static_cast<unsigned char>(jsonBuffer[i]))) << ' ';
106+
oss_head << std::hex << std::setfill('0') << std::setw(2)
107+
<< (static_cast<unsigned int>(static_cast<unsigned char>(jsonBuffer[i]))) << ' ';
107108
}
108-
// Log as WARNING first so we can attempt a non-fatal fallback before
109-
// escalating to an error that triggers the abort behavior in some
110-
// Android builds.
111-
//LOG(LWARNING, ("JSON parse error for locale:", localeName, "file:", relPath, "error:", exJson.what(), "sample_bytes:", oss.str()));
109+
110+
if (jsonSize > 128)
111+
{
112+
size_t tail_start = jsonSize > 128 ? jsonSize - 128 : 0;
113+
for (size_t i = tail_start; i < jsonSize; ++i)
114+
{
115+
oss_tail << std::hex << std::setfill('0') << std::setw(2)
116+
<< (static_cast<unsigned int>(static_cast<unsigned char>(jsonBuffer[i]))) << ' ';
117+
}
118+
}
119+
120+
std::ostringstream log_msg;
121+
log_msg << "JSON parse error for locale: " << localeName
122+
<< " file: " << relPath
123+
<< " error: " << exJson.what()
124+
<< " head_bytes: " << oss_head.str()
125+
<< " tail_bytes: " << oss_tail.str();
126+
LOG(LWARNING, (log_msg.str()));
112127

113128
// Mitigation: try interpreting the bytes as ISO-8859-1 (Latin1) and
114129
// convert to UTF-8, then attempt parsing again. If conversion+
115130
// parse succeeds we keep the converted buffer and proceed; otherwise
116131
// preserve original behavior and rethrow to trigger fallback logic.
117132
try
118133
{
119-
//LOG(LINFO, ("Attempting Latin1->UTF8 fallback for:", relPath));
120134
string converted = Latin1ToUtf8(jsonBuffer);
121135
// Try parsing converted buffer
122136
try
@@ -133,13 +147,24 @@ bool GetJsonBuffer(platform::TextSource textSource, string const & localeName, s
133147
// Fallback parsing failed — log as WARNING so we can try higher-
134148
// level fallback (embedded default) instead of aborting the
135149
// process on platforms where ERROR logs trigger aborts.
136-
//LOG(LWARNING, ("Latin1->UTF8 fallback parse failed for:", relPath,"original_error:", exJson.what(), "fallback_error:", ex2.what(), "sample_bytes:", oss.str()));
150+
std::ostringstream log_msg;
151+
log_msg << "Latin1->UTF8 fallback parse failed for: " << relPath
152+
<< " original_error: " << exJson.what()
153+
<< " fallback_error: " << ex2.what()
154+
<< " head_bytes: " << oss_head.str()
155+
<< " tail_bytes: " << oss_tail.str();
156+
LOG(LWARNING, (log_msg.str()));
137157
MYTHROW(RootException, ("Invalid JSON in file", relPath, exJson.what()));
138158
}
139159
}
140160
catch (std::exception const & e)
141161
{
142-
LOG(LWARNING, ("Latin1->UTF8 fallback failed with exception:", e.what(), "file:", relPath));
162+
std::ostringstream log_msg;
163+
log_msg << "Latin1->UTF8 fallback failed with exception: " << e.what()
164+
<< " file: " << relPath
165+
<< " head_bytes: " << oss_head.str()
166+
<< " tail_bytes: " << oss_tail.str();
167+
LOG(LWARNING, (log_msg.str()));
143168
MYTHROW(RootException, ("Invalid JSON in file", relPath, exJson.what()));
144169
}
145170
}

0 commit comments

Comments
 (0)