Merge branch 'charlesmchen/sendDebugLogToSelf'

pull/1/head
Matthew Chen 8 years ago
commit f1c42a5dbc

@ -2,13 +2,10 @@
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// //
@interface Pastelog : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate, UIAlertViewDelegate> @interface Pastelog : NSObject
typedef void (^successBlock)(NSError *error, NSString *urlString); typedef void (^successBlock)(NSError *error, NSString *urlString);
+(void)reportErrorAndSubmitLogsWithAlertTitle:(NSString*)alertTitle alertBody:(NSString*)alertBody;
+(void)reportErrorAndSubmitLogsWithAlertTitle:(NSString*)alertTitle alertBody:(NSString*)alertBody completionBlock:(successBlock)block;
+(void)submitLogs; +(void)submitLogs;
+(void)submitLogsWithCompletion:(successBlock)block; +(void)submitLogsWithCompletion:(successBlock)block;
+(void)submitLogsWithCompletion:(successBlock)block forFileLogger:(DDFileLogger*)fileLogger; +(void)submitLogsWithCompletion:(successBlock)block forFileLogger:(DDFileLogger*)fileLogger;

@ -3,44 +3,81 @@
// //
#import "Pastelog.h" #import "Pastelog.h"
#import "DebugLogger.h" #import "Signal-Swift.h"
#import "ThreadUtil.h"
#import <SignalMessaging/DebugLogger.h>
#import <SignalMessaging/Environment.h>
#import <SignalServiceKit/AppContext.h>
#import <SignalServiceKit/TSAccountManager.h>
#import <SignalServiceKit/TSContactThread.h>
#import <SignalServiceKit/TSStorageManager.h>
#import <SignalServiceKit/Threading.h>
#import <sys/sysctl.h> #import <sys/sysctl.h>
@interface Pastelog () @interface Pastelog () <NSURLConnectionDelegate, NSURLConnectionDataDelegate, UIAlertViewDelegate>
@property (nonatomic)UIAlertView *reportAlertView;
@property (nonatomic)UIAlertView *loadingAlertView;
@property (nonatomic)UIAlertView *submitAlertView;
@property (nonatomic)UIAlertView *infoAlertView;
@property (nonatomic)NSMutableData *responseData;
@property (nonatomic, copy)successBlock block;
@property (nonatomic, copy)NSString *gistURL;
@property (nonatomic) UIAlertController *loadingAlert;
@property (nonatomic) NSMutableData *responseData;
@property (nonatomic) successBlock block;
@end @end
@implementation Pastelog #pragma mark -
+(void)reportErrorAndSubmitLogsWithAlertTitle:(NSString*)alertTitle alertBody:(NSString*)alertBody { @implementation Pastelog
[self reportErrorAndSubmitLogsWithAlertTitle:alertTitle alertBody:alertBody completionBlock:nil];
}
+(void)reportErrorAndSubmitLogsWithAlertTitle:(NSString*)alertTitle alertBody:(NSString*)alertBody completionBlock:(successBlock)block {
Pastelog *sharedManager = [self sharedManager];
sharedManager.block = block;
sharedManager.reportAlertView = [[UIAlertView alloc] initWithTitle:alertTitle message:alertBody delegate:[self sharedManager] cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[sharedManager.reportAlertView show];
}
+(void)submitLogs { +(void)submitLogs {
Pastelog *sharedManager = [self sharedManager];
[self submitLogsWithCompletion:^(NSError *error, NSString *urlString) { [self submitLogsWithCompletion:^(NSError *error, NSString *urlString) {
if (!error) { if (!error) {
sharedManager.gistURL = urlString; UIAlertController *alert = [UIAlertController
sharedManager.submitAlertView = [[UIAlertView alloc] initWithTitle:@"One More Step" message:@"What would you like to do with the link to your debug log?" delegate:[self sharedManager] cancelButtonTitle:@"Open a Bug Report" otherButtonTitles:@"Email Support", @"Copy Link", nil]; alertControllerWithTitle:NSLocalizedString(@"DEBUG_LOG_ALERT_TITLE", @"Title of the debug log alert.")
[sharedManager.submitAlertView show]; message:NSLocalizedString(
@"DEBUG_LOG_ALERT_MESSAGE", @"Message of the debug log alert.")
preferredStyle:UIAlertControllerStyleAlert];
[alert
addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"DEBUG_LOG_ALERT_OPTION_EMAIL",
@"Label for the 'email debug log' option of the the debug log alert.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[Pastelog.sharedManager submitEmail:urlString];
}]];
[alert addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"DEBUG_LOG_ALERT_OPTION_COPY_LINK",
@"Label for the 'copy link' option of the the debug log alert.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:urlString];
}]];
#ifdef DEBUG
[alert addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"DEBUG_LOG_ALERT_OPTION_SEND_TO_SELF",
@"Label for the 'send to self' option of the the debug log alert.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[Pastelog.sharedManager sendToSelf:urlString];
}]];
#endif
[alert addAction:
[UIAlertAction
actionWithTitle:NSLocalizedString(@"DEBUG_LOG_ALERT_OPTION_BUG_REPORT",
@"Label for the 'Open a Bug Report' option of the the debug log alert.")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *_Nonnull action) {
[Pastelog.sharedManager prepareRedirection:urlString];
}]];
UIViewController *presentingViewController
= UIApplication.sharedApplication.frontmostViewControllerIgnoringAlerts;
[presentingViewController presentViewController:alert animated:NO completion:nil];
} else{ } else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failed to submit debug log" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DEBUG_LOG_FAILURE_ALERT_TITLE",
@"Title of the alert indicating the debug log upload failed.")
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView show]; [alertView show];
} }
}]; }];
@ -54,11 +91,13 @@
[self sharedManager].block = block; [self sharedManager].block = block;
[self sharedManager].loadingAlertView = [[UIAlertView alloc] initWithTitle:@"Sending debug log..." [self sharedManager].loadingAlert =
message:nil delegate:self [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DEBUG_LOG_ACTIVITY_INDICATOR",
cancelButtonTitle:nil @"Message indicating that the debug log is being uploaded.")
otherButtonTitles:nil]; message:nil
[[self sharedManager].loadingAlertView show]; preferredStyle:UIAlertControllerStyleAlert];
UIViewController *presentingViewController = UIApplication.sharedApplication.frontmostViewControllerIgnoringAlerts;
[presentingViewController presentViewController:[self sharedManager].loadingAlert animated:NO completion:nil];
NSArray<NSString *> *logFilePaths = DebugLogger.sharedLogger.allLogFilePaths; NSArray<NSString *> *logFilePaths = DebugLogger.sharedLogger.allLogFilePaths;
@ -133,16 +172,20 @@
} }
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.loadingAlertView dismissWithClickedButtonIndex:0 animated:YES]; [self.loadingAlert
dismissViewControllerAnimated:NO
NSError *error; completion:^{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error]; NSError *error;
if (!error) { NSDictionary *dict =
self.block(nil, [dict objectForKey:@"html_url"]); [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error];
} else{ if (!error) {
DDLogError(@"Error on debug response: %@", error); self.block(nil, [dict objectForKey:@"html_url"]);
self.block(error, nil); } else {
} DDLogError(@"Error on debug response: %@", error);
self.block(error, nil);
}
}];
self.loadingAlert = nil;
} }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
@ -151,44 +194,23 @@
if ( [httpResponse statusCode] != 201) { if ( [httpResponse statusCode] != 201) {
DDLogError(@"Failed to submit debug log: %@", httpResponse.debugDescription); DDLogError(@"Failed to submit debug log: %@", httpResponse.debugDescription);
[self.loadingAlertView dismissWithClickedButtonIndex:0 animated:YES]; [self.loadingAlert
[connection cancel]; dismissViewControllerAnimated:NO
self.block([NSError errorWithDomain:@"PastelogKit" code:10001 userInfo:@{}],nil); completion:^{
[connection cancel];
self.block([NSError errorWithDomain:@"PastelogKit" code:10001 userInfo:@{}], nil);
}];
self.loadingAlert = nil;
} }
} }
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.loadingAlertView dismissWithClickedButtonIndex:0 animated:YES]; [self.loadingAlert dismissViewControllerAnimated:NO
DDLogError(@"Uploading logs failed with error: %@", error); completion:^{
self.block(error,nil); DDLogError(@"Uploading logs failed with error: %@", error);
} self.block(error, nil);
}];
#pragma mark Alert View Delegates self.loadingAlert = nil;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == self.reportAlertView) {
if (buttonIndex == 0) {
if (self.block) {
[[self class] submitLogsWithCompletion:self.block];
} else{
[[self class] submitLogs];
}
} else{
// User declined, nevermind.
}
} else if (alertView == self.submitAlertView) {
if (buttonIndex == 0) {
[self prepareRedirection:self.gistURL];
} else if (buttonIndex == 1) {
[self submitEmail:self.gistURL];
} else {
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:self.gistURL];
}
} else if (alertView == self.infoAlertView) {
[UIApplication.sharedApplication openURL:[NSURL URLWithString:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"LOGS_URL"]]];
}
} }
#pragma mark Logs submission #pragma mark Logs submission
@ -204,8 +226,41 @@
- (void)prepareRedirection:(NSString*)url { - (void)prepareRedirection:(NSString*)url {
UIPasteboard *pb = [UIPasteboard generalPasteboard]; UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:url]; [pb setString:url];
self.infoAlertView = [[UIAlertView alloc]initWithTitle:@"GitHub redirection" message:@"The gist link was copied in your clipboard. You are about to be redirected to the GitHub issue list." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[self.infoAlertView show]; UIAlertController *alert =
[UIAlertController alertControllerWithTitle:NSLocalizedString(@"DEBUG_LOG_GITHUB_ISSUE_ALERT_TITLE",
@"Title of the alert before redirecting to Github Issues.")
message:NSLocalizedString(@"DEBUG_LOG_GITHUB_ISSUE_ALERT_MESSAGE",
@"Message of the alert before redirecting to Github Issues.")
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[UIApplication.sharedApplication
openURL:[NSURL URLWithString:[[NSBundle mainBundle]
objectForInfoDictionaryKey:@"LOGS_URL"]]];
}]];
UIViewController *presentingViewController = UIApplication.sharedApplication.frontmostViewControllerIgnoringAlerts;
[presentingViewController presentViewController:alert animated:NO completion:nil];
}
- (void)sendToSelf:(NSString *)url
{
if (![TSAccountManager isRegistered]) {
return;
}
NSString *recipientId = [TSAccountManager localNumber];
OWSMessageSender *messageSender = Environment.current.messageSender;
DispatchMainThreadSafe(^{
__block TSThread *thread = nil;
[[TSStorageManager sharedManager].dbReadWriteConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction];
}];
[ThreadUtil sendMessageWithText:url inThread:thread messageSender:messageSender];
});
} }
@end @end

