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