001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package gololang;
012
013import java.text.MessageFormat;
014import java.util.ResourceBundle;
015import java.util.Locale;
016
017/**
018 * Functions to display various localized messages.
019 */
020public final class Messages {
021
022  private static final boolean ANSI = !System.getProperty("os.name").contains("Windows");
023  private static final boolean color = (System.console() != null);
024
025  private static final ResourceBundle MESSAGES = ResourceBundle.getBundle("messages", Locale.getDefault());
026
027  private static final String ERROR = "\u001B[31m"; // red
028  private static final String INFO = "\u001B[34m";  // blue
029  private static final String WARNING = "\u001B[33m"; // yellow
030  private static final String STACK = "\u001B[36m"; // cyan
031
032  private Messages() {
033    // utility class
034  }
035
036  private static boolean withColor() {
037    return ANSI && color;
038  }
039
040  /**
041   * Format a localized message.
042   */
043  public static String message(String key, Object... values) {
044    return MessageFormat.format(MESSAGES.getString(key), values);
045  }
046
047  /**
048   * Return a localized message.
049   */
050  public static String message(String key) {
051    return MESSAGES.getString(key);
052  }
053
054  public static String prefixed(String prefix, String message) {
055    return prefixed(prefix, message, null);
056  }
057
058  public static String prefixed(String prefix, String message, String color) {
059    if (!withColor() || color == null) {
060      return String.format("[%s] %s", MESSAGES.getString(prefix), message);
061    }
062    return String.format("[%s%s\u001B[0m] %s", color, MESSAGES.getString(prefix), message);
063  }
064
065  public static void printPrefixed(String prefix, String message, String color) {
066    System.err.println(prefixed(prefix, message, color));
067  }
068
069  /**
070   * Prints an error message to standard error.
071   */
072  public static void error(Object message, String prefix) {
073    printPrefixed("error", prefix + String.valueOf(message), ERROR);
074  }
075
076  /**
077   * Prints an error message to standard error.
078   */
079  public static void error(Object message) {
080    error(message, "");
081  }
082
083  /**
084   * Prints a warning to standard error.
085   */
086  public static void warning(Object message) {
087    printPrefixed("warning", message.toString(), WARNING);
088  }
089
090  /**
091   * Prints an info message to standard error.
092   */
093  public static void info(Object message) {
094    printPrefixed("info", message.toString(), INFO);
095  }
096
097  /**
098   * Prints an error stack trace to standard error.
099   */
100  public static void printStackTrace(Throwable e) {
101    if (withColor()) {
102      System.err.print(STACK);
103    }
104    e.printStackTrace();
105    if (withColor()) {
106      System.err.println("\u001B[0m");
107    }
108  }
109
110
111
112}