1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 package org.rocksdb;
3 
4 import org.junit.ClassRule;
5 import org.junit.Rule;
6 import org.junit.Test;
7 import org.junit.rules.TemporaryFolder;
8 import org.rocksdb.util.Environment;
9 
10 import java.io.IOException;
11 
12 import static java.nio.file.Files.readAllBytes;
13 import static java.nio.file.Paths.get;
14 import static org.assertj.core.api.Assertions.assertThat;
15 
16 public class InfoLogLevelTest {
17 
18   @ClassRule
19   public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
20       new RocksNativeLibraryResource();
21 
run(Module & M,ModuleAnalysisManager & AM)22   @Rule
23   public TemporaryFolder dbFolder = new TemporaryFolder();
24 
25   @Test
26   public void testInfoLogLevel() throws RocksDBException,
27       IOException {
28     try (final RocksDB db =
29              RocksDB.open(dbFolder.getRoot().getAbsolutePath())) {
30       db.put("key".getBytes(), "value".getBytes());
31       db.flush(new FlushOptions().setWaitForFlush(true));
32       assertThat(getLogContentsWithoutHeader()).isNotEmpty();
33     }
34   }
35 
36   @Test
37   public void testFatalLogLevel() throws RocksDBException,
38       IOException {
WriteBitcodePass()39     try (final Options options = new Options().
40         setCreateIfMissing(true).
41         setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
42          final RocksDB db = RocksDB.open(options,
WriteBitcodePass(raw_ostream & o,bool ShouldPreserveUseListOrder,bool EmitSummaryIndex,bool EmitModuleHash)43              dbFolder.getRoot().getAbsolutePath())) {
44       assertThat(options.infoLogLevel()).
45           isEqualTo(InfoLogLevel.FATAL_LEVEL);
46       db.put("key".getBytes(), "value".getBytes());
47       // As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
48       // content to be empty.
49       assertThat(getLogContentsWithoutHeader()).isEmpty();
50     }
getPassName() const51   }
52 
53   @Test
54   public void testFatalLogLevelWithDBOptions()
55       throws RocksDBException, IOException {
56     try (final DBOptions dbOptions = new DBOptions().
57         setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
58          final Options options = new Options(dbOptions,
59              new ColumnFamilyOptions()).
60              setCreateIfMissing(true);
61          final RocksDB db =
62              RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
63       assertThat(dbOptions.infoLogLevel()).
64           isEqualTo(InfoLogLevel.FATAL_LEVEL);
65       assertThat(options.infoLogLevel()).
66           isEqualTo(InfoLogLevel.FATAL_LEVEL);
67       db.put("key".getBytes(), "value".getBytes());
68       assertThat(getLogContentsWithoutHeader()).isEmpty();
69     }
70   }
71 
72   @Test(expected = IllegalArgumentException.class)
INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)73   public void failIfIllegalByteValueProvided() {
74     InfoLogLevel.getInfoLogLevel((byte) -1);
75   }
76 
77   @Test
78   public void valueOf() {
79     assertThat(InfoLogLevel.valueOf("DEBUG_LEVEL")).
80         isEqualTo(InfoLogLevel.DEBUG_LEVEL);
81   }
82 
83   /**
isBitcodeWriterPass(Pass * P)84    * Read LOG file contents into String.
85    *
86    * @return LOG file contents as String.
87    * @throws IOException if file is not found.
88    */
89   private String getLogContentsWithoutHeader() throws IOException {
90     final String separator = Environment.isWindows() ?
91         "\n" : System.getProperty("line.separator");
92     final String[] lines = new String(readAllBytes(get(
93         dbFolder.getRoot().getAbsolutePath() + "/LOG"))).split(separator);
94 
95     int first_non_header = lines.length;
96     // Identify the last line of the header
97     for (int i = lines.length - 1; i >= 0; --i) {
98       if (lines[i].indexOf("DB pointer") >= 0) {
99         first_non_header = i + 1;
100         break;
101       }
102     }
103     StringBuilder builder = new StringBuilder();
104     for (int i = first_non_header; i < lines.length; ++i) {
105       builder.append(lines[i]).append(separator);
106     }
107     return builder.toString();
108   }
109 }
110