Skip to content
Snippets Groups Projects
Commit bc2c6872 authored by George Nachman's avatar George Nachman
Browse files

Name auto session logs with a format defined by an advanced prefs taking...

Name auto session logs with a format defined by an advanced prefs taking values from the session variables.  Issue 1695.
parent 93d9587b
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -183,6 +183,10 @@ static NSString *const kVariableKeySessionUsername = @"session.username";
static NSString *const kVariableKeySessionPath = @"session.path";
static NSString *const kVariableKeySessionLastCommand = @"session.lastCommand";
static NSString *const kVariableKeySessionTTY = @"session.tty";
static NSString *const kVariableKeyTermID = @"session.termid";
static NSString *const kVariableKeySessionCreationTimeString = @"session.creationTimeString";
static NSString *const kVariableKeySessionPID = @"iterm2.pid";
static NSString *const kVariableKeySessionAutoLogID = @"session.autoLogId";
 
// Maps Session GUID to saved contents. Only live between window restoration
// and the end of startup activities.
Loading
Loading
@@ -461,6 +465,7 @@ static const NSUInteger kMaxHosts = 100;
NSInteger _adaptiveFrameRateThroughputThreshold;
double _slowFrameRate;
 
uint32_t _autoLogId;
}
 
+ (void)registerSessionInArrangement:(NSDictionary *)arrangement {
Loading
Loading
@@ -484,6 +489,7 @@ static const NSUInteger kMaxHosts = 100;
- (instancetype)init {
self = [super init];
if (self) {
_autoLogId = arc4random();
_useAdaptiveFrameRate = [iTermAdvancedSettingsModel useAdaptiveFrameRate];
_adaptiveFrameRateThroughputThreshold = [iTermAdvancedSettingsModel adaptiveFrameRateThroughputThreshold];
_slowFrameRate = [iTermAdvancedSettingsModel slowFrameRate];
Loading
Loading
@@ -787,6 +793,19 @@ ITERM_WEAKLY_REFERENCEABLE
} else {
[_variables removeObjectForKey:kVariableKeySessionTTY];
}
if (_variables[kVariableKeyTermID] == nil) {
// Variables that only need to be updated once.
_variables[kVariableKeyTermID] = [self.sessionId stringByReplacingOccurrencesOfString:@":" withString:@"."];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyyMMdd_HHmmss";
_variables[kVariableKeySessionCreationTimeString] = [dateFormatter stringFromDate:_creationDate];
_variables[kVariableKeySessionPID] = [@(getpid()) stringValue];
_variables[kVariableKeySessionAutoLogID] = [@(_autoLogId) stringValue];
}
[_textview setBadgeLabel:[self badgeLabel]];
}
 
Loading
Loading
@@ -1103,7 +1122,7 @@ ITERM_WEAKLY_REFERENCEABLE
[aSession setWindowTitle:title];
}
if ([aSession.profile[KEY_AUTOLOG] boolValue]) {
[aSession.shell startLoggingToFileWithPath:[aSession _autoLogFilenameForTermId:[aSession.sessionId stringByReplacingOccurrencesOfString:@":" withString:@"."]]
[aSession.shell startLoggingToFileWithPath:[aSession autoLogFilename]
shouldAppend:NO];
}
}
Loading
Loading
@@ -1513,17 +1532,15 @@ ITERM_WEAKLY_REFERENCEABLE
return [iTermPromptOnCloseReason profileAlwaysPrompts:_profile];
}
 
- (NSString *)_autoLogFilenameForTermId:(NSString *)termid {
// $(LOGDIR)/YYYYMMDD_HHMMSS.$(NAME).wNtNpN.$(PID).$(RANDOM).log
- (NSString *)autoLogFilename {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyyMMdd_HHmmss";
return [NSString stringWithFormat:@"%@/%@.%@.%@.%d.%0x.log",
[_profile objectForKey:KEY_LOGDIR],
[dateFormatter stringFromDate:[NSDate date]],
[_profile objectForKey:KEY_NAME],
termid,
(int)getpid(),
(int)arc4random()];
NSString *format = [iTermAdvancedSettingsModel autoLogFormat];
[self updateVariables];
NSString *name = [[format stringByReplacingVariableReferencesWithVariables:self.variables] stringByReplacingOccurrencesOfString:@"/" withString:@"__"];
NSString *filename = [[iTermProfilePreferences stringForKey:KEY_LOGDIR inProfile:_profile] stringByAppendingPathComponent:name];
DLog(@"Using autolog filename %@ from format %@ and variables %@", filename, format, self.variables);
return filename;
}
 
- (BOOL)shouldSetCtype {
Loading
Loading
@@ -1602,7 +1619,7 @@ ITERM_WEAKLY_REFERENCEABLE
env[@"ITERM_PROFILE"] = [_profile[KEY_NAME] stringByPerformingSubstitutions:substitutions];
}
if ([_profile[KEY_AUTOLOG] boolValue]) {
[_shell startLoggingToFileWithPath:[self _autoLogFilenameForTermId:[itermId stringByReplacingOccurrencesOfString:@":" withString:@"."]]
[_shell startLoggingToFileWithPath:[self autoLogFilename]
shouldAppend:NO];
}
@synchronized(self) {
Loading
Loading
Loading
Loading
@@ -182,5 +182,6 @@
+ (BOOL)drawOutlineAroundCursor;
+ (double)underlineCursorOffset;
+ (BOOL)logRestorableStateSize;
+ (NSString *)autoLogFormat;
 
@end
Loading
Loading
@@ -175,6 +175,11 @@ DEFINE_BOOL(runJobsInServers, YES, @"Session: Enable session restoration.\nSessi
DEFINE_BOOL(killJobsInServersOnQuit, YES, @"Session: User-initiated Quit (⌘Q) of iTerm2 will kill all running jobs.\nApplies only when session restoration is on.");
DEFINE_SETTABLE_BOOL(suppressRestartAnnouncement, SuppressRestartAnnouncement, NO, @"Session: Suppress the Restart Session offer.\nWhen a sessions terminates, it will offer to restart itself. Turn this on to suppress the offer permanently.");
DEFINE_BOOL(showSessionRestoredBanner, YES, @"Session: When restoring a session without restoring a running job, draw a banner saying “Session Restored” below the restored contents.");
DEFINE_STRING(autoLogFormat,
@"\\(session.creationTimeString).\\(session.name).\\(session.termid).\\(iterm2.pid).\\(session.autoLogId).log",
@"Session: Format for automatic session log filenames. See the Badges documentation for supported substitutions.");
 
#pragma mark - Window
DEFINE_BOOL(openFileInNewWindows, NO, @"Windows: Open files in new windows, not new tabs.\nThis affects shell scripts opened from Finder, for example.");
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment