summaryrefslogtreecommitdiffstats
path: root/vespalog/src/main/java/com/yahoo/log/LogFileDb.java
blob: d0fa64805bf82ce173bf4d5c50df92ee8ae52522 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.*;

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

import static com.yahoo.vespa.defaults.Defaults.getDefaults;


/**
 * @author arnej
 *
 * This class takes care of saving meta-data about a log-file,
 * ensuring that we can enact policies about log retention.
 **/
public class LogFileDb {

    static final String DBDIR = "var/db/vespa/logfiledb/";

    private static long dayStamp() {
        long s = System.currentTimeMillis() / 1000;
        return s / 100000;
    }

    private static OutputStream metaFile() throws java.io.IOException {
        String fn = getDefaults().underVespaHome(DBDIR + "logfiles." + dayStamp());
        Path path = Paths.get(fn);
        return Files.newOutputStream(path, CREATE, APPEND);
    }

    public static void nowLoggingTo(String filename) {
        if (filename.contains("\n")) {
            throw new IllegalArgumentException("Cannot use filename with newline: "+filename);
        }
        long s = System.currentTimeMillis() / 1000;
        String meta = "" + s + " " + filename + "\n";
        byte[] data = meta.getBytes(UTF_8);
        try (OutputStream out = metaFile()) {
            out.write(data);
        } catch (java.io.IOException e) {
            System.err.println("Saving meta-data about logfile "+filename+" failed: "+e);
            // ignore
        }
    }
}