|
1 | 1 | package com.salesforce.messaging; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Arrays; |
5 | 3 | import java.util.List; |
6 | 4 |
|
7 | 5 | import com.google.common.collect.Lists; |
8 | 6 | import com.google.gson.Gson; |
9 | 7 |
|
10 | 8 | public class CliMessager { |
11 | | - // The START string gives us something to scan for when we're processing output. |
12 | | - private static final String START = "SF-START"; |
13 | | - // The END string lets us know when a message stops, which should prevent bugs involving multi-line output. |
14 | | - private static final String END = "SF-END"; |
15 | | - |
16 | 9 | private static final String REALTIME_START = "SFCA-REALTIME-START"; |
17 | 10 | private static final String REALTIME_END = "SFCA-REALTIME-END"; |
18 | 11 |
|
19 | | - /* Deprecated: Don't maintain state in a class that's essentially used as a utility.*/ |
20 | | - @Deprecated |
21 | | - private static final List<Message> MESSAGES = new ArrayList<>(); |
22 | | - |
23 | | - /** |
24 | | - * Deprecated - switch to static invocation of {@link #postMessage(String, EventKey, String...)} |
25 | | - */ |
26 | | - @Deprecated |
27 | | - public static CliMessager getInstance() { |
28 | | - return LazyHolder.INSTANCE; |
29 | | - } |
30 | | - |
31 | | - /** |
32 | | - * Add exception to pass onto Typescript layer. |
33 | | - * Will be treated as an Error based on the properties set |
34 | | - * in EventKey. Please make sure that EventKey is correct and is |
35 | | - * in sync with messages/EventKeyTemplates.json |
36 | | - * |
37 | | - * @param exception to send to Typescript layer |
38 | | - */ |
39 | | - @Deprecated |
40 | | - public void addMessage(MessagePassableException exception) { |
41 | | - final EventKey eventKey = exception.getEventKey(); |
42 | | - addMessage( |
43 | | - exception.getFullStacktrace(), |
44 | | - eventKey, |
45 | | - exception.getArgs()); |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Add message to pass onto Typescript layer. |
50 | | - * Make sure EventKey is updated with messages/EventKeyTemplates.json |
51 | | - * and has correct properties in the enum. |
52 | | - * |
53 | | - * @param internalLog Information for internal use. Will be logged but not displayed to user |
54 | | - * @param eventKey EventKey to display to user |
55 | | - * @param args String args passed to the EventKey to make the displayed message meaningful |
56 | | - */ |
57 | | - @Deprecated |
58 | | - public void addMessage(String internalLog, EventKey eventKey, String... args) { |
59 | | - final Message message = createMessage(internalLog, eventKey, args); |
60 | | - MESSAGES.add(message); |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - * Publish formatted stdout message to pass onto Typescript layer. |
65 | | - * Make sure EventKey is updated with messages/EventKeyTemplates.json |
66 | | - * and has correct properties in the enum. |
67 | | - * |
68 | | - * @param internalLog Information for internal use. Will be logged but not displayed to user |
69 | | - * @param eventKey EventKey to display to user |
70 | | - * @param args String args passed to the EventKey to make the displayed message meaningful |
71 | | - */ |
72 | | - public static void postMessage(String internalLog, EventKey eventKey, String... args) { |
73 | | - final Message message = createMessage(internalLog, eventKey, args); |
74 | | - final List<Message> messages = Lists.newArrayList(message); |
| 12 | + public static void postLogMessage(String internalLog, LogMessage.LogEventKey logEventKey, String... args) { |
| 13 | + final LogMessage logMessage = LogMessage.create(internalLog, logEventKey, args); |
| 14 | + final List<LogMessage> logMessages = Lists.newArrayList(logMessage); |
75 | 15 |
|
76 | | - final String messageAsJson = new Gson().toJson(messages); |
77 | | - System.out.println(REALTIME_START + messageAsJson + REALTIME_END); |
78 | | - } |
79 | | - |
80 | | - private static Message createMessage(String internalLog, EventKey eventKey, String[] args) { |
81 | | - // Developer error if eventKey was not added to exception and we'll get a bunch of NPEs |
82 | | - assert eventKey != null : "EventKey must not be null"; |
83 | | - // Confirm that the correct number of arguments for the message has been provided |
84 | | - // If this fails, this would be a developer error |
85 | | - assert eventKey.getArgCount() == args.length : "EventKey expected " + eventKey.getArgCount() + " args, received " + args.length; |
86 | | - |
87 | | - final Message message = new Message( |
88 | | - eventKey.getMessageKey(), |
89 | | - Arrays.asList(args), |
90 | | - internalLog, |
91 | | - eventKey.getMessageType(), |
92 | | - eventKey.getMessageHandler(), |
93 | | - eventKey.isVerbose()); |
94 | | - return message; |
95 | | - } |
96 | | - |
97 | | - /** |
98 | | - * Convert all messages stored by the instance into a JSON-formatted string, enclosed in the start and end strings. |
99 | | - * Java code can use this method to log the messages to console, and TypeScript code can seek the start and stop |
100 | | - * strings to get an array of messages that can be deserialized. |
101 | | - * @return |
102 | | - */ |
103 | | - @Deprecated |
104 | | - public String getAllMessagesWithFormatting() { |
105 | | - final String messagesAsJson = getMessagesAsJson(); |
106 | | - return START + messagesAsJson + END; |
107 | | - } |
108 | | - |
109 | | - @Deprecated |
110 | | - private String getMessagesAsJson() { |
111 | | - return new Gson().toJson(MESSAGES); |
112 | | - } |
113 | | - |
114 | | - /** |
115 | | - * TO BE USED ONLY BY TESTS! |
116 | | - * |
117 | | - * @return all messages as JSON without formatting |
118 | | - */ |
119 | | - public String getAllMessages() { |
120 | | - return getMessagesAsJson(); |
| 16 | + final String messageAsJson = new Gson().toJson(logMessages); |
| 17 | + System.out.println(REALTIME_START + messageAsJson + REALTIME_END); |
121 | 18 | } |
122 | 19 |
|
123 | | - /** |
124 | | - * TO BE USED ONLY BY TESTS! |
125 | | - * STAY AWAY!! |
126 | | - */ |
127 | | - public void resetMessages() { |
128 | | - MESSAGES.clear(); |
129 | | - } |
| 20 | + public static void postProgressMessage(String internalLog, ProgressMessage.ProgressEventKey progressEventKey, int progressPercent, String... args) { |
| 21 | + final ProgressMessage progressMessage = ProgressMessage.create(internalLog, progressEventKey, progressPercent, args); |
| 22 | + final List<ProgressMessage> progressMessages = Lists.newArrayList(progressMessage); |
130 | 23 |
|
131 | | - private static final class LazyHolder { |
132 | | - // Postpone initialization until first use |
133 | | - private static final CliMessager INSTANCE = new CliMessager(); |
| 24 | + final String messageAsJson = new Gson().toJson(progressMessages); |
| 25 | + System.out.println(REALTIME_START + messageAsJson + REALTIME_END); |
134 | 26 | } |
135 | 27 | } |
0 commit comments