Skip to content

Commit

Permalink
Enhanced info and debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 15, 2015
1 parent d65ac35 commit 13cd3e9
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 33 deletions.
6 changes: 6 additions & 0 deletions NTFS-OSX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
13CDB3BA1B1FF08E00A8A374 /* NTFS_OSXTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 13CDB3B91B1FF08E00A8A374 /* NTFS_OSXTests.m */; };
13CDB3C71B1FFA2D00A8A374 /* NTFSApp.m in Sources */ = {isa = PBXBuildFile; fileRef = 13CDB3C61B1FFA2D00A8A374 /* NTFSApp.m */; };
13CDB3CA1B1FFD4E00A8A374 /* ntfs_osx.png in Resources */ = {isa = PBXBuildFile; fileRef = 13CDB3C91B1FFD4E00A8A374 /* ntfs_osx.png */; };
9C1830511B2F6A640055E01D /* Logger.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C1830501B2F6A640055E01D /* Logger.m */; };
9C1DA1871B26597000B596CB /* LaunchService.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C1DA1861B26597000B596CB /* LaunchService.m */; };
9C5A30121B28FFD100BE77C2 /* ntfs_osx_small.png in Resources */ = {isa = PBXBuildFile; fileRef = 9C5A30111B28FFD100BE77C2 /* ntfs_osx_small.png */; };
9C8CA2A81B275AF5009D4DA5 /* ServiceManagement.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C8CA2A71B275AF5009D4DA5 /* ServiceManagement.framework */; };
Expand Down Expand Up @@ -61,6 +62,8 @@
13CDB3C61B1FFA2D00A8A374 /* NTFSApp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NTFSApp.m; sourceTree = "<group>"; };
13CDB3C81B1FFA6C00A8A374 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainMenu.xib; sourceTree = "<group>"; };
13CDB3C91B1FFD4E00A8A374 /* ntfs_osx.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ntfs_osx.png; sourceTree = "<group>"; };
9C18304F1B2F6A640055E01D /* Logger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Logger.h; sourceTree = "<group>"; };
9C1830501B2F6A640055E01D /* Logger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Logger.m; sourceTree = "<group>"; };
9C1DA1851B26597000B596CB /* LaunchService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LaunchService.h; sourceTree = "<group>"; };
9C1DA1861B26597000B596CB /* LaunchService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LaunchService.m; sourceTree = "<group>"; };
9C5A30111B28FFD100BE77C2 /* ntfs_osx_small.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ntfs_osx_small.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -184,6 +187,8 @@
9C1DA1861B26597000B596CB /* LaunchService.m */,
9CC369F01B27719E002D535B /* Constants.h */,
9CC369F11B27719E002D535B /* Constants.m */,
9C18304F1B2F6A640055E01D /* Logger.h */,
9C1830501B2F6A640055E01D /* Logger.m */,
);
name = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -313,6 +318,7 @@
9CC369F21B27719E002D535B /* Constants.m in Sources */,
13824BCD1B21DD2F00EEE019 /* Arbitration.m in Sources */,
13CDB3A91B1FF08D00A8A374 /* main.m in Sources */,
9C1830511B2F6A640055E01D /* Logger.m in Sources */,
13904C5F1B23F7AA0055253A /* STPrivilegedTask.m in Sources */,
13824BC71B215C2700EEE019 /* Preferences.m in Sources */,
13CDB3C71B1FFA2D00A8A374 /* NTFSApp.m in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions NTFS-OSX/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ @interface AppDelegate (NFTSOSX)
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSLog(@"Loading...");
LogInfo(@"Loading...");

app = [NTFSApp new];

NSLog(@"Loaded successfully.");
LogInfo(@"Loaded successfully.");
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
NSLog(@"App will terminate.");
LogInfo(@"App will terminate.");
}

@end
26 changes: 13 additions & 13 deletions NTFS-OSX/Arbitration.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void RegisterDA(void) {
return;
}

NSLog(@"Disk Arbitration Session created");
LogDebug(@"Disk Arbitration Session created");

ntfsDisks = [NSMutableSet new];

Expand Down Expand Up @@ -77,11 +77,11 @@ void RegisterDA(void) {
// Disk Arbitration Approval Session
approvalSession = DAApprovalSessionCreate(kCFAllocatorDefault);
if (!approvalSession) {
NSLog(@"Unable to create Disk Arbitration approval session.");
LogDebug(@"Unable to create Disk Arbitration approval session.");
return;
}

NSLog(@"Disk Arbitration Approval Session created");
LogDebug(@"Disk Arbitration Approval Session created");
DAApprovalSessionScheduleWithRunLoop(approvalSession, CFRunLoopGetMain(), kCFRunLoopCommonModes);

// Same match condition for Approval session too
Expand All @@ -99,7 +99,7 @@ void UnregisterDA(void) {
DASessionUnscheduleFromRunLoop(session, CFRunLoopGetMain(), kCFRunLoopCommonModes);
Release(session);

NSLog(@"Disk Arbitration Session destoryed");
LogDebug(@"Disk Arbitration Session destoryed");
}

// DA Approval Session
Expand All @@ -109,7 +109,7 @@ void UnregisterDA(void) {
DAApprovalSessionUnscheduleFromRunLoop(approvalSession, CFRunLoopGetMain(), kCFRunLoopCommonModes);
Release(approvalSession);

NSLog(@"Disk Arbitration Approval Session destoryed");
LogDebug(@"Disk Arbitration Approval Session destoryed");
}

[ntfsDisks removeAllObjects];
Expand All @@ -126,48 +126,48 @@ BOOL Validate(DADiskRef diskRef) {
}

void DiskAppearedCallback(DADiskRef diskRef, void *context) {
NSLog(@"DiskAppearedCallback called: %s", DADiskGetBSDName(diskRef));
LogDebug(@"DiskAppearedCallback called: %s", DADiskGetBSDName(diskRef));

if (Validate(diskRef)) {
Disk *disk = [[Disk alloc] initWithDADiskRef:diskRef];
NSLog(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
LogDebug(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);

[[NSNotificationCenter defaultCenter] postNotificationName:NTFSDiskAppearedNotification object:disk];
}
}

void DiskDisappearedCallback(DADiskRef diskRef, void *context) {
NSLog(@"DiskDisappearedCallback called: %s", DADiskGetBSDName(diskRef));
LogDebug(@"DiskDisappearedCallback called: %s", DADiskGetBSDName(diskRef));

if (Validate(diskRef)) {
Disk *disk = [Disk getDiskForDARef:diskRef];
NSLog(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
LogDebug(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);

[[NSNotificationCenter defaultCenter] postNotificationName:NTFSDiskDisappearedNotification object:disk];
}
}

void DiskDescriptionChangedCallback(DADiskRef diskRef, CFArrayRef keys, void *context) {
NSLog(@"DiskDescriptionChangedCallback called: %s", DADiskGetBSDName(diskRef));
LogDebug(@"DiskDescriptionChangedCallback called: %s", DADiskGetBSDName(diskRef));

Disk *disk = [Disk getDiskForDARef:diskRef];

if (disk) {
CFDictionaryRef newDesc = DADiskCopyDescription(diskRef);
disk.desc = newDesc;

NSLog(@"Updated Disk Description: %@", disk.desc);
LogDebug(@"Updated Disk Description: %@", disk.desc);

Release(newDesc);
}
}

DADissenterRef DiskMountApprovalCallback(DADiskRef diskRef, void *context) {
NSLog(@"DiskMountApprovalCallback called: %s", DADiskGetBSDName(diskRef));
LogDebug(@"DiskMountApprovalCallback called: %s", DADiskGetBSDName(diskRef));

if (Validate(diskRef)) {
Disk *disk = [[Disk alloc] initWithDADiskRef:diskRef];
NSLog(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
LogDebug(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
}

return NULL;
Expand Down
2 changes: 1 addition & 1 deletion NTFS-OSX/CommandLine.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ + (NSString *)run:(NSString *)command {
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *file = [pipe fileHandleForReading];

NSLog(@"Running command: %@",command);
LogDebug(@"Running command: %@",command);

[task setLaunchPath: @"/bin/sh"];
[task setArguments: [NSArray arrayWithObjects: @"-c", command, nil]];
Expand Down
5 changes: 4 additions & 1 deletion NTFS-OSX/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ extern NSString * const AppBundleID;
extern NSString * const AppStatusBarIconName;

extern NSString * const FstabFile;
extern NSString * const Yes;
extern NSString * const No;

extern NSString * const NSDevicePath;
extern NSString * const DADiskDescriptionVolumeKindValue;

extern NSString * const NTFSDiskAppearedNotification;
extern NSString * const NTFSDiskDisappearedNotification;

extern NSString * const PrefsLaunchAtLogin;
extern NSString * const PrefsLaunchAtLogin;
extern NSString * const PrefsDebugMode;
3 changes: 3 additions & 0 deletions NTFS-OSX/Constants.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
NSString * const AppStatusBarIconName = @"ntfs_osx.png";

NSString * const FstabFile = @"/etc/fstab";
NSString * const Yes = @"Yes";
NSString * const No = @"No";

NSString * const NSDevicePath = @"NSDevicePath";
NSString * const DADiskDescriptionVolumeKindValue = @"ntfs";
Expand All @@ -47,3 +49,4 @@
NSString * const NTFSDiskDisappearedNotification = @"NTFSDiskDisappearedNotification";

NSString * const PrefsLaunchAtLogin = @"LaunchAtLogin";
NSString * const PrefsDebugMode = @"DebugMode";
6 changes: 3 additions & 3 deletions NTFS-OSX/Disk.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ - (void)enableNTFSWrite {
NSString *cmd = [NSString stringWithFormat:@"echo \"%@\" | tee -a %@", [self ntfsConfig], FstabFile];

[STPrivilegedTask launchedPrivilegedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects: @"-c", cmd, nil]];
NSLog(@"NTFS write mode enabled for disk '%@'", self.volumeName);
LogInfo(@"Write mode enabled for disk '%@'", self.volumeName);
}

- (void)mount {
Expand Down Expand Up @@ -162,12 +162,12 @@ - (NSString *)volumePath {

- (BOOL)isNTFSWritable {
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:FstabFile];
NSLog(@"File '%@' exists: %@", FstabFile, status ? @"YES" : @"NO");
LogDebug(@"File '%@' exists: %@", FstabFile, status ? @"YES" : @"NO");

if (status) {
NSString *cmd = [NSString stringWithFormat:@"grep \"%@\" %@", volumeUUID, FstabFile];
NSString *output = [CommandLine run:cmd];
NSLog(@"output: %@", output);
LogDebug(@"output: %@", output);

if ([[self ntfsConfig] isEqualToString:output]) {
return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion NTFS-OSX/LaunchService.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ LSSharedFileListItemRef InsertItemURL(LSSharedFileListRef inList,

OSStatus RemoveItemFromList(LSSharedFileListRef inList, LSSharedFileListItemRef item) {
OSStatus status = LSSharedFileListItemRemove(inList, item);
NSLog(@"RemoveItemFromList Status: %d", (int)status);
LogDebug(@"RemoveItemFromList Status: %d", (int)status);

return status;
}
Expand Down
17 changes: 17 additions & 0 deletions NTFS-OSX/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Logger.h
// NTFS-OSX
//
// Created by Jeevanandam M. on 6/15/15.
// Copyright (c) 2015 myjeeva.com. All rights reserved.
//

#define LogInfo(s, ...) NSLog(@"%@", [NSString stringWithFormat:(s), ##__VA_ARGS__])
#define LogDebug(s, ...) [Logger logFor:__FILE__ line:__LINE__ format:(s), ##__VA_ARGS__]

@interface Logger : NSObject

+ (void)setDebugLog:(BOOL)debugOn;
+ (void)logFor:(char *)srcFile line:(int)line format:(NSString *)format, ...;

@end
40 changes: 40 additions & 0 deletions NTFS-OSX/Logger.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Logger.m
// NTFS-OSX
//
// Created by Jeevanandam M. on 6/15/15.
// Copyright (c) 2015 myjeeva.com. All rights reserved.
//
// Inspried by http://borkware.com/rants/agentm/mlog/


#import "Logger.h"

static BOOL __DebugLogOn = NO;

@implementation Logger

+ (void)setDebugLog:(BOOL)debugOn {
__DebugLogOn = debugOn;
}

+ (void)logFor:(char *)srcFile line:(int)line format:(NSString *)format, ... {
if (__DebugLogOn == NO) {
return;
}

va_list ap;
NSString *file;
NSString *msg;

va_start(ap, format);
file = [[NSString stringWithUTF8String:srcFile] lastPathComponent];
msg = [NSString stringWithFormat:format, ap];
va_end(ap);

NSLog(@"[%@:%d] %@", file, line, msg);

return;
}

@end
1 change: 1 addition & 0 deletions NTFS-OSX/NTFS-OSX-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@import Foundation;

#import "Constants.h"
#import "Logger.h"

#endif

Expand Down
29 changes: 18 additions & 11 deletions NTFS-OSX/NTFSApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ @implementation NTFSApp

- (id)init {
if (self = [super init]) {
// registering default preferences
[self registarDefaults];
NSLog(@"Is NTFS OS X App launch on login: %@", GetDefaultBool(PrefsLaunchAtLogin) ? @"YES" : @"NO");

BOOL debugEnabled = GetDefaultBool(PrefsDebugMode);
[Logger setDebugLog:debugEnabled];

LogInfo(@"Debug enabled - %@", debugEnabled ? Yes : No);
LogInfo(@"Launch on login - %@", GetDefaultBool(PrefsLaunchAtLogin) ? Yes : No);

// Initializing Status Bar and Menus
[self initStatusBar];
Expand Down Expand Up @@ -112,11 +118,11 @@ - (void)quitMenuClicked:(id)sender {
- (void)ntfsDiskAppeared:(NSNotification *)notification {
Disk *disk = notification.object;

NSLog(@"ntfsDiskAppeared called - %@", disk.BSDName);
NSLog(@"NTFS Disks Count: %lu", (unsigned long)[ntfsDisks count]);
LogInfo(@"DiskAppeared - %@", disk.BSDName);
LogDebug(@"Disks Count - %lu", (unsigned long)[ntfsDisks count]);

if (disk.isNTFSWritable) {
NSLog(@"NTFS write mode already enabled for '%@'", disk.volumeName);
LogInfo(@"Write mode already enabled for '%@'", disk.volumeName);
} else {
NSString *msgText = [NSString stringWithFormat:@"Disk detected: %@", disk.volumeName];

Expand All @@ -139,7 +145,7 @@ - (void)ntfsDiskAppeared:(NSNotification *)notification {
NSString *volumePath = disk.volumePath;
BOOL isExits = [[NSFileManager defaultManager] fileExistsAtPath:volumePath];
if (isExits) {
NSLog(@"Opening mounted NTFS Volume '%@'", volumePath);
LogDebug(@"Opening mounted NTFS Volume '%@'", volumePath);
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:volumePath]];

[self notifyUser:[NSString stringWithFormat:@"NTFS write enabled for '%@'.", disk.volumeName]];
Expand All @@ -149,7 +155,7 @@ - (void)ntfsDiskAppeared:(NSNotification *)notification {
- (void)ntfsDiskDisappeared:(NSNotification *)notification {
Disk *disk = notification.object;

NSLog(@"ntfsDiskDisappeared called - %@", disk.BSDName);
LogInfo(@"DiskDisappeared - %@", disk.BSDName);

[disk disappeared];
}
Expand All @@ -158,10 +164,10 @@ - (void)volumeMountNotification:(NSNotification *) notification {
Disk *disk = [Disk getDiskForUserInfo:notification.userInfo];

if (disk) {
NSLog(@"NTFS Disk: '%@' mounted at '%@'", disk.BSDName, disk.volumePath);
LogDebug(@"NTFS Disk: '%@' mounted at '%@'", disk.BSDName, disk.volumePath);

disk.favoriteItem = AddPathToFinderFavorites(disk.volumePath);
NSLog(@"Added path to favorties");
LogDebug(@"Added path to favorties");
}

}
Expand All @@ -170,11 +176,11 @@ - (void)volumeUnmountNotification:(NSNotification *) notification {
Disk *disk = [Disk getDiskForUserInfo:notification.userInfo];

if (disk) {
NSLog(@"NTFS Disk: '%@' unmounted from '%@'", disk.BSDName, disk.volumePath);
LogDebug(@"NTFS Disk: '%@' unmounted from '%@'", disk.BSDName, disk.volumePath);

if (disk.favoriteItem) {
RemoveItemFromFinderFavorties((LSSharedFileListItemRef)disk.favoriteItem);
NSLog(@"Removed path from favorties");
LogDebug(@"Removed path from favorties");
}
}
}
Expand Down Expand Up @@ -226,7 +232,7 @@ - (void)initStatusBar {
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
statusItem.highlightMode = YES;
statusItem.image = [NSImage imageNamed:AppStatusBarIconName];
statusItem.toolTip = @"Enable native option of NTFS Write on Mac OSX";
statusItem.toolTip = @"Enable native option of NTFS Write on Mac OS X";
statusItem.menu = statusMenu;
statusItem.title = @"";
[statusItem.image setTemplate:YES];
Expand Down Expand Up @@ -259,6 +265,7 @@ - (void)bringToFront {
- (void)registarDefaults {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], PrefsLaunchAtLogin,
[NSNumber numberWithBool:NO], PrefsDebugMode,
nil];

[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
Expand Down

0 comments on commit 13cd3e9

Please sign in to comment.