简体   繁体   中英

How read/Write from /Library/Logs of MAC

I want to create a file at /Libary/Logs in my Mac application.

I am using the following code,

NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSLocalDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Logs"];
//path = [path stringByAppendingPathComponent:@"e315Updater"];

if ( ![fm fileExistsAtPath:path] )
{
    BOOL sucess = [fm createDirectoryAtPath:path
 withIntermediateDirectories:YES
                  attributes:nil
                       error:&error];

    NSLog(@"Success value = %d",sucess);
    if ( error ){
        NSLog(@"Err = %@",[error description]);
    }
}

It keeps failing because of the permissions issue as every user does NOT have read/write permissions to this directory.

I also tried to set permissions on /Libary/Logs directory by using NSFilePosixPermissions but it failes to set the Read/write permissions also.

So I need help in figuring out that 'How to create and read/write to file inside Library/Logs of main Disk not the users Library'

The OS X permissions model doesn't allow normal users to write to most system directories, including /Library/Logs. If you find a way around this, you have found a security bug and it needs to be fixed (and then your app will fail); if you change the permissions (which you could do, if you elevate privileges first) you're breaking the OS X security model, which is a very bad idea .

Rather than trying to write directly to your own private log, I'd recommend going through the system logging facility via NSLog , or possibly one of the lower-level interfaces ASL or syslog . You won't get your own private log file, but it will actually work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM