001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import java.text.*;
016
017import org.apache.juneau.marshaller.*;
018
019/**
020 * A utility for logging formatted messages to the console.
021 * Uses the {@link Json5} marshaller for serializing objects so any
022 * POJOs can be used as format arguments.
023 */
024public class Console {
025
026   /**
027    * Prints a message with arguments to {@link System#out}.
028    *
029    * <p>
030    * Arguments are automatically converted to strings using the {@link Json5} marshaller.
031    *
032    * <p>
033    * Useful for debug messages.
034    *
035    * <h5 class='figure'>Example:</h5>
036    * <p class='bjava'>
037    *    Console.<jsm>out</jsm>(<js>"myPojo={0}"</js>, <jv>myPojo</jv>);
038    * </p>
039    *
040    * @param msg The {@link MessageFormat}-styled message.
041    * @param args The arguments sent to the the formatter after running them through the {@link Json5} marshaller.
042    */
043   public static final void out(String msg, Object...args) {
044      System.out.println(format(msg, args));
045   }
046
047   /**
048    * Prints a message with arguments to {@link System#err}.
049    *
050    * <p>
051    * Arguments are automatically converted to strings using the {@link Json5} marshaller.
052    *
053    * <p>
054    * Useful for debug messages.
055    *
056    * <h5 class='figure'>Example:</h5>
057    * <p class='bjava'>
058    *    Console.<jsm>err</jsm>(<js>"myPojo={0}"</js>, <jv>myPojo</jv>);
059    * </p>
060    *
061    * @param msg The {@link MessageFormat}-styled message.
062    * @param args The arguments sent to the the formatter after running them through the {@link Json5} marshaller.
063    */
064   public static final void err(String msg, Object...args) {
065      System.err.println(format(msg, args));  // NOT DEBUG
066   }
067
068   /**
069    * Formats a message with arguments.
070    *
071    * <p>
072    * Arguments are automatically converted to strings using the {@link Json5} marshaller.
073    *
074    * <p>
075    * Useful for debug messages.
076    *
077    * <h5 class='figure'>Example:</h5>
078    * <p class='bjava'>
079    *    String <jv>msg</jv> = Console.<jsm>format</jsm>(<js>"myPojo={0}"</js>, <jv>myPojo</jv>);
080    * </p>
081    *
082    * @param msg The {@link MessageFormat}-styled message.
083    * @param args The arguments sent to the the formatter after running them through the {@link Json5} marshaller.
084    * @return This object.
085    */
086   public static final String format(String msg, Object...args) {
087      for (int i = 0; i < args.length; i++)
088         args[i] = Json5.of(args[i]);
089      return MessageFormat.format(msg, args);
090   }
091}