SSR: script log first pass
[idea/community.git] / platform / structuralsearch / source / com / intellij / structuralsearch / impl / matcher / predicates / ScriptLog.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.structuralsearch.impl.matcher.predicates;
17
18 import com.intellij.notification.NotificationGroup;
19 import com.intellij.notification.NotificationType;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.structuralsearch.SSRBundle;
22
23 /**
24  * @author Bas Leijdekkers
25  */
26 public class ScriptLog {
27
28   private final NotificationGroup myEventLog;
29   private final Project myProject;
30
31   public ScriptLog(Project project) {
32     myProject = project;
33     myEventLog = NotificationGroup.logOnlyGroup(SSRBundle.message("structural.search.title"));
34   }
35
36   public void info(Object message) {
37     log(message, NotificationType.INFORMATION);
38   }
39
40   public void warn(Object message) {
41     log(message, NotificationType.WARNING);
42   }
43
44   public void error(Object message) {
45     log(message, NotificationType.ERROR);
46   }
47
48   private void log(Object message, NotificationType type) {
49     final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
50     String location = "";
51     for (StackTraceElement e : stackTrace) {
52       final String methodName = e.getMethodName();
53       if ("run".equals(methodName)) {
54         location = "(" + e.getFileName() + ":" + e.getLineNumber() + ") ";
55         break;
56       }
57     }
58     myEventLog.createNotification(location + String.valueOf(message), type).notify(myProject);
59   }
60 }