/*
 * Licensed to David Pilato (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package fr.pilato.elasticsearch.crawler.fs.framework;


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Provides utility methods to read and write metadata and settings files
 */
public class MetaFileHandler {

    public final static Path DEFAULT_ROOT = Paths.get(System.getProperty("user.home"), ".fscrawler");

    private final Path root;

    protected MetaFileHandler(Path root) {
        this.root = root;
    }

    /**
     * Read a file in ~/.fscrawler/{subdir} dir
     * @param subdir subdir where we can read the file (null if we read in the root dir)
     * @param filename filename
     * @return The String UTF-8 content
     * @throws IOException in case of error while reading
     */
    protected String readFile(String subdir, String filename) throws IOException {
        Path dir = root;
        if (subdir != null) {
            dir = dir.resolve(subdir);
        }
        return Files.readString(dir.resolve(filename));
    }

    /**
     * Write a file in ~/.fscrawler/{subdir} dir
     * @param subdir subdir where we can read the file (null if we read in the root dir)
     * @param filename filename
     * @param content The String UTF-8 content to write
     * @throws IOException in case of error while reading
     */
    protected void writeFile(String subdir, String filename, String content) throws IOException {
        Path dir = root;
        if (subdir != null) {
            dir = dir.resolve(subdir);

            // If the dir does not exist, we need to create it
            if (Files.notExists(dir)) {
                Files.createDirectory(dir);
            }
        }
        Files.writeString(dir.resolve(filename), content);
    }

    /**
     * Remove a file from ~/.fscrawler/{subdir} dir
     * @param subdir subdir where we can read the file (null if we read in the root dir)
     * @param filename filename
     * @throws IOException in case of error while reading
     */
    protected void removeFile(String subdir, String filename) throws IOException {
        Path dir = root;
        if (subdir != null) {
            dir = dir.resolve(subdir);
        }
        Files.deleteIfExists(dir.resolve(filename));
    }
}

@@ -848,16 +848,101 @@
 work;%0A%0A%0A
+import org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0A
 import j
@@ -961,16 +961,16 @@
 eption;%0A
-
 import j
@@ -1159,16 +1159,102 @@
 dler %7B%0A%0A
+    private static final Logger logger = LogManager.getLogger(MetaFileHandler.class);%0A
     publ
@@ -1916,32 +1916,96 @@
 dir);%0A        %7D%0A
+        logger.trace(%22Reading file %7B%7D from %7B%7D%22, filename, dir);%0A
         return F
@@ -2707,32 +2707,94 @@
     %7D%0A        %7D%0A
+        logger.trace(%22Writing file %7B%7D to %7B%7D%22, filename, dir);%0A
         Files.wr
@@ -3259,32 +3259,32 @@
 esolve(subdir);%0A
-
         %7D%0A      
@@ -3269,32 +3269,107 @@
 dir);%0A        %7D%0A
+        logger.trace(%22Removing file %7B%7D from %7B%7D if exists%22, filename, dir);%0A
         Files.de

Add more loggers for meta files