Skip to content

Commit 425e7e3

Browse files
committed
MtProtoKit: bound decoded-object log lines, honour MTLogSetEnabled
A tester's log came back 44 MB across 21 rotated files covering only 6.6 hours — the previous log covered 21 hours in the same budget. 31.3 MB of it, 71% of everything collected, was two lines: [MTRequestMessageService#... response for <id> is <whole decoded object>] [MTProto#...@... [N] received <whole decoded object> (...)] `%@` on a parsed rpc result prints the object in full. Sticker-set responses alone accounted for 14.6 MB across 113 lines — 129 KB per line, one of them 458,653 characters. Rotation is by file count against a fixed per-file size, so the payloads were evicting the diagnostics they were meant to sit beside. MTLogTruncatedDescription caps a description at 1024 characters and appends the true length, expanding the cut to whole character sequences so it cannot land inside a surrogate pair. Replaying the same log through the cap leaves 13.1 MB of 42 MB — the same rotation budget now holds roughly 3.2x the span. Separately, MTLog/MTLogWithPrefix/MTShortLog tested only whether a logging function was installed. NetworkRegisterLoggingFunction installs those once and never removes them; MTLogSetEnabled only flips MTLogEnabledValue, which MTLogEnabled() consults but the log functions themselves did not. Turning logging off therefore silenced only the call sites wrapped in an explicit MTLogEnabled() check, and MTProto's outgoing short-message line was building its description and logging unconditionally. Both are fixed. Co-authored-by: D3C0Y <decoder-dev@users.noreply.github.com>
1 parent d8aace5 commit 425e7e3

4 files changed

Lines changed: 28 additions & 7 deletions

File tree

‎submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTLogging.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ extern "C" {
1010
#endif
1111

1212
bool MTLogEnabled();
13+
14+
/// A decoded MTProto object, cut down to something a log line can hold.
15+
///
16+
/// `%@` on a parsed rpc result or update prints the entire object. A sticker set or a forum
17+
/// topic list runs to hundreds of kilobytes on a single line, and in one tester's log the
18+
/// responses alone came to 31.3 MB of 44 MB — 71% of everything collected, against a fixed
19+
/// rotation budget. The head of the description identifies the message; the rest is payload.
20+
NSString *MTLogTruncatedDescription(id value);
1321
void MTLog(NSString *format, ...);
1422
void MTLogWithPrefix(NSString *(^getLogPrefix)(), NSString *format, ...);
1523
void MTShortLog(NSString *format, ...);

‎submodules/MtProtoKit/Sources/MTLogging.m‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ bool MTLogEnabled() {
88
return loggingFunction != NULL && MTLogEnabledValue;
99
}
1010

11+
NSString *MTLogTruncatedDescription(id value) {
12+
NSString *description = [NSString stringWithFormat:@"%@", value];
13+
static const NSUInteger limit = 1024;
14+
if (description.length <= limit) {
15+
return description;
16+
}
17+
// Expanded to whole character sequences, so the cut cannot land inside a surrogate pair.
18+
NSRange range = [description rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, limit)];
19+
return [NSString stringWithFormat:@"%@… (truncated, %lu characters total)", [description substringWithRange:range], (unsigned long)description.length];
20+
}
21+
1122
void MTLog(NSString *format, ...) {
1223
va_list L;
1324
va_start(L, format);
14-
if (loggingFunction != NULL) {
25+
if (loggingFunction != NULL && MTLogEnabledValue) {
1526
NSString *string = [[NSString alloc] initWithFormat:format arguments:L];
1627
loggingFunction(string);
1728
}
@@ -21,7 +32,7 @@ void MTLog(NSString *format, ...) {
2132
void MTLogWithPrefix(NSString *(^getLogPrefix)(), NSString *format, ...) {
2233
va_list L;
2334
va_start(L, format);
24-
if (loggingFunction != NULL) {
35+
if (loggingFunction != NULL && MTLogEnabledValue) {
2536
NSString *string = [[NSString alloc] initWithFormat:format arguments:L];
2637
if (getLogPrefix) {
2738
NSString *prefix = getLogPrefix();
@@ -37,7 +48,7 @@ void MTLogWithPrefix(NSString *(^getLogPrefix)(), NSString *format, ...) {
3748
void MTShortLog(NSString *format, ...) {
3849
va_list L;
3950
va_start(L, format);
40-
if (shortLoggingFunction != NULL) {
51+
if (shortLoggingFunction != NULL && MTLogEnabledValue) {
4152
NSString *string = [[NSString alloc] initWithFormat:format arguments:L];
4253
shortLoggingFunction(string);
4354
}

‎submodules/MtProtoKit/Sources/MTProto.m‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ - (NSString *)outgoingShortMessageDescription:(MTOutgoingMessage *)message messa
880880

881881
- (NSString *)incomingMessageDescription:(MTIncomingMessage *)message
882882
{
883-
return [[NSString alloc] initWithFormat:@"%@ (%" PRId64", %" PRId64"/%" PRId64")", message.body, message.messageId, message.authKeyId, message.sessionId];
883+
return [[NSString alloc] initWithFormat:@"%@ (%" PRId64", %" PRId64"/%" PRId64")", MTLogTruncatedDescription(message.body), message.messageId, message.authKeyId, message.sessionId];
884884
}
885885

886886
- (MTDatacenterAuthKey *)getAuthKeyForCurrentScheme:(MTTransportScheme *)scheme createIfNeeded:(bool)createIfNeeded authInfoSelector:(MTDatacenterAuthInfoSelector *)authInfoSelector {
@@ -1106,8 +1106,10 @@ - (void)transportReadyForTransaction:(MTTransport *)transport scheme:(MTTranspor
11061106
NSString *messageDescription = [self outgoingMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo authKeyId:authKey.authKeyId];
11071107
MTLogWithPrefix(_getLogPrefix, @"[MTProto#%p@%p preparing %@]", self, _context, messageDescription);
11081108
}
1109-
NSString *shortMessageDescription = [self outgoingShortMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo];
1110-
MTShortLog(@"[MTProto#%p@%p preparing %@]", self, _context, shortMessageDescription);
1109+
if (MTLogEnabled()) {
1110+
NSString *shortMessageDescription = [self outgoingShortMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo];
1111+
MTShortLog(@"[MTProto#%p@%p preparing %@]", self, _context, shortMessageDescription);
1112+
}
11111113

11121114
if (!monotonityViolated || _useUnauthorizedMode)
11131115
{

‎submodules/MtProtoKit/Sources/MTRequestMessageService.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ - (void)mtProto:(MTProto *)__unused mtProto receivedMessage:(MTIncomingMessage *
822822

823823
if (rpcResult != nil) {
824824
if (MTLogEnabled()) {
825-
MTLog(@"[MTRequestMessageService#%p response for %" PRId64 " is %@]", self, request.requestContext.messageId, rpcResult);
825+
MTLog(@"[MTRequestMessageService#%p response for %" PRId64 " is %@]", self, request.requestContext.messageId, MTLogTruncatedDescription(rpcResult));
826826
}
827827
} else {
828828
if (MTLogEnabled()) {

0 commit comments

Comments
 (0)