[LIB-9] Output of application results to console
[chesshog.git] / chesshog-core / src / main / java / org / hedgecode / chess / ChessHogConsole.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess;
18
19 import java.time.LocalDateTime;
20 import java.time.format.DateTimeFormatter;
21 import java.util.Calendar;
22
23 /**
24  *
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public class ChessHogConsole {
29
30     private static final String EMPTY = "";
31     private static final String SPACE = " ";
32
33     private static final String[] ASCII_LOGO = {
34             "  _______     _                   _                  ",
35             " |   |  _|_ _| |___ ___ ___ ___ _| |___              ",
36             " |     | -_| . | . | -_|  _| . | . | -_|             ",
37             " |     |___|___|_  |___|___|___|___|___|             ",
38             " |___|___|     |___|    _               _            ",
39             "                    ___| |_ ___ ___ ___| |_ ___ ___  ",
40             "                   |  _|   | -_|_ -|_ -|   | . | . | ",
41             "                   |___|_|_|___|___|___|_|_|___|_  | ",
42             "                                               |___| "
43     };
44
45     private static final String COPYRIGHT = "Copyright (c) Hedgecode";
46
47     private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
48     private static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
49
50     private static boolean logMode = false;
51
52     private ChessHogConsole() {
53     }
54
55     public static void setLogMode(boolean isLogMode) {
56         logMode = isLogMode;
57     }
58
59     public static void console(String message) {
60         if (logMode) {
61             log(message);
62         } else {
63             output(message);
64         }
65     }
66
67     public static void output(String message) {
68         System.out.println(
69                 message
70         );
71     }
72
73     public static void log(String message) {
74         System.out.println(
75                 logTime() + message
76         );
77     }
78
79     public static void init() {
80         asciiLogo();
81         copyright();
82         version();
83         console(EMPTY);
84         os();
85         jave();
86     }
87
88
89     private static String logTime() {
90         return "[" + TIME_FORMAT.format(LocalDateTime.now()) + "]" + SPACE;
91     }
92
93     private static void asciiLogo() {
94         for (String line : ASCII_LOGO) {
95             console(line);
96         }
97     }
98
99     private static void copyright() {
100         int inceptionYear = Integer.parseInt(
101                 ChessHogProperties.get("chesshog.inception.year")
102         );
103         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
104         String years = currentYear > inceptionYear
105                 ? inceptionYear + "-" + currentYear + SPACE
106                 : inceptionYear + SPACE;
107         console(
108                 alignCenter(years + COPYRIGHT)
109         );
110     }
111
112     private static void version() {
113         console(
114                 alignCenter("v. " + ChessHogProperties.get("chesshog.version"))
115         );
116     }
117
118     private static void os() {
119         console(
120                 "OS:" +
121                         SPACE + System.getProperty("os.name") +
122                         SPACE + System.getProperty("os.version") +
123                         SPACE + System.getProperty("os.arch")
124         );
125     }
126
127     private static void jave() {
128         console(
129                 "JRE:" +
130                         SPACE + System.getProperty("java.runtime.name") +
131                         SPACE + System.getProperty("java.runtime.version")
132         );
133         console(
134                 "JVM:" +
135                         SPACE + System.getProperty("java.vm.vendor") +
136                         SPACE + System.getProperty("java.vm.name") +
137                         SPACE + System.getProperty("java.vm.version")
138         );
139     }
140
141     private static String alignCenter(String message) {
142         int indent = (ASCII_LOGO[0].length() + message.length()) / 2;
143         return String.format("%" + indent + "s", message);
144     }
145
146 }