@ -421,6 +421,36 @@
/* Title shown while the app is updating its database. */ /* Title shown while the app is updating its database. */
"DATABASE_VIEW_OVERLAY_TITLE" = "Updating Database"; "DATABASE_VIEW_OVERLAY_TITLE" = "Updating Database";
/* Message indicating that the debug log is being uploaded. */
"DEBUG_LOG_ACTIVITY_INDICATOR" = "Sending debug log...";
/* Message of the debug log alert. */
"DEBUG_LOG_ALERT_MESSAGE" = "What would you like to do with the link to your debug log?";
/* Label for the 'Open a Bug Report' option of the the debug log alert. */
"DEBUG_LOG_ALERT_OPTION_BUG_REPORT" = "Open a Bug Report";
/* Label for the 'copy link' option of the the debug log alert. */
"DEBUG_LOG_ALERT_OPTION_COPY_LINK" = "Copy Link";
/* Label for the 'email debug log' option of the the debug log alert. */
"DEBUG_LOG_ALERT_OPTION_EMAIL" = "Email Support";
/* Label for the 'send to self' option of the the debug log alert. */
"DEBUG_LOG_ALERT_OPTION_SEND_TO_SELF" = "Send to Self";
/* Title of the debug log alert. */
"DEBUG_LOG_ALERT_TITLE" = "One More Step";
/* Title of the alert indicating the debug log upload failed. */
"DEBUG_LOG_FAILURE_ALERT_TITLE" = "Failed to submit debug log";
/* Message of the alert before redirecting to Github Issues. */
"DEBUG_LOG_GITHUB_ISSUE_ALERT_MESSAGE" = "The gist link was copied in your clipboard. You are about to be redirected to the GitHub issue list.";
/* Title of the alert before redirecting to Github Issues. */
"DEBUG_LOG_GITHUB_ISSUE_ALERT_TITLE" = "GitHub redirection";
/* {{Short Date}} when device last communicated with Signal Server. */ /* {{Short Date}} when device last communicated with Signal Server. */
"DEVICE_LAST_ACTIVE_AT_LABEL" = "Last active: %@"; "DEVICE_LAST_ACTIVE_AT_LABEL" = "Last active: %@";

Loading…
Cancel
Save