/**
 *    Retz
 *    Copyright (C) 2016-2017 Nautilus Technologies, Inc.
 *
 *    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 io.github.retz.misc;

import java.sql.SQLException;

import org.slf4j.Logger;

public final class LogUtil {

    private LogUtil() {
        throw new UnsupportedOperationException();
    }

    public static void error(Logger logger, String message, Throwable throwable) {
        action(logger, message, throwable, (log, m, t) -> log.error("{}", m, t));
    }

    public static void warn(Logger logger, String message, Throwable throwable) {
        action(logger, message, throwable, (log, m, t) -> log.warn("{}", m, t));
    }

    public static void info(Logger logger, String message, Throwable throwable) {
        action(logger, message, throwable, (log, m, t) -> log.info("{}", m, t));
    }

    @FunctionalInterface
    private interface LogConsumer {
        void log(Logger log, String message, Throwable t);
    }

    private static void action(Logger logger, String message, Throwable t, LogConsumer consumer) {
        consumer.log(logger, message, t);
        for (; t != null; t = t.getCause()) {
            if (t instanceof SQLException) {
                SQLException e = (SQLException) t;
                for (e = e.getNextException(); e != null; e = e.getNextException()) {
                    consumer.log(logger, "next exception", e);
                }
            }
        }
    }
}

@@ -1,12 +1,11 @@
 /**
-%0D
 %0A *    R
@@ -7,17 +7,16 @@
     Retz
-%0D
 %0A *    C
@@ -65,21 +65,19 @@
 es, Inc.
-%0D
 %0A *
-%0D
 %0A *    L
@@ -138,17 +138,16 @@
 cense%22);
-%0D
 %0A *    y
@@ -209,17 +209,16 @@
 License.
-%0D
 %0A *    Y
@@ -255,21 +255,19 @@
 cense at
-%0D
 %0A *
-%0D
 %0A *     
@@ -315,13 +315,11 @@
 -2.0
-%0D
 %0A *
-%0D
 %0A * 
@@ -388,17 +388,16 @@
 software
-%0D
 %0A *    d
@@ -460,17 +460,16 @@
 %22 BASIS,
-%0D
 %0A *    W
@@ -539,17 +539,16 @@
 implied.
-%0D
 %0A *    S
@@ -613,17 +613,16 @@
 ions and
-%0D
 %0A *    l
@@ -654,14 +654,12 @@
 nse.
-%0D
 %0A */
-%0D
 %0Apac
@@ -683,19 +683,17 @@
 tz.misc;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -714,19 +714,17 @@
 ception;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -740,19 +740,17 @@
 .Logger;
-%0D%0A%0D
+%0A
 %0Apublic 
@@ -770,19 +770,17 @@
 ogUtil %7B
-%0D%0A%0D
+%0A
 %0A    pri
@@ -795,17 +795,16 @@
 Util() %7B
-%0D
 %0A       
@@ -846,26 +846,23 @@
 ption();
-%0D
 %0A    %7D
-%0D%0A%0D
+%0A
 %0A    pub
@@ -928,33 +928,32 @@
 ble throwable) %7B
-%0D
 %0A        action(
@@ -1014,34 +1014,31 @@
 %7B%7D%22, m, t));
-%0D
 %0A    %7D
-%0D%0A%0D
+%0A
 %0A    public 
@@ -1099,33 +1099,32 @@
 ble throwable) %7B
-%0D
 %0A        action(
@@ -1184,34 +1184,31 @@
 %7B%7D%22, m, t));
-%0D
 %0A    %7D
-%0D%0A%0D
+%0A
 %0A    public 
@@ -1277,17 +1277,16 @@
 wable) %7B
-%0D
 %0A       
@@ -1358,26 +1358,23 @@
  m, t));
-%0D
 %0A    %7D
-%0D%0A%0D
+%0A
 %0A    @Fu
@@ -1390,17 +1390,16 @@
 nterface
-%0D
 %0A    pri
@@ -1426,17 +1426,16 @@
 nsumer %7B
-%0D
 %0A       
@@ -1489,18 +1489,15 @@
  t);
-%0D
 %0A    %7D
-%0D%0A%0D
+%0A
 %0A   
@@ -1591,17 +1591,16 @@
 sumer) %7B
-%0D
 %0A       
@@ -1633,17 +1633,16 @@
 age, t);
-%0D
 %0A       
@@ -1675,25 +1675,24 @@
 etCause()) %7B
-%0D
 %0A           
@@ -1724,17 +1724,16 @@
 ption) %7B
-%0D
 %0A       
@@ -1775,17 +1775,16 @@
 tion) t;
-%0D
 %0A       
@@ -1861,17 +1861,16 @@
 ion()) %7B
-%0D
 %0A       
@@ -1924,17 +1924,16 @@
 on%22, e);
-%0D
 %0A       
@@ -1934,33 +1934,32 @@
                %7D
-%0D
 %0A            %7D%0D%0A
@@ -1952,25 +1952,24 @@
            %7D
-%0D
 %0A        %7D%0D%0A
@@ -1970,16 +1970,13 @@
    %7D
-%0D
 %0A    %7D
-%0D
 %0A%7D
-%0D
 %0A

Replace line feed (CRLF -> LF)