/*
Copyright 2012 Opera Software ASA

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.opera.core.systems;

import com.opera.core.systems.scope.internal.ServiceCallback;
import com.opera.core.systems.scope.protos.ConsoleLoggerProtos.ConsoleMessage;

import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.logging.StoringLocalLogs;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class OperaLogs extends StoringLocalLogs implements Logs {

  public OperaLogs() {
    super(new HashSet<String>());
  }

  public OperaLogs(Set<String> logTypesToIgnore) {
    super(logTypesToIgnore);
  }

  /**
   * Converts the console messages we receive through Scope to {@link LogEntry}'s for use with
   * WebDriver's {@link Logs} interface.
   */
  public static class ConsoleMessageConverter implements ServiceCallback<ConsoleMessage> {

    private final OperaLogs logs;

    public ConsoleMessageConverter(OperaLogs logs) {
      this.logs = logs;
    }

    public void call(ConsoleMessage message) {
      LogEntry entry = new LogEntry(OperaSeverity.get(message.getSeverity()).toLevel(),
                                    message.getTime(),
                                    message.getDescription());
      logs.addEntry(message.getSource(), entry);
    }

  }

  /**
   * Forwards log entries from {@link OperaDriver} to the WebDriver {@link Logs} interface for
   * remote retrieval.
   */
  public static class DriverLogsHandler extends Handler {

    private final OperaLogs logs;

    public DriverLogsHandler(OperaLogs logs) {
      this.logs = logs;
    }

    public void publish(LogRecord record) {
      LogEntry entry = new LogEntry(record.getLevel(),
                                    System.currentTimeMillis() / 1000L,
                                    record.getMessage());
      logs.addEntry(LogType.DRIVER, entry);
    }

    public void flush() {
    }

    public void close() throws SecurityException {
    }

  }

}
@@ -589,24 +589,104 @@
 e.systems;%0A%0A
+import com.google.common.collect.Lists;%0Aimport com.google.common.collect.Maps;%0A%0A
 import com.o
@@ -815,16 +815,63 @@
 ssage;%0A%0A
+import org.openqa.selenium.logging.LogEntries;%0A
 import o
@@ -996,61 +996,57 @@
 gs;%0A
+%0A
 import 
-org.openqa.selenium.logging.StoringLocalLogs;%0A
+java.util.HashSet;%0Aimport java.util.List;
 %0Aimp
@@ -1051,39 +1051,35 @@
 mport java.util.
-HashSet
+Map
 ;%0Aimport java.ut
@@ -1161,73 +1161,240 @@
 d;%0A%0A
-public class OperaLogs extends 
+// TODO(andreastt): Extend StoringLocalLogs when Selenium 2.26 is out%0Apublic class OperaLogs implements Logs %7B%0A%0A  private final Map%3C
 St
-o
 ring
-LocalLogs implements Logs %7B
+, List%3CLogEntry%3E%3E localLogs = Maps.newHashMap();%0A  private final Set%3CString%3E logTypesToIgnore;
 %0A%0A  
@@ -1418,21 +1418,20 @@
 ) %7B%0A    
-super
+this
 (new Has
@@ -1511,30 +1511,919 @@
     
-super(logTypesToIgnore
+this.logTypesToIgnore = logTypesToIgnore;%0A  %7D%0A%0A  public LogEntries get(String logType) %7B%0A    Iterable%3CLogEntry%3E toReturn = getLocalLogs(logType);%0A    localLogs.remove(logType);%0A    return new LogEntries(toReturn);%0A  %7D%0A%0A  private Iterable%3CLogEntry%3E getLocalLogs(String logType) %7B%0A    if (localLogs.containsKey(logType)) %7B%0A      return localLogs.get(logType);%0A    %7D%0A%0A    return Lists.newArrayList();%0A  %7D%0A%0A  /**%0A   * Add a new log entry to the local storage.%0A   *%0A   * @param logType the log type to store%0A   * @param entry   the entry to store%0A   */%0A  public void addEntry(String logType, LogEntry entry) %7B%0A    if (logTypesToIgnore.contains(logType)) %7B%0A      return;%0A    %7D%0A%0A    if (!localLogs.containsKey(logType)) %7B%0A      localLogs.put(logType, Lists.newArrayList(entry));%0A    %7D else %7B%0A      localLogs.get(logType).add(entry);%0A    %7D%0A  %7D%0A%0A  public Set%3CString%3E getAvailableLogTypes() %7B%0A    return localLogs.keySet(
 );%0A 

Implement Logs ourselves until Selenium 2.26 is out