summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/diagnostic/logging/LogFilesManager.java
blob: a581a15ed6b86cf7f5c87cff737f96c36e6a2997 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * 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.execution.configurations.LogFileOptions;
import com.intellij.execution.configurations.RunConfigurationBase;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.Alarm;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.io.File;
import java.util.*;

/**
 * User: anna
 * Date: 01-Feb-2006
 */
public class LogFilesManager implements Disposable {
  public static final Logger LOG = Logger.getInstance(LogFilesManager.class);

  private static final int UPDATE_INTERVAL = 500;

  private final Map<LogFileOptions, Set<String>> myLogFileManagerMap = new LinkedHashMap<LogFileOptions, Set<String>>();
  private final Runnable myUpdateRequest;
  private final LogConsoleManager myManager;
  private final Alarm myUpdateAlarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, this);
  private boolean myDisposed;

  public LogFilesManager(@NotNull final Project project, LogConsoleManager manager, Disposable parentDisposable) {
    myManager = manager;
    Disposer.register(parentDisposable, this);

    myUpdateRequest = new Runnable() {
      @Override
      public void run() {
        if (project.isDisposed() || myDisposed) return;
        myUpdateAlarm.cancelAllRequests();
        for (final LogFileOptions logFile : myLogFileManagerMap.keySet()) {
          final Set<String> oldFiles = myLogFileManagerMap.get(logFile);
          final Set<String> newFiles = logFile.getPaths(); // should not be called in UI thread
          myLogFileManagerMap.put(logFile, newFiles);

          final Set<String> obsoleteFiles = new THashSet<String>(oldFiles);
          obsoleteFiles.removeAll(newFiles);

          SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
              if (project.isDisposed() || myDisposed) return;

              addConfigurationConsoles(logFile, new Condition<String>() {
                @Override
                public boolean value(final String file) {
                  return !oldFiles.contains(file);
                }
              }, newFiles);
              for (String each : obsoleteFiles) {
                myManager.removeLogConsole(each);
              }
              myUpdateAlarm.addRequest(myUpdateRequest, UPDATE_INTERVAL);
            }
          });
        }
      }
    };
  }

  public void registerFileMatcher(@NotNull RunConfigurationBase runConfiguration) {
    final ArrayList<LogFileOptions> logFiles = runConfiguration.getAllLogFiles();
    for (LogFileOptions logFile : logFiles) {
      if (logFile.isEnabled()) {
        myLogFileManagerMap.put(logFile, logFile.getPaths());
      }
    }
    Alarm updateAlarm = myUpdateAlarm;
    if (updateAlarm != null) {
      updateAlarm.addRequest(myUpdateRequest, UPDATE_INTERVAL);
    }
  }

  @Override
  public void dispose() {
    myDisposed = true;
    if (myUpdateAlarm != null) {
      myUpdateAlarm.cancelAllRequests();
    }
  }

  public void initLogConsoles(@NotNull RunConfigurationBase base, ProcessHandler startedProcess) {
    List<LogFileOptions> logFiles = base.getAllLogFiles();
    for (LogFileOptions logFile : logFiles) {
      if (logFile.isEnabled()) {
        addConfigurationConsoles(logFile, Conditions.<String>alwaysTrue(), logFile.getPaths());
      }
    }
    base.createAdditionalTabComponents(myManager, startedProcess);
  }

  private void addConfigurationConsoles(final LogFileOptions logFile, Condition<String> shouldInclude, final Set<String> paths) {
    if (!paths.isEmpty()) {
      final TreeMap<String, String> title2Path = new TreeMap<String, String>();
      if (paths.size() == 1) {
        final String path = paths.iterator().next();
        if (shouldInclude.value(path)) {
          title2Path.put(logFile.getName(), path);
        }
      }
      else {
        for (String path : paths) {
          if (shouldInclude.value(path)) {
            String title = new File(path).getName();
            if (title2Path.containsKey(title)) {
              title = path;
            }
            title2Path.put(title, path);
          }
        }
      }
      for (final String title : title2Path.keySet()) {
        final String path = title2Path.get(title);
        myManager.addLogConsole(title, path, logFile.getCharset(), logFile.isSkipContent() ? new File(path).length() : 0);
      }
    }
  }
}