]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
Refactor log config
[signal-cli] / src / main / java / org / asamk / signal / Main.java
1 /*
2 Copyright (C) 2015-2022 AsamK and contributors
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package org.asamk.signal;
18
19 import net.sourceforge.argparse4j.ArgumentParsers;
20 import net.sourceforge.argparse4j.DefaultSettings;
21 import net.sourceforge.argparse4j.impl.Arguments;
22 import net.sourceforge.argparse4j.inf.ArgumentParserException;
23 import net.sourceforge.argparse4j.inf.Namespace;
24
25 import org.asamk.signal.commands.exceptions.CommandException;
26 import org.asamk.signal.commands.exceptions.IOErrorException;
27 import org.asamk.signal.commands.exceptions.RateLimitErrorException;
28 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
29 import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
30 import org.asamk.signal.commands.exceptions.UserErrorException;
31 import org.asamk.signal.logging.LogConfigurator;
32 import org.asamk.signal.manager.ManagerLogger;
33 import org.asamk.signal.util.SecurityProvider;
34 import org.bouncycastle.jce.provider.BouncyCastleProvider;
35 import org.slf4j.bridge.SLF4JBridgeHandler;
36
37 import java.io.File;
38 import java.security.Security;
39
40 public class Main {
41
42 public static void main(String[] args) {
43 // enable unlimited strength crypto via Policy, supported on relevant JREs
44 Security.setProperty("crypto.policy", "unlimited");
45 installSecurityProviderWorkaround();
46
47 // Configuring the logger needs to happen before any logger is initialized
48 final var loggingConfig = parseLoggingConfig(args);
49 configureLogging(loggingConfig);
50
51 final var parser = App.buildArgumentParser();
52 final var ns = parser.parseArgsOrFail(args);
53
54 int status = 0;
55 try {
56 new App(ns).init();
57 } catch (CommandException e) {
58 System.err.println(e.getMessage());
59 if (loggingConfig.verboseLevel > 0 && e.getCause() != null) {
60 e.getCause().printStackTrace(System.err);
61 }
62 status = getStatusForError(e);
63 } catch (Throwable e) {
64 e.printStackTrace(System.err);
65 status = 2;
66 }
67 System.exit(status);
68 }
69
70 private static void installSecurityProviderWorkaround() {
71 // Register our own security provider
72 Security.insertProviderAt(new SecurityProvider(), 1);
73 Security.addProvider(new BouncyCastleProvider());
74 }
75
76 private static LoggingConfig parseLoggingConfig(final String[] args) {
77 final var nsLog = parseArgs(args);
78 if (nsLog == null) {
79 return new LoggingConfig(0, null, false);
80 }
81
82 final var verboseLevel = nsLog.getInt("verbose");
83 final var logFile = nsLog.<File>get("log-file");
84 final var scrubLog = nsLog.getBoolean("scrub-log");
85 return new LoggingConfig(verboseLevel, logFile, scrubLog);
86 }
87
88 /**
89 * This method only parses commandline args relevant for logging configuration.
90 */
91 private static Namespace parseArgs(String[] args) {
92 var parser = ArgumentParsers.newFor("signal-cli", DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS)
93 .includeArgumentNamesAsKeysInResult(true)
94 .build()
95 .defaultHelp(false);
96 parser.addArgument("-v", "--verbose").action(Arguments.count());
97 parser.addArgument("--log-file").type(File.class);
98 parser.addArgument("--scrub-log").action(Arguments.storeTrue());
99
100 try {
101 return parser.parseKnownArgs(args, null);
102 } catch (ArgumentParserException e) {
103 return null;
104 }
105 }
106
107 private static void configureLogging(final LoggingConfig loggingConfig) {
108 LogConfigurator.setVerboseLevel(loggingConfig.verboseLevel);
109 LogConfigurator.setLogFile(loggingConfig.logFile);
110 LogConfigurator.setScrubSensitiveInformation(loggingConfig.scrubLog);
111
112 if (loggingConfig.verboseLevel > 0) {
113 java.util.logging.Logger.getLogger("")
114 .setLevel(loggingConfig.verboseLevel > 2
115 ? java.util.logging.Level.FINEST
116 : java.util.logging.Level.INFO);
117 ManagerLogger.initLogger();
118 }
119 SLF4JBridgeHandler.removeHandlersForRootLogger();
120 SLF4JBridgeHandler.install();
121 }
122
123 private static int getStatusForError(final CommandException e) {
124 return switch (e) {
125 case UserErrorException userErrorException -> 1;
126 case UnexpectedErrorException unexpectedErrorException -> 2;
127 case IOErrorException ioErrorException -> 3;
128 case UntrustedKeyErrorException untrustedKeyErrorException -> 4;
129 case RateLimitErrorException rateLimitErrorException -> 5;
130 case null -> 2;
131 };
132 }
133
134 private record LoggingConfig(int verboseLevel, File logFile, boolean scrubLog) {}
135 }