Skip to content

Commit

Permalink
Optimize logging and enable structured info in ASL. Logging UI forthc…
Browse files Browse the repository at this point in the history
…oming
  • Loading branch information
Michael Gorbach committed Mar 20, 2008
1 parent 2926854 commit 0cddf55
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 118 deletions.
2 changes: 1 addition & 1 deletion MFClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ - (void)fillInitialStatus
if (fs)
[self storeFilesystem: fs];
else
MFLogS(self, @"Could not init client fs from server fs %@", remoteFS);
MFLogSO(self, remoteFS, @"Could not init client fs from server fs %@", remoteFS);
}

// Fill Recents
Expand Down
2 changes: 1 addition & 1 deletion MFCommunicationServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ - (id) init
- (void)vendDisributedObject
{
NSConnection* connection = [NSConnection defaultConnection];
// TODO: Vend a proxy to set up protocol instead of, um , everything
// TODO: Vend a proxy to set up protocol
[connection setRootObject:self];
if ([connection registerName:kMFDistributedObjectName] == YES)
{
Expand Down
6 changes: 3 additions & 3 deletions MFFilesystemController.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ - (MFServerFS*)newFilesystemWithPlugin:(MFServerPlugin*)plugin
}
else
{
MFLogS(self, @"Failed to create new filesystem with plugin %@",
MFLogSO(self, plugin, @"Failed to create new filesystem with plugin %@",
plugin);
return nil;
}
Expand Down Expand Up @@ -395,8 +395,8 @@ - (NSString*)tokenForFilesystem:(MFServerFS*)fs
NSString* tokenString = [(NSString *)string autorelease];
if ([[tokens allValues] containsObject: fs])
{
MFLogS(self, @"Uh oh ... adding a second token for an FS already in tokens");
MFLogS(self, @"Tokens Before %@", tokens);
MFLogSO(self, fs, @"Uh oh ... adding a second token for an FS already in tokens");
// MFLogS(self, @"Tokens Before %@", tokens);
}

[tokens setObject: fs forKey: tokenString];
Expand Down
20 changes: 17 additions & 3 deletions MFLoggingController.h → MFLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
// limitations under the License.

#import <Cocoa/Cocoa.h>
#import <asl.h>

#define MF_ASL_SERVICE_NAME "org.mgorbach.macfusion"
#define ASL_KEY_SUBSYSTEM "Subsystem"
#define ASL_KEY_UUID "UUID"


enum MFLogType {
kMFLogTypeCore,
kMFLogTypeEvent,
Expand All @@ -24,14 +31,21 @@ enum MFLogType {
void MFLogP(int type, NSString* format, ...);
void MFLog(NSString* format, ...);
void MFPrint(NSString* format, ...);
void MFLogS(id sender, NSString* format, ...);
void MFLogSO(id sender, id object, NSString* format, ...);

@interface MFLoggingController : NSObject {
@interface MFLogging : NSObject {
NSFileHandle* fileHandle;
BOOL stdOut;
aslclient aslClient;
int fd;
}

+ (MFLoggingController*)sharedController;
- (void)logMessage:(NSString*)message ofType:(int)type sender:(id)sender;
+ (MFLogging*)sharedLogging;
- (void)logMessage:(NSString*)message
ofType:(NSInteger)type
object:(id)object
sender:(id)sender;
- (void)setPrintToStandardOut:(BOOL)b;

@end
130 changes: 63 additions & 67 deletions MFLoggingController.m → MFLogging.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "MFLoggingController.h"
#import "MFLogging.h"
#import "MFFilesystem.h"

#define LOG_FILE_PATH @"~/Library/Logs/MacFusion2.log"

// Print to logging system
void MFLog(NSString* format, ...)
{
MFLoggingController* logger = [MFLoggingController sharedController];
MFLogging* logger = [MFLogging sharedLogging];

// get a reference to the arguments on the stack that follow
// the format paramter
Expand All @@ -33,15 +35,15 @@ void MFLog(NSString* format, ...)
string = [[NSString alloc] initWithFormat: format
arguments: argList];
va_end (argList);
[logger logMessage:string ofType:kMFLogTypeCore sender:nil];
[logger logMessage:string ofType:0 object: nil sender:@"MFCORE"];

[string release];
}


void MFLogP(int type, NSString* format, ...)
{
MFLoggingController* logger = [MFLoggingController sharedController];
MFLogging* logger = [MFLogging sharedLogging];

// get a reference to the arguments on the stack that follow
// the format paramter
Expand All @@ -54,14 +56,34 @@ void MFLogP(int type, NSString* format, ...)
string = [[NSString alloc] initWithFormat: format
arguments: argList];
va_end (argList);
[logger logMessage:string ofType:type sender:nil];
[logger logMessage:string ofType:type object: nil sender:nil];

[string release];
}

void MFLogS(id sender, NSString* format, ...)
{
MFLoggingController* logger = [MFLoggingController sharedController];
MFLogging* logger = [MFLogging sharedLogging];

// get a reference to the arguments on the stack that follow
// the format paramter
va_list argList;
va_start (argList, format);

// NSString luckily provides us with this handy method which
// will do all the work for us, including %@
NSString *string;
string = [[NSString alloc] initWithFormat: format
arguments: argList];
va_end (argList);
[logger logMessage:string ofType:0 object: nil sender:sender];

[string release];
}

void MFLogSO(id sender, id object, NSString* format, ...)
{
MFLogging* logger = [MFLogging sharedLogging];

// get a reference to the arguments on the stack that follow
// the format paramter
Expand All @@ -74,7 +96,7 @@ void MFLogS(id sender, NSString* format, ...)
string = [[NSString alloc] initWithFormat: format
arguments: argList];
va_end (argList);
[logger logMessage:string ofType:0 sender:sender];
[logger logMessage:string ofType:0 object:object sender:sender];

[string release];
}
Expand All @@ -99,92 +121,65 @@ void MFPrint(NSString* format, ...)
}


@implementation MFLoggingController
@implementation MFLogging

static MFLoggingController* sharedController = nil;
static MFLogging* sharedLogging = nil;

+ (MFLoggingController*) sharedController
+ (MFLogging*) sharedLogging
{
if (sharedController == nil)
if (sharedLogging == nil)
[[self alloc] init];

return sharedController;
return sharedLogging;
}

+ (id)allocWithZone:(NSZone*)zone
{
if (sharedController == nil)
if (sharedLogging == nil)
{
sharedController = [super allocWithZone:zone];
return sharedController;
sharedLogging = [super allocWithZone:zone];
return sharedLogging;
}

return nil;
}

- (void)registerNotifications
{
// We need notifications here, but what about the differnece between
// client and server processes?
}

- (void)init
{
// Nothing here yet
fd = -1;
stdOut = YES;
}

- (NSString*)descriptionForObject:(id)object
- (void)setupLogFile
{
if (object == nil)
{
return @"NILL";
}
if (stdOut)
aslClient = asl_open(NULL, MF_ASL_SERVICE_NAME, ASL_OPT_STDERR);
else
{
return [object description];
}
}

- (NSFileHandle*)handleForLogfile
{
NSFileManager* fm = [NSFileManager defaultManager];
NSString* filePath = [ LOG_FILE_PATH stringByExpandingTildeInPath ];

if (![fm fileExistsAtPath:filePath])
{
[fm createFileAtPath:filePath contents:nil attributes:nil];
}
aslClient = asl_open(NULL, MF_ASL_SERVICE_NAME, 0);

fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
return fileHandle;
}

- (void)logMessageToFile:(NSString*)message ofType:(int)type sender:(id)sender
{
NSString* description = [self descriptionForObject: sender];
NSString* writeString = [NSString stringWithFormat: @"%@: %@\n",
description, message];

NSFileHandle* handle = [self handleForLogfile];
[handle truncateFileAtOffset: [fileHandle seekToEndOfFile]];
[handle writeData: [writeString dataUsingEncoding:NSUTF8StringEncoding]];
[handle synchronizeFile];
fd = open( [[LOG_FILE_PATH stringByExpandingTildeInPath] cStringUsingEncoding: NSUTF8StringEncoding],
O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR );
asl_add_log_file(aslClient, fd);
asl_set_filter(aslClient, ASL_FILTER_MASK_UPTO(ASL_LEVEL_INFO));
}

- (void)logMessage:(NSString*)message ofType:(int)type sender:(id)sender
- (void)logMessage:(NSString*)message
ofType:(NSInteger)type
object:(id)object
sender:(id)sender
{
if (fd == -1)
[self setupLogFile];

message = [message stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self logMessageToFile:message ofType:type sender:sender];
if (stdOut)
{
if (!sender)
printf("%s\n", [message cStringUsingEncoding:NSUTF8StringEncoding]);
else
printf("%s: %s\n", [[sender description] cStringUsingEncoding:NSUTF8StringEncoding],
[message cStringUsingEncoding: NSUTF8StringEncoding]);
}

aslmsg m = asl_new(ASL_TYPE_MSG);
asl_set(m, ASL_KEY_FACILITY, MF_ASL_SERVICE_NAME);
if ([sender isKindOfClass: [MFFilesystem class]])
asl_set(m, ASL_KEY_UUID, [[(MFFilesystem*)sender uuid] cStringUsingEncoding: NSUTF8StringEncoding]);
if ([object isKindOfClass: [MFFilesystem class]])
asl_set(m, ASL_KEY_UUID, [[(MFFilesystem*)object uuid] cStringUsingEncoding: NSUTF8StringEncoding]);
asl_set(m, ASL_KEY_SUBSYSTEM, [[sender description] cStringUsingEncoding: NSUTF8StringEncoding]);
asl_log(aslClient, m, ASL_LEVEL_NOTICE, [message cStringUsingEncoding: NSUTF8StringEncoding]);
return;
}

Expand All @@ -195,7 +190,8 @@ - (void)setPrintToStandardOut:(BOOL)b

- (void)finalize
{
[fileHandle closeFile];
asl_close(aslClient);
close(fd);
[super finalize];
}

Expand Down
2 changes: 1 addition & 1 deletion MFMainController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#import "MFFilesystem.h"
#import "MFCommunicationServer.h"
#include <sys/xattr.h>
#import "MFLoggingController.h"
#import "MFLogging.h"
#import "MFConstants.h"

@implementation MFMainController
Expand Down
Loading

0 comments on commit 0cddf55

Please sign in to comment.