summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java
blob: 1f8a48d7c605a89b7880533b5cf64f66d0afd87a (plain)
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
111
112
113
114
115
116
117
118
119
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.diagnostic.logging;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.charset.Charset;

/**
 * User: anna
 * Date: Apr 19, 2005
 */
public abstract class LogConsoleImpl extends LogConsoleBase {
  private final String myPath;
  @NotNull
  private final File myFile;
  @NotNull
  private final Charset myCharset;
  private long myOldLength = 0;

  /**
   * @deprecated use {@link #LogConsoleImpl(com.intellij.openapi.project.Project, java.io.File, java.nio.charset.Charset, long, String, boolean, com.intellij.psi.search.GlobalSearchScope)}
   */
  public LogConsoleImpl(Project project,
                        @NotNull File file,
                        @NotNull Charset charset,
                        long skippedContents,
                        @NotNull String title,
                        final boolean buildInActions) {
    this(project, file, charset, skippedContents, title, buildInActions, GlobalSearchScope.allScope(project));
  }

  public LogConsoleImpl(Project project,
                        @NotNull File file,
                        @NotNull Charset charset,
                        long skippedContents,
                        @NotNull String title,
                        final boolean buildInActions,
                        final GlobalSearchScope searchScope) {
    super(project, getReader(file, charset, skippedContents), title, buildInActions, new DefaultLogFilterModel(project),
          searchScope);
    myPath = file.getAbsolutePath();
    myFile = file;
    myCharset = charset;
  }

  @Nullable
  private static Reader getReader(@NotNull File file, @NotNull Charset charset, long skippedContents) {
    try {
      try {
        @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
        FileInputStream inputStream = new FileInputStream(file);
        //do not skip forward
        if (file.length() >= skippedContents) {
          long skipped = 0;
          while (skipped < skippedContents) {
            skipped += inputStream.skip(skippedContents - skipped);
          }
        }
        return new BufferedReader(new InputStreamReader(inputStream, charset));
      }
      catch (FileNotFoundException ignored) {
        if (FileUtilRt.createIfNotExists(file)) {
          return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
        }
        return null;
      }
    }
    catch (Throwable ignored) {
      return null;
    }
  }

  @Override
  @Nullable
  public String getTooltip() {
    return myPath;
  }

  public String getPath() {
    return myPath;
  }

  @Nullable
  @Override
  protected BufferedReader updateReaderIfNeeded(@Nullable BufferedReader reader) throws IOException {
    if (reader == null) {
      return null;
    }

    long length = myFile.length();
    if (length < myOldLength) {
      reader.close();
      //noinspection IOResourceOpenedButNotSafelyClosed
      reader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile), myCharset));
    }
    myOldLength = length;
    return reader;
  }
}