/*
 * 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 java.util.logging;

import java.io.PrintWriter;
import java.io.StringWriter;

/*-[
#import "java/lang/Throwable.h"
#import <asl.h>
]-*/

/**
 * Handler implementation that calls iOS asl_log().
 *
 * @author Tom Ball
 */
class IOSLogHandler extends Handler {

  static class IOSLogFormatter extends Formatter {

    /**
     * Very simple formatter, since asl_log adds its own text.
     */
    @Override
    public String format(LogRecord record) {
      return formatMessage(record);
    }
  }

  private static final String IOS_LOG_MANAGER_DEFAULTS =
      ".level=INFO\nhandlers=java.util.logging.IOSLogHandler\n";

  public IOSLogHandler() {
    setFormatter(new IOSLogFormatter());
  }

  @Override
  public void close() {
    // No action needed
  }

  @Override
  public void flush() {
    // No action needed
  }

  @Override
  public void publish(LogRecord record) {
    if (!isLoggable(record)) {
      return;
    }
    StringBuilder sb = new StringBuilder(getFormatter().format(record));
    int aslLevel;
    switch (record.getLevel().intValue()) {
      case 1000:       // Level.SEVERE
        aslLevel = 3;  // ASL_LEVEL_ERR
        break;
      case 900:        // Level.WARNING
        aslLevel = 4;  // ASL_LEVEL_WARNING
        break;
      case 800:        // Level.INFO
      case 700:        // Level.CONFIG
        aslLevel = 5;  // ASL_LEVEL_NOTICE
        break;
      default:
        aslLevel = 6;  // ASL_LEVEL_INFO
    }

    if (record.getThrown() != null) {
      sb.append('\n');
      StringWriter stringWriter = new StringWriter();
      record.getThrown().printStackTrace(new PrintWriter(stringWriter));
      sb.append(stringWriter.toString());
    }
    log(sb.toString(), aslLevel);
  }

  private native void log(String logMessage, int aslLevel) /*-[
    // Add stderr as a log file, so that log messages are seen on the debug log,
    // and not just the device log.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        asl_add_log_file(NULL, STDERR_FILENO);
    });

    asl_log(NULL, NULL, aslLevel, "%s", [logMessage UTF8String]);
  ]-*/;

  public static String getDefaultProperties() {
    return IOS_LOG_MANAGER_DEFAULTS;
  }
}

@@ -1110,16 +1110,22 @@
 DEFAULTS
+_DEBUG
  =%0A     
@@ -1183,16 +1183,151 @@
 dler%5Cn%22;
+%0A  private static final String IOS_LOG_MANAGER_DEFAULTS_PRODUCTION =%0A      %22.level=SEVERE%5Cnhandlers=java.util.logging.IOSLogHandler%5Cn%22;
 %0A%0A  publ
@@ -2827,16 +2827,23 @@
  static 
+native 
 String g
@@ -2868,49 +2868,196 @@
 s() 
-%7B%0A    return IOS_LOG_MANAGER_DEFAULTS;%0A  %7D
+/*-%5B%0A#ifdef DEBUG%0A    return JavaUtilLoggingIOSLogHandler_IOS_LOG_MANAGER_DEFAULTS_DEBUG_;%0A#else%0A    return JavaUtilLoggingIOSLogHandler_IOS_LOG_MANAGER_DEFAULTS_PRODUCTION_;%0A#endif%0A  %5D-*/;
 %0A%7D%0A

Set default log level to SEVERE in production code. Change on 2014/07/15 by tball <tball@google.com> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=71203255