Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't create log directory

When I test my application on iPad it run perfectly i.e. call the database , create folder and perform other task but in console it show me

2015-05-12 11:25:32.478 MyApp[291:19680] [PLLogging] Couldn't create log directory: The operation couldn’t be completed. (Cocoa error 513.).

When I search, found this answer NSFileManager creating folder (Cocoa error 513.)

But not abel to remove this.
Also when I shutdown and restart the iPad this line is not show

Question:

  1. Why i get this statement in console?
  2. The above statement can crash my app in future?
  3. How to remove Cocoa error 513 ?

Here is my code to calling the database

let fileManager = NSFileManager()
var Sourcepath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("DataBase.db");
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
let databaseStr = "DataBase.db"
let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
println(dbPath)    
if(fileManager .fileExistsAtPath(dbPath) == false) {

    var error:NSError?
    fileManager.copyItemAtPath(Sourcepath!, toPath: dbPath, error: &error)
    println(error)
}

and here is a functions which create a logs folder

func CreateLog(Log:String)
{
    autoreleasepool{

        var formatter:NSDateFormatter! = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd";
        var DateString = formatter.stringFromDate(NSDate()).stringByAppendingString("_tempLogs")
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss";
        var FileManager:NSFileManager! = NSFileManager.defaultManager()

        var LogFolder = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("Logs")
        let searchPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        let LogStr = "Logs"
        let LogFolderPath = searchPath.stringByAppendingPathComponent(LogStr)

        if(FileManager.fileExistsAtPath(LogFolderPath) == false)
        {
            var error:NSError?
            FileManager.createDirectoryAtPath(LogFolderPath, withIntermediateDirectories: true, attributes: nil, error: &error)

        }
        var LogPath = LogFolderPath.stringByAppendingPathComponent(DateString).stringByAppendingPathExtension("txt")
        var WriteString = (formatter.stringFromDate(NSDate()).stringByAppendingString(" ").stringByAppendingString(Log).stringByAppendingString("\n"));
        var Data = WriteString.dataUsingEncoding(NSUTF8StringEncoding)
        if(!FileManager.fileExistsAtPath(LogPath!))
        {
            FileManager.createFileAtPath(LogPath!, contents: Data, attributes: nil)
        }
        else
        {
            var output = NSFileHandle(forWritingAtPath: LogPath!);
            output?.seekToEndOfFile();
            output?.writeData(Data!);
            output?.closeFile()
            output = nil
        }
        formatter = nil
        FileManager = nil

    }

}
like image 619
Rizwan Shaikh Avatar asked May 12 '15 07:05

Rizwan Shaikh


4 Answers

The 513 is a permission issue (NSFileWriteNoPermissionError).

Try to println the LogFolderPath and double check if it is correct (or post it here the result).

D.

like image 97
mastro35 Avatar answered Nov 09 '22 09:11

mastro35


The problem is that you are trying to create a file in your app's bundle. The bundle is read-only in iOS (and you should treat it as read-only on Mac OS as well, even though it is possible to write to your app bundle in Mac OS.)

Your code will work on the simulator because changing the app bundle is allowed on Mac OS, and the simulator runs under Mac OS.

Change your code to use your documents directory, temp directory, or some other directory in your bundle and you should be fine.

like image 31
Duncan C Avatar answered Nov 09 '22 09:11

Duncan C


I've been thinking about this scenario, and I can't come up with any better suggestion than: Race Condition? Could it be that two treads are trying to log at the same time? The first one creates the folder directly after the second tread is passing the .fileExistsAtPath check?

  • T1: Folder? -> No
  • T2: Folder? -> No
  • T1: Create
  • T2: WTF? There is a folder here already?

If this is the case, it could solve the problem by moving the create directory check and logic to a earlier stage, like on App start or similar.

Not sure this is the issue or that this is the definite answer, so just let's call it a qualified guess... :)

like image 1
Mikael Hellman Avatar answered Nov 09 '22 10:11

Mikael Hellman


The format you using for getting date string is "yyyy-MM-dd HH:mm:ss" which introduces a space in the result string. Remove the space in the formatter so that it looks like this "yyyy-MM-ddHH:mm:ss" and try the above code or you could replace the space in the resulting path and write data to the file

like image 1
vineeth s thayyil Avatar answered Nov 09 '22 08:11

vineeth s thayyil