aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/java/com/yahoo/log/VespaLevelControllerRepoTest.java
blob: 1086b99d55739286605b481af79682e04d39a26c (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 * @since 5.1
 */
@SuppressWarnings({"deprecation"})
public class VespaLevelControllerRepoTest {

    static int findControlString(RandomAccessFile f, String s) {
        try {
            String toFind = "\n." + s + ": ";
            byte[] contents = new byte[(int) f.length()];
            f.seek(0);
            f.read(contents);
            f.seek(0);
            String c_as_s = new String(contents, StandardCharsets.US_ASCII);
            int off = c_as_s.indexOf(toFind);
            if (off < 0) {
                System.err.println("did not find control line for level '"+s+"' in logcontrol file:");
                System.err.println(c_as_s);
                throw new RuntimeException("bad state in logcontrol file");
            }
            off += toFind.length();
            while ((off % 4) != 0) ++off;
            return off;
        } catch (IOException e) {
            throw new RuntimeException("problem reading logcontrol file", e);
        }
    }

    @Test
    public void testLogCtl () throws InterruptedException, IOException {
        File lcf = new File("./my-testilol-config-id.logcontrol");
        try {
            lcf.delete();
            Logger.getLogger("com.yahoo.log.test").setLevel(null);
            assertNull(Logger.getLogger("com.yahoo.log.test").getLevel());

            LevelControllerRepo repo = new VespaLevelControllerRepo(lcf.getName(), "all -debug -spam", "TST");

            long timeout = System.currentTimeMillis() + 60000;
            while (System.currentTimeMillis() < timeout) {
                if (Level.CONFIG == Logger.getLogger("com.yahoo.log.test").getLevel())
                    break;
                Thread.sleep(100);
            }

            RandomAccessFile lcfile = new RandomAccessFile(lcf, "rw");

            lcfile.seek(VespaLevelControllerRepo.controlFileHeaderLength+1);
            assertEquals(lcfile.readByte(), '\n');
            lcfile.seek(VespaLevelControllerRepo.controlFileHeaderLength+2);
            assertEquals(lcfile.readByte(), 'd');
            lcfile.seek(VespaLevelControllerRepo.controlFileHeaderLength+2 + 7);
            assertEquals(lcfile.readByte(), ':');
            assertEquals(0, (VespaLevelControllerRepo.controlFileHeaderLength+13) % 4);
            lcfile.seek(VespaLevelControllerRepo.controlFileHeaderLength + 13);
            assertEquals(0x20204f4e, lcfile.readInt());

            int off = findControlString(lcfile, "com.yahoo.log.test");
            lcfile.seek(off);
            assertEquals(0x20204f4e, lcfile.readInt());
            assertEquals(0x20204f4e, lcfile.readInt());
            assertEquals(0x20204f4e, lcfile.readInt());
            assertEquals(0x20204f4e, lcfile.readInt());
            assertEquals(0x20204f4e, lcfile.readInt());
            assertEquals(0x20204f4e, lcfile.readInt());

            assertEquals(0x204f4646, lcfile.readInt());
            assertEquals(0x204f4646, lcfile.readInt());
            lcfile.seek(off);
            lcfile.writeInt(0x204f4646);
            lcfile.writeInt(0x204f4646);
            lcfile.writeInt(0x204f4646);
            lcfile.writeInt(0x204f4646);
            lcfile.writeInt(0x204f4646);
            lcfile.writeInt(0x204f4646);

            lcfile.writeInt(0x20204f4e);
            lcfile.writeInt(0x20204f4e);
            lcfile.close();
            assertFalse(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.FATAL));
            assertFalse(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.ERROR));
            assertFalse(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.WARNING));
            assertFalse(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.INFO));
            assertFalse(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.EVENT));
            assertFalse(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.CONFIG));
            assertTrue(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.DEBUG));
            assertTrue(repo.getLevelController("com.yahoo.log.test").shouldLog(LogLevel.SPAM));
        } finally {
            lcf.delete();
        }
    }
